Skip to main content

apply

Function apply 

Source
pub fn apply(
    path: &str,
    text: &str,
    edits: &[Replacement],
) -> Result<Applied, ToolExecutionError>
Expand description

Makes edits to text, the whole text of the file at path, exactly as Edit does, without touching the file: each edit is matched, exactly or loosely, against the text the edits before it left, and all are applied or none. A byte-order mark and CRLF line endings are kept. path names the file in the FileChange and in an error.

use rig_tools::{Replacement, edit::apply};

let applied = apply("a.txt", "one\r\ntwo\r\n", &[Replacement::new("two", "2")])?;
assert_eq!(applied.text, "one\r\n2\r\n");
assert_eq!((applied.change.added, applied.change.removed), (1, 1));
let missed = apply("a.txt", "one\n", &[Replacement::new("three", "3")]);
assert!(missed.is_err_and(|error| error.to_string().contains("`a.txt` was not changed")));

ยงErrors

When an edit matches nowhere or more than once, or the edits change nothing: the error says what to send instead.