Skip to main content

braid_core/fs/
diff.rs

1use crate::core::Patch;
2use dissimilar::{diff, Chunk}; // Use crate Patch type
3
4pub 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                // Delete: Range [start, end]
16                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(), // Empty content
21                    content_length: Some(0),
22                });
23                // Current pos stays same (text removed)
24            }
25            Chunk::Insert(text) => {
26                // Insert: Range [pos, pos]
27                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    // Optimize: Merge adjacent delete/inserts? (Replacements)
40    // For now, simple list is fine.
41    patches
42}