pub struct PatchSet { /* private fields */ }Expand description
A collection of patches that can be applied together.
PatchSet handles the complexity of applying multiple patches to the same file
by sorting them appropriately and tracking offset changes. Patches are applied
in reverse order (highest position first) to avoid invalidating subsequent patches.
§Examples
use textum::{Patch, PatchSet, BoundaryMode};
let mut set = PatchSet::new();
set.add(Patch::from_literal_target(
"tests/fixtures/sample.txt".to_string(),
"hello",
BoundaryMode::Include,
"goodbye",
));
set.add(Patch::from_literal_target(
"tests/fixtures/sample.txt".to_string(),
"world",
BoundaryMode::Include,
"rust",
));
let results = set.apply_to_files().unwrap();
assert_eq!(results.get("tests/fixtures/sample.txt").unwrap(), "goodbye rust\n");Implementations§
Source§impl PatchSet
impl PatchSet
Sourcepub fn add(&mut self, patch: Patch)
pub fn add(&mut self, patch: Patch)
Add a patch to this set.
Patches are not applied until apply_to_files is called. Multiple patches
can target the same file.
§Examples
use textum::{Patch, PatchSet, BoundaryMode};
let mut set = PatchSet::new();
set.add(Patch::from_literal_target(
"main.rs".to_string(),
"old",
BoundaryMode::Include,
"new",
));Sourcepub fn apply_to_files(&self) -> Result<HashMap<String, String>, PatchError>
pub fn apply_to_files(&self) -> Result<HashMap<String, String>, PatchError>
Apply all patches in this set to their target files.
Patches are grouped by file and all snippets are resolved before sorting. Resolved ranges are validated for overlaps - if two patches with non-empty replacements have overlapping resolved ranges, an error is returned.
Patches are then sorted by reverse character index (highest first) and applied sequentially to maintain stable positions. The resulting file contents are returned as a map from file path to content.
This method reads files from disk, applies all patches for that file, and returns the modified content. It does not write to disk - use the returned map to write files as needed.
§Errors
Returns an error if:
- any file cannot be read,
- any snippet cannot be resolved,
- resolved ranges overlap with non-empty replacements,
- or any patch has an invalid range.
If an error occurs, no files are modified.
§Examples
use textum::{Patch, PatchSet};
let mut set = PatchSet::new();
set.add(Patch::from_literal_target(
"tests/fixtures/sample.txt".to_string(),
"world",
textum::BoundaryMode::Include,
"rust",
));
let results = set.apply_to_files().unwrap();
assert_eq!(results.get("tests/fixtures/sample.txt").unwrap(), "hello rust\n");