From da32711e1a3f7c11e798563dd2f39d4035f33c11 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Wed, 27 Jan 2021 18:08:36 +0100 Subject: [PATCH] Added DiffHook::apply_to_hook --- CHANGELOG.md | 4 ++++ src/algorithms/capture.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a27ef1f..77f9091 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ All notable changes to similar are documented here. +## 0.5.0 + +* Add `DiffOp::apply_to_hook` to apply a captured op to a diff hook. + ## 0.4.0 * Change `get_close_matches` to use Python's quick ratio optimization diff --git a/src/algorithms/capture.rs b/src/algorithms/capture.rs index c2393cd..537026d 100644 --- a/src/algorithms/capture.rs +++ b/src/algorithms/capture.rs @@ -109,6 +109,33 @@ impl DiffOp { ), } } + + /// Apply this operation to a diff hook. + pub fn apply_to_hook(&self, d: &mut D) -> Result<(), D::Error> { + match *self { + DiffOp::Equal { + old_index, + new_index, + len, + } => d.equal(old_index, new_index, len), + DiffOp::Delete { + old_index, + old_len, + new_index, + } => d.delete(old_index, old_len, new_index), + DiffOp::Insert { + old_index, + new_index, + new_len, + } => d.insert(old_index, new_index, new_len), + DiffOp::Replace { + old_index, + old_len, + new_index, + new_len, + } => d.replace(old_index, old_len, new_index, new_len), + } + } } /// A [`DiffHook`] that captures all diff operations.