PatchSet

Struct PatchSet 

Source
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

Source

pub fn new() -> Self

Create a new empty patch set.

§Examples
use textum::PatchSet;

let set = PatchSet::new();
Source

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",
));
Source

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");

Trait Implementations§

Source§

impl Default for PatchSet

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.