| Crates.io | patcher |
| lib.rs | patcher |
| version | 0.2.1 |
| created_at | 2025-04-23 04:11:40.978755+00 |
| updated_at | 2025-04-23 04:11:40.978755+00 |
| description | patcher is a Rust library for generating and applying Git-style unified diff patches. |
| homepage | https://github.com/tyrchen/patcher |
| repository | https://github.com/tyrchen/patcher |
| max_upload_size | |
| id | 1645007 |
| size | 943,546 |
A Rust library for generating and applying Git-style unified diff patches. See 中文说明.
See Tutorial.
Add to your Cargo.toml:
[dependencies]
patcher = { version = "0.1.0", default-features = false }
Or install the CLI tool:
cargo install patcher
use patcher::{DiffAlgorithm, Differ};
fn main() {
let old_content = "line1\nline2\nline3\nline4";
let new_content = "line1\nline2 modified\nline3\nline4";
let differ = Differ::new(old_content, new_content);
let patch = differ.generate();
println!("{}", patch);
}
use patcher::{DiffAlgorithm, Differ, PatchAlgorithm, Patcher};
fn main() {
let old_content = "line1\nline2\nline3\nline4";
let new_content = "line1\nline2 modified\nline3\nline4";
// Generate a patch
let differ = Differ::new(old_content, new_content);
let patch = differ.generate();
// Apply it to the original content
let patcher = Patcher::new(patch);
let result = patcher.apply(old_content, false).unwrap();
assert_eq!(result, new_content);
}
use patcher::Patch;
fn main() {
let patch_content = "\
--- a/file.txt
+++ b/file.txt
@@ -1,4 +1,4 @@
line1
-line2
+line2 modified
line3
line4
";
let patch = Patch::parse(patch_content).unwrap();
println!("Original file: {}", patch.old_file);
println!("Modified file: {}", patch.new_file);
println!("Number of chunks: {}", patch.chunks.len());
}
use patcher::{MultifilePatch, MultifilePatcher};
use std::path::Path;
fn main() {
// Parse a multi-file patch from a file
let patch_path = Path::new("changes.patch");
let multipatch = MultifilePatch::parse_from_file(patch_path).unwrap();
// Apply all patches to files in the current directory
let patcher = MultifilePatcher::new(multipatch);
let written_files = patcher.apply_and_write(false).unwrap();
println!("Updated files: {:?}", written_files);
}
Patch: Represents a complete diff between two filesChunk: Represents a contiguous section of changesOperation: Represents a single line in a diff (addition, deletion, or context)MultifilePatch: Collection of patches for multiple filesMultifilePatcher: Applies multiple patches to filesDiff: Trait for implementing custom diff logicmyers_diff: Function to apply the efficient Myers algorithm to custom sequence typesThis project is distributed under the terms of MIT.
See LICENSE for details.
Copyright 2025 Tyr Chen