diff options
-rw-r--r-- | Cargo.lock | 21 | ||||
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | src/lib.rs | 52 |
3 files changed, 74 insertions, 0 deletions
@@ -161,6 +161,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bb1f6b1ce1c140482ea30ddd3335fc0024ac7ee112895426e0a629a6c20adfe3" [[package]] +name = "encoding_rs" +version = "0.8.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8ac63f94732332f44fe654443c46f6375d1939684c17b0afb6cb56b0456e171" +dependencies = [ + "cfg-if", +] + +[[package]] name = "error-chain" version = "0.12.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -223,6 +232,7 @@ dependencies = [ "serde", "serde_json", "thiserror", + "unidiff", ] [[package]] @@ -1018,6 +1028,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" [[package]] +name = "unidiff" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8a62719acf1933bfdbeb73a657ecd9ecece70b405125267dd549e2e2edc232c" +dependencies = [ + "encoding_rs", + "lazy_static", + "regex", +] + +[[package]] name = "untrusted" version = "0.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -9,3 +9,4 @@ regex = "1.3.9" serde = { version = "1.0.114", features = ["derive"] } serde_json = "1.0.56" thiserror = "1.0.20" +unidiff = "0.3.3" @@ -147,4 +147,56 @@ mod tests { "#, ); } + + #[test] + fn unified_diff() { + use unidiff::PatchSet; + + let diff = r#"--- a/command/pr.go ++++ b/command/pr.go +@@ -1,9 +1,11 @@ + package command + + import ( ++ "bufio" // used to input comment + "errors" + "fmt" + "io" ++ "os" // used to input comment +"#; + + let mut patch = PatchSet::new(); + patch.parse(diff).unwrap(); + + println!("{:?}", patch); + println!("{}", patch); + + let lines = patch.files_mut()[0].hunks_mut()[0].lines_mut(); + + // for line in &lines { + // if line.is_removed() { + // } else if line.is_added() { + // line.line_type = unidiff::LINE_TYPE_CONTEXT.to_owned(); + // } + // } + + lines + .iter_mut() + .filter(|l| !l.is_removed()) + // .map(|l| { + .for_each(|l| { + if l.is_added() { + l.line_type = unidiff::LINE_TYPE_CONTEXT.to_owned(); + } + }); + + lines[lines.len() - 2].line_type = unidiff::LINE_TYPE_REMOVED.to_owned(); + + patch.files_mut()[0].hunks_mut()[0].append(unidiff::Line::new( + r#" "os""#, + unidiff::LINE_TYPE_ADDED, + )); + + println!("{}", patch); + } } |