Skip to main content

apply_hunks

Function apply_hunks 

Source
pub fn apply_hunks(
    path: &str,
    text: &str,
    hunks: &[Hunk],
) -> Result<String, PatchError>
Expand description

Applies hunks to text, returning the new text (spec 3.1 §7.26).

§Errors

Returns a PatchError naming path, the hunk that does not apply, its lines and the nearest candidate lines in the file.

§Example

use adk_devtools::patch::{PatchOp, apply_hunks, parse_patch};

let patch = parse_patch("*** Begin Patch\n*** Update File: a.txt\n@@\n one\n-two\n+2\n*** End Patch")?;
let PatchOp::Update { hunks, .. } = &patch.ops[0] else { unreachable!() };
assert_eq!(apply_hunks("a.txt", "one\r\ntwo\r\n", hunks)?, "one\r\n2\r\n");