Crate patch

source ·
Expand description

Parse and produce patch files (diffs) in the Unified Format.

The format is not fully specified, but people like Guido van Rossum have done the work to figure out the details.

The parser attempts to be forgiving enough to be compatible with diffs produced by programs like git. It accomplishes this by ignoring the additional code context and information provided in the diff by those programs.

Example

// Make sure you add the `patch` crate to the `[dependencies]` key of your Cargo.toml file.
use patch::Patch;

let sample = "\
--- before.py
+++ path/to/after.py
@@ -1,4 +1,4 @@
-bacon
-eggs
-ham
+python
+eggy
+hamster
 guido\n";

let patch = Patch::from_single(sample)?;
assert_eq!(&patch.old.path, "before.py");
assert_eq!(&patch.new.path, "path/to/after.py");

// Print out the parsed patch file in its Rust representation
println!("{:#?}", patch);

// Print out the parsed patch file in the Unified Format. For input that was originally in the
// Unified Format, this will produce output identical to that original input.
println!("{}", patch); // use format!("{}\n", patch) to get this as a String

Structs

The file path and any additional info of either the old file or the new file
One area where the files differ
Type returned when an error occurs while parsing a patch
A complete patch summarizing the differences between two files
A range of lines in a given file

Enums

Additional metadata provided with the file path
A line of the old file, new file, or both