Skip to main content

teaql_tool_std/
diff.rs

1use similar::{ChangeTag, TextDiff};
2
3#[derive(Debug, Clone)]
4pub struct DiffTool;
5
6impl DiffTool {
7    pub fn new() -> Self { Self }
8
9    /// Returns a textual diff showing differences between old and new text
10    pub fn text_diff(&self, old: &str, new: &str) -> String {
11        let diff = TextDiff::from_lines(old, new);
12        let mut result = String::new();
13        for change in diff.iter_all_changes() {
14            let sign = match change.tag() {
15                ChangeTag::Delete => "-",
16                ChangeTag::Insert => "+",
17                ChangeTag::Equal => " ",
18            };
19            result.push_str(&format!("{}{}", sign, change));
20        }
21        result
22    }
23}
24
25impl Default for DiffTool {
26    fn default() -> Self { Self::new() }
27}