1use crate::core::Patch;
2use dissimilar::{diff, Chunk}; pub fn compute_patches(original: &str, new: &str) -> Vec<Patch> {
5 let chunks = diff(original, new);
6 let mut patches = Vec::new();
7 let mut current_pos = 0;
8
9 for chunk in chunks {
10 match chunk {
11 Chunk::Equal(text) => {
12 current_pos += text.chars().count();
13 }
14 Chunk::Delete(text) => {
15 let end = current_pos + text.chars().count();
17 patches.push(Patch {
18 unit: "text".to_string(),
19 range: format!("[{}:{}]", current_pos, end),
20 content: bytes::Bytes::new(), content_length: Some(0),
22 });
23 }
25 Chunk::Insert(text) => {
26 let bytes = bytes::Bytes::copy_from_slice(text.as_bytes());
28 patches.push(Patch {
29 unit: "text".to_string(),
30 range: format!("[{}:{}]", current_pos, current_pos),
31 content: bytes.clone(),
32 content_length: Some(bytes.len()),
33 });
34 current_pos += text.chars().count();
35 }
36 }
37 }
38
39 patches
42}