Add ability to restore sequence after differ
This commit is contained in:
@@ -181,6 +181,28 @@ impl Differ{
|
||||
res
|
||||
}
|
||||
|
||||
pub fn restore(delta: &Vec<String>, which: usize) -> Vec<String> {
|
||||
if !(which == 1 || which == 2) {
|
||||
panic!("Second parameter must be 1 or 2");
|
||||
}
|
||||
let mut res = Vec::new();
|
||||
let mut tag = String::new();
|
||||
if which == 1 {
|
||||
tag = "- ".to_string();
|
||||
} else {
|
||||
tag = "+ ".to_string();
|
||||
}
|
||||
let prefixes = vec![tag, " ".to_string()];
|
||||
for line in delta {
|
||||
for prefix in &prefixes {
|
||||
if line.starts_with(prefix) {
|
||||
res.push( line.split_at(2).1.to_string() );
|
||||
}
|
||||
}
|
||||
}
|
||||
res
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
+1
-1
@@ -124,4 +124,4 @@ pub fn context_diff<T: Sequence>(first_sequence: &T, second_sequence: &T, from_f
|
||||
}
|
||||
}
|
||||
res
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,6 +102,16 @@ fn test_differ_compare() {
|
||||
assert_eq!(result, "- one\n? ^\n+ ore\n? ^\n- two\n- three\n? -\n+ tree\n+ emu\n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_differ_restore() {
|
||||
let first_text = vec!["one\n", "two\n", "three\n"];
|
||||
let second_text = vec!["ore\n", "tree\n", "emu\n"];
|
||||
let differ = Differ::new();
|
||||
let diff = differ.compare(&first_text, &second_text);
|
||||
assert_eq!(first_text, Differ::restore(&diff, 1));
|
||||
assert_eq!(second_text, Differ::restore(&diff, 2));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_unified_diff() {
|
||||
let first_text = "one two three four".split(" ").collect::<Vec<&str>>();
|
||||
|
||||
Reference in New Issue
Block a user