//! Demonstrates how to apply a parsed diff to a file use patch::{Line, Patch}; fn apply(diff: Patch, old: &str) -> String { let old_lines = old.lines().collect::>(); let mut out: Vec<&str> = vec![]; let mut old_line = 0; for hunk in diff.hunks { while old_line < hunk.old_range.start - 1 { out.push(old_lines[old_line as usize]); old_line += 1; } old_line += hunk.old_range.count; for line in hunk.lines { match line { Line::Add(s) | Line::Context(s) => out.push(s), Line::Remove(_) => {} } } } out.join("\n") } static LAO: &str = "\ The Way that can be told of is not the eternal Way; The name that can be named is not the eternal name. The Nameless is the origin of Heaven and Earth; The Named is the mother of all things. Therefore let there always be non-being, so we may see their subtlety, And let there always be being, so we may see their outcome. The two are the same, But after they are produced, they have different names. "; static RAW_DIFF: &str = "\ --- lao 2002-02-21 23:30:39.942229878 -0800 +++ tzu 2002-02-21 23:30:50.442260588 -0800 @@ -1,7 +1,6 @@ -The Way that can be told of is not the eternal Way; -The name that can be named is not the eternal name. The Nameless is the origin of Heaven and Earth; -The Named is the mother of all things. +The named is the mother of all things. + Therefore let there always be non-being, so we may see their subtlety, And let there always be being, @@ -9,3 +8,6 @@ The two are the same, But after they are produced, they have different names. +They both may be called deep and profound. +Deeper and more profound, +The door of all subtleties! "; fn main() { let diff = Patch::from_single(RAW_DIFF).unwrap(); let new = apply(diff, LAO); println!("should be tzu:\n\n{}", new); }