From b23a817508362c3cf93138509c0f30cd0b42ecd9 Mon Sep 17 00:00:00 2001 From: Joao Duarte Date: Thu, 29 Feb 2024 23:32:23 -0300 Subject: [PATCH] refactor(merge): Remove usage of unwrap for more idiomatic rust --- merge/src/ordered_merge.rs | 118 +++++++++++++++++------------------ merge/src/unordered_merge.rs | 76 ++++++++++------------ 2 files changed, 91 insertions(+), 103 deletions(-) diff --git a/merge/src/ordered_merge.rs b/merge/src/ordered_merge.rs index 94d34ed..e5477e5 100644 --- a/merge/src/ordered_merge.rs +++ b/merge/src/ordered_merge.rs @@ -23,16 +23,16 @@ pub fn ordered_merge<'a>( let mut children_left_it = left.children.iter(); let mut children_right_it = right.children.iter(); - let mut cur_left = children_left_it.next(); - let mut cur_right = children_right_it.next(); - - while cur_left.is_some() && cur_right.is_some() { - let matching_base_left = base_left_matchings.find_matching_for(cur_left.unwrap()); - let matching_base_right = base_right_matchings.find_matching_for(cur_right.unwrap()); - let left_matching_in_right = left_right_matchings.find_matching_for(cur_left.unwrap()); - let right_matching_in_left = left_right_matchings.find_matching_for(cur_right.unwrap()); + let mut cur_left_option = children_left_it.next(); + let mut cur_right_option = children_right_it.next(); + + while let (Some(cur_left), Some(cur_right)) = (cur_left_option, cur_right_option) { + let matching_base_left = base_left_matchings.find_matching_for(cur_left); + let matching_base_right = base_right_matchings.find_matching_for(cur_right); + let left_matching_in_right = left_right_matchings.find_matching_for(cur_left); + let right_matching_in_left = left_right_matchings.find_matching_for(cur_right); let has_bidirectional_matching_left_right = - left_right_matchings.has_bidirectional_matching(cur_left.unwrap(), cur_right.unwrap()); + left_right_matchings.has_bidirectional_matching(cur_left, cur_right); match ( has_bidirectional_matching_left_right, @@ -43,76 +43,76 @@ pub fn ordered_merge<'a>( ) { (true, Some(_), Some(_), Some(_), Some(_)) => { result_children.push(crate::merge( - cur_left.unwrap(), - cur_left.unwrap(), - cur_right.unwrap(), + cur_left, + cur_left, + cur_right, base_left_matchings, base_right_matchings, left_right_matchings, )?); - cur_left = children_left_it.next(); - cur_right = children_right_it.next(); + cur_left_option = children_left_it.next(); + cur_right_option = children_right_it.next(); } (true, Some(_), None, Some(_), None) => { result_children.push(crate::merge( - cur_left.unwrap(), - cur_left.unwrap(), - cur_right.unwrap(), + cur_left, + cur_left, + cur_right, base_left_matchings, base_right_matchings, left_right_matchings, )?); - cur_left = children_left_it.next(); - cur_right = children_right_it.next(); + cur_left_option = children_left_it.next(); + cur_right_option = children_right_it.next(); } (false, Some(_), Some(_), None, Some(matching_base_right)) => { if !matching_base_right.is_perfect_match { result_children.push(MergedCSTNode::Conflict { left: None, - right: Some(Box::new(cur_right.unwrap().to_owned().into())), + right: Some(Box::new(cur_right.to_owned().into())), }); } - cur_right = children_right_it.next(); + cur_right_option = children_right_it.next(); } (false, Some(_), Some(_), None, None) => { - result_children.push(cur_right.unwrap().to_owned().into()); + result_children.push(cur_right.to_owned().into()); - cur_right = children_right_it.next(); + cur_right_option = children_right_it.next(); } (false, Some(_), None, None, Some(matching_base_right)) => { if !matching_base_right.is_perfect_match { result_children.push(MergedCSTNode::Conflict { left: None, - right: Some(Box::new(cur_right.unwrap().to_owned().into())), + right: Some(Box::new(cur_right.to_owned().into())), }) } - cur_right = children_right_it.next(); + cur_right_option = children_right_it.next(); } (false, Some(_), None, None, None) => { - result_children.push(cur_right.unwrap().to_owned().into()); - cur_right = children_right_it.next(); + result_children.push(cur_right.to_owned().into()); + cur_right_option = children_right_it.next(); } (false, None, Some(matching_base_left), Some(_), Some(_)) => { if !matching_base_left.is_perfect_match { result_children.push(MergedCSTNode::Conflict { - left: Some(Box::new(cur_left.unwrap().to_owned().into())), + left: Some(Box::new(cur_left.to_owned().into())), right: None, }); } - cur_left = children_left_it.next(); + cur_left_option = children_left_it.next(); } (false, None, Some(matching_base_left), Some(_), None) => { if !matching_base_left.is_perfect_match { result_children.push(MergedCSTNode::Conflict { - left: Some(Box::new(cur_left.unwrap().to_owned().into())), + left: Some(Box::new(cur_left.to_owned().into())), right: None, }) } - cur_left = children_left_it.next(); + cur_left_option = children_left_it.next(); } (false, None, Some(matching_base_left), None, Some(matching_base_right)) => { match ( @@ -121,63 +121,63 @@ pub fn ordered_merge<'a>( ) { (true, true) => {} (true, false) => result_children.push(MergedCSTNode::Conflict { - left: Some(Box::new(cur_left.unwrap().to_owned().into())), + left: Some(Box::new(cur_left.to_owned().into())), right: None, }), (false, true) => result_children.push(MergedCSTNode::Conflict { left: None, - right: Some(Box::new(cur_right.unwrap().to_owned().into())), + right: Some(Box::new(cur_right.to_owned().into())), }), (false, false) => result_children.push(MergedCSTNode::Conflict { - left: Some(Box::new(cur_left.unwrap().to_owned().into())), - right: Some(Box::new(cur_right.unwrap().to_owned().into())), + left: Some(Box::new(cur_left.to_owned().into())), + right: Some(Box::new(cur_right.to_owned().into())), }), }; - cur_left = children_left_it.next(); - cur_right = children_right_it.next(); + cur_left_option = children_left_it.next(); + cur_right_option = children_right_it.next(); } (false, None, Some(matching_base_left), None, None) => { - result_children.push(cur_right.unwrap().to_owned().into()); + result_children.push(cur_right.to_owned().into()); if !matching_base_left.is_perfect_match { result_children.push(MergedCSTNode::Conflict { - left: Some(Box::new(cur_left.unwrap().to_owned().into())), + left: Some(Box::new(cur_left.to_owned().into())), right: None, }) } - cur_left = children_left_it.next(); - cur_right = children_right_it.next(); + cur_left_option = children_left_it.next(); + cur_right_option = children_right_it.next(); } (false, None, None, Some(_), Some(_)) => { - result_children.push(cur_left.unwrap().to_owned().into()); - cur_left = children_left_it.next(); + result_children.push(cur_left.to_owned().into()); + cur_left_option = children_left_it.next(); } (false, None, None, Some(_), None) => { - result_children.push(cur_left.unwrap().to_owned().into()); - cur_left = children_left_it.next(); + result_children.push(cur_left.to_owned().into()); + cur_left_option = children_left_it.next(); } (false, None, None, None, Some(matching_base_right)) => { - result_children.push(cur_left.unwrap().to_owned().into()); + result_children.push(cur_left.to_owned().into()); if !matching_base_right.is_perfect_match { result_children.push(MergedCSTNode::Conflict { left: None, - right: Some(Box::new(cur_right.unwrap().to_owned().into())), + right: Some(Box::new(cur_right.to_owned().into())), }) } - cur_left = children_left_it.next(); - cur_right = children_right_it.next(); + cur_left_option = children_left_it.next(); + cur_right_option = children_right_it.next(); } (false, None, None, None, None) => { result_children.push(MergedCSTNode::Conflict { - left: Some(Box::new(cur_left.unwrap().to_owned().into())), - right: Some(Box::new(cur_right.unwrap().to_owned().into())), + left: Some(Box::new(cur_left.to_owned().into())), + right: Some(Box::new(cur_right.to_owned().into())), }); - cur_left = children_left_it.next(); - cur_right = children_right_it.next(); + cur_left_option = children_left_it.next(); + cur_right_option = children_right_it.next(); } (a, b, c, d, e) => { return Err(MergeError::InvalidMatchingConfiguration( @@ -191,14 +191,14 @@ pub fn ordered_merge<'a>( } } - while cur_left.is_some() { - result_children.push(cur_left.unwrap().to_owned().into()); - cur_left = children_left_it.next(); + while let Some(cur_left) = cur_left_option { + result_children.push(cur_left.to_owned().into()); + cur_left_option = children_left_it.next(); } - while cur_right.is_some() { - result_children.push(cur_right.unwrap().to_owned().into()); - cur_right = children_right_it.next(); + while let Some(cur_right) = cur_right_option { + result_children.push(cur_right.to_owned().into()); + cur_right_option = children_right_it.next(); } Ok(MergedCSTNode::NonTerminal { diff --git a/merge/src/unordered_merge.rs b/merge/src/unordered_merge.rs index c391563..85ee3e6 100644 --- a/merge/src/unordered_merge.rs +++ b/merge/src/unordered_merge.rs @@ -47,17 +47,14 @@ pub fn unordered_merge<'a>( processed_nodes.insert(left_child); } (None, Some(right_matching)) => { - result_children.push( - merge( - left_child, - left_child, - right_matching.matching_node, - base_left_matchings, - base_right_matchings, - left_right_matchings, - ) - .unwrap(), - ); + result_children.push(merge( + left_child, + left_child, + right_matching.matching_node, + base_left_matchings, + base_right_matchings, + left_right_matchings, + )?); processed_nodes.insert(left_child); processed_nodes.insert(right_matching.matching_node); } @@ -73,17 +70,14 @@ pub fn unordered_merge<'a>( processed_nodes.insert(left_child); } (Some(_), Some(right_matching)) => { - result_children.push( - merge( - left_child, - left_child, - right_matching.matching_node, - base_left_matchings, - base_right_matchings, - left_right_matchings, - ) - .unwrap(), - ); + result_children.push(merge( + left_child, + left_child, + right_matching.matching_node, + base_left_matchings, + base_right_matchings, + left_right_matchings, + )?); processed_nodes.insert(left_child); processed_nodes.insert(right_matching.matching_node); } @@ -104,17 +98,14 @@ pub fn unordered_merge<'a>( result_children.push(right_child.to_owned().into()); } (None, Some(matching_left_right)) => { - result_children.push( - merge( - right_child, - matching_left_right.matching_node, - right_child, - base_left_matchings, - base_right_matchings, - left_right_matchings, - ) - .unwrap(), - ); + result_children.push(merge( + right_child, + matching_left_right.matching_node, + right_child, + base_left_matchings, + base_right_matchings, + left_right_matchings, + )?); } // Removed in left (Some(matching_base_right), None) => { @@ -127,17 +118,14 @@ pub fn unordered_merge<'a>( } } (Some(_), Some(matching_left_right)) => { - result_children.push( - merge( - right_child, - matching_left_right.matching_node, - right_child, - base_left_matchings, - base_right_matchings, - left_right_matchings, - ) - .unwrap(), - ); + result_children.push(merge( + right_child, + matching_left_right.matching_node, + right_child, + base_left_matchings, + base_right_matchings, + left_right_matchings, + )?); } } }