pub enum PatchInput<'a, T: DType> {
Texts(&'a str, &'a str),
Diffs(&'a [Diff<T>]),
TextDiffs(&'a str, &'a [Diff<T>]),
}
Variants§
Implementations§
Source§impl<'a, T: DType> PatchInput<'a, T>
impl<'a, T: DType> PatchInput<'a, T>
pub fn new_text_text(old: &'a str, new: &'a str) -> Self
Sourcepub fn new_diffs(diffs: &'a [Diff<T>]) -> Self
pub fn new_diffs(diffs: &'a [Diff<T>]) -> Self
Examples found in repository?
examples/delta.rs (line 49)
42fn at_destination(delta: &str) -> Result<(), Error> {
43 // initializing the module
44 let dmp = DiffMatchPatch::new();
45
46 // lets recreate the diffs from the minimal `delta` string
47 let delta = dmp.diff_from_delta::<Efficient>(TXT_OLD, delta)?;
48 // Additional step of conveting `delta` -> `patches`
49 let patches = dmp.patch_make(PatchInput::new_diffs(&delta))?;
50
51 // Now, lets apply these patches to the `old_txt` which is the original to get the new text
52 let (new_txt, ops) = dmp.patch_apply(&patches, TXT_OLD)?;
53
54 // Lets print out if the ops succeeded or not
55 ops.iter()
56 .for_each(|&o| println!("{}", if o { "OK" } else { "FAIL" }));
57
58 // If everything goes as per plan you should see
59 // OK
60 // OK
61 // ... and so on
62
63 // lets check out if our `NEW_TXT` (presumably the edited one)
64 if new_txt != TXT_NEW {
65 return Err(Error::InvalidInput);
66 }
67
68 println!("Wallah! Patch applied successfully!");
69
70 Ok(())
71}
More examples
examples/compat.rs (line 31)
23fn at_source() -> Result<String, Error> {
24 // initializing the module
25 let dmp = DiffMatchPatch::new();
26
27 // create a list of diffs
28 let diffs = dmp.diff_main::<Efficient>(TXT_OLD, TXT_NEW)?;
29
30 // Now, we are going to create a list of `patches` to be applied to the old text to get the new text
31 let patches = dmp.patch_make(PatchInput::new_diffs(&diffs))?;
32
33 // in the real world you are going to transmit or store this diff serialized to undiff format to be consumed or used somewhere elese
34 let patch_txt = dmp.patch_to_text(&patches);
35
36 // lets see how our patches look
37 println!("{patch_txt:?}");
38 // You should see something like this
39 // @@ -22,225 +22,250 @@\n f a \n-m\n+carto\n o\n-der\n n \n-Major-Ge\n+i\n n\n-er\n+dividu\n al,%0A\n-I've\n+My\n \n-i\n+a\n n\n-for\n+i\n mation\n+'s\n \n-veget\n+comic\n a\n-b\n l\n-e\n , \n-a\n+u\n n\n-im\n+usu\n al, and \n+whi\n m\n+s\n i\n-ner\n+c\n al,%0AI\n+'m\n \n-know \n+qui\n t\n-h\n e \n-kings of Engl\n a\n-n\n d\n-,\n+ept\n a\n+t fu\n n\n-d\n+ny\n \n-I\n+gags,\n \n-qu\n+c\n o\n-t\n+m\n e\n+dic\n the\n+ory\n \n-fights\n+I\n h\n-isto\n+ave \n r\n-ic\n+e\n a\n-l\n+d\n ,%0AFrom \n-M\n+wicked puns \n a\n-ra\n+nd s\n t\n-h\n+upid j\n o\n-n\n+kes\n to \n-W\n+anvils th\n at\n-e\n+ d\n r\n-l\n o\n+p \n o\n-, i\n n \n+y\n o\n-rde\n+u\n r \n-cat\n+h\n e\n-goric\n a\n-l\n+d\n .%0A%0A\n-L\n+Now, l\n et's \n-sta\n+explo\n r\n-t with\n+e\n some \n-bas\n+emot\n i\n-c\n+onal extreme\n s %F0%9F\n-%98\n+%8C\n %8A. W\n@@ -282,55 +282,53 @@\n our \n+ec\n sta\n-ndard sm\n+t\n i\n-ley\n+c\n face %F0%9F\n-%99%82\n+%A4%A9\n , your \n+deva\n s\n+t\n a\n+te\n d face \n-%E2\n+%F0%9F\n %98\n-%B9%EF%B8%8F\n+%AD\n , an\n@@ -338,53 +338,60 @@\n our \n-ang\n+utte\n r\n+l\n y \n+confused \n face %F0%9F\n-%98%A0\n+%A4%AF\n . But \n-w\n+th\n a\n-i\n t\n-, there\n 's \n-m\n+n\n o\n-re\n+t all\n ! %F0%9F%A4\n-%A9\n+%94\n We'\n@@ -411,20 +411,14 @@\n ome \n-more comp\n+subt\n le\n-x\n emo\n@@ -435,78 +435,15 @@\n %F0%9F%98\n-%8D, %F0%9F%A4%A4, and %F0%9F%9A%80. And let's not forget about the classics: %F0%9F%98%89\n+%90\n , %F0%9F\n-%91%8D\n+%99%83\n , an\n@@ -451,6 +451,6 @@\n %F0%9F%91\n-%8F\n+%80\n .\n
40
41 Ok(patch_txt)
42}
examples/efficiency.rs (line 39)
31fn at_source() -> Result<String, Error> {
32 // initializing the module
33 let dmp = DiffMatchPatch::new();
34
35 // create a list of diffs
36 let diffs = dmp.diff_main::<Efficient>(TXT_OLD, TXT_NEW)?;
37
38 // Now, we are going to create a list of `patches` to be applied to the old text to get the new text
39 let patches = dmp.patch_make(PatchInput::new_diffs(&diffs))?;
40
41 // in the real world you are going to transmit or store this diff serialized to undiff format to be consumed or used somewhere elese
42 let patch_txt = dmp.patch_to_text(&patches);
43
44 // lets see how our patches look
45 println!("{patch_txt:?}");
46 // You should see something like this
47 // @@ -22,225 +22,250 @@\n f a \n-m\n+carto\n o\n-der\n n \n-Major-Ge\n+i\n n\n-er\n+dividu\n al,%0A\n-I've\n+My\n \n-i\n+a\n n\n-for\n+i\n mation\n+'s\n \n-veget\n+comic\n a\n-b\n l\n-e\n , \n-a\n+u\n n\n-im\n+usu\n al, and \n+whi\n m\n+s\n i\n-ner\n+c\n al,%0AI\n+'m\n \n-know \n+qui\n t\n-h\n e \n-kings of Engl\n a\n-n\n d\n-,\n+ept\n a\n+t fu\n n\n-d\n+ny\n \n-I\n+gags,\n \n-qu\n+c\n o\n-t\n+m\n e\n+dic\n the\n+ory\n \n-fights\n+I\n h\n-isto\n+ave \n r\n-ic\n+e\n a\n-l\n+d\n ,%0AFrom \n-M\n+wicked puns \n a\n-ra\n+nd s\n t\n-h\n+upid j\n o\n-n\n+kes\n to \n-W\n+anvils th\n at\n-e\n+ d\n r\n-l\n o\n+p \n o\n-, i\n n \n+y\n o\n-rde\n+u\n r \n-cat\n+h\n e\n-goric\n a\n-l\n+d\n .%0A%0A\n-L\n+Now, l\n et's \n-sta\n+explo\n r\n-t with\n+e\n some \n-bas\n+emot\n i\n-c\n+onal extreme\n s %F0%9F\n-%98\n+%8C\n %8A. W\n@@ -282,55 +282,53 @@\n our \n+ec\n sta\n-ndard sm\n+t\n i\n-ley\n+c\n face %F0%9F\n-%99%82\n+%A4%A9\n , your \n+deva\n s\n+t\n a\n+te\n d face \n-%E2\n+%F0%9F\n %98\n-%B9%EF%B8%8F\n+%AD\n , an\n@@ -338,53 +338,60 @@\n our \n-ang\n+utte\n r\n+l\n y \n+confused \n face %F0%9F\n-%98%A0\n+%A4%AF\n . But \n-w\n+th\n a\n-i\n t\n-, there\n 's \n-m\n+n\n o\n-re\n+t all\n ! %F0%9F%A4\n-%A9\n+%94\n We'\n@@ -411,20 +411,14 @@\n ome \n-more comp\n+subt\n le\n-x\n emo\n@@ -435,78 +435,15 @@\n %F0%9F%98\n-%8D, %F0%9F%A4%A4, and %F0%9F%9A%80. And let's not forget about the classics: %F0%9F%98%89\n+%90\n , %F0%9F\n-%91%8D\n+%99%83\n , an\n@@ -451,6 +451,6 @@\n %F0%9F%91\n-%8F\n+%80\n .\n
48
49 Ok(patch_txt)
50}
pub fn new_text_diffs(old: &'a str, diffs: &'a [Diff<T>]) -> Self
Auto Trait Implementations§
impl<'a, T> Freeze for PatchInput<'a, T>
impl<'a, T> RefUnwindSafe for PatchInput<'a, T>where
T: RefUnwindSafe,
impl<'a, T> Send for PatchInput<'a, T>where
T: Sync,
impl<'a, T> Sync for PatchInput<'a, T>where
T: Sync,
impl<'a, T> Unpin for PatchInput<'a, T>
impl<'a, T> UnwindSafe for PatchInput<'a, T>where
T: RefUnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more