Skip to main content

apply_bytes_reporting

Function apply_bytes_reporting 

Source
pub fn apply_bytes_reporting(
    base_image: &[u8],
    diff: &Diff<'_, [u8]>,
    config: &ApplyConfig,
) -> ApplyOutcome
Expand description

Apply a non-utf8 Diff to a base image, reporting whether it was applied, was already applied, or failed.

This is a convenience wrapper around is_diff_applied_with_config and apply_bytes_with_config that answers “apply this, but if it is already applied tell me so instead of applying it again”. It checks for the already-applied case first (via a reverse round-trip) and only forward applies when the diff is genuinely not yet applied. This ordering is necessary under fuzzy matching, where a forward apply of an already-applied diff is unreliable — it may fail, or it may succeed while wrongly re-applying the change (e.g. duplicating an inserted line).

§Examples

use flickzeug::{apply_bytes_reporting, ApplyConfig, ApplyOutcome, Diff};

let patch = "\
--- a/version
+++ b/version
@@ -1 +1 @@
-3.1
+3.12
";
let diff = Diff::from_bytes(patch.as_bytes()).unwrap();
let config = ApplyConfig::default();

// Fresh content: the diff applies and changes it.
assert!(matches!(
    apply_bytes_reporting(b"3.1\n", &diff, &config),
    ApplyOutcome::Applied(..)
));
// Already-patched content: reported as already applied.
assert!(matches!(
    apply_bytes_reporting(b"3.12\n", &diff, &config),
    ApplyOutcome::AlreadyApplied(_)
));