From 5da131f93767dc16ffc82b7d7a8ff7316a3c9516 Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Thu, 6 Jul 2023 05:19:33 -0700 Subject: [PATCH] tree: add a `Diff::from_options()` constructor I'm not sure `Diff` is worth keeping, but as long as we have, it seems that it should have this constructor. --- lib/src/tree.rs | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/lib/src/tree.rs b/lib/src/tree.rs index 08ed2dc42..fb4e685fe 100644 --- a/lib/src/tree.rs +++ b/lib/src/tree.rs @@ -302,6 +302,15 @@ pub enum Diff { } impl Diff { + pub fn from_options(left: Option, right: Option) -> Self { + match (left, right) { + (Some(left), Some(right)) => Diff::Modified(left, right), + (None, Some(right)) => Diff::Added(right), + (Some(left), None) => Diff::Removed(left), + (None, None) => panic!("left and right cannot both be None"), + } + } + pub fn into_options(self) -> (Option, Option) { match self { Diff::Modified(left, right) => (Some(left), Some(right)), @@ -468,23 +477,10 @@ impl Iterator for TreeDiffIterator<'_> { ); } } else if !tree_before && !tree_after { - match (before, after) { - (Some(file_before), Some(file_after)) => { - return Some(( - file_path, - Diff::Modified(file_before.clone(), file_after.clone()), - )); - } - (None, Some(file_after)) => { - return Some((file_path, Diff::Added(file_after.clone()))); - } - (Some(file_before), None) => { - return Some((file_path, Diff::Removed(file_before.clone()))); - } - (None, None) => { - panic!("unexpected diff") - } - } + return Some(( + file_path, + Diff::from_options(before.cloned(), after.cloned()), + )); } } }