my fork of dmp
Diffstat (limited to 'src/dmp.rs')
| -rw-r--r-- | src/dmp.rs | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/src/dmp.rs b/src/dmp.rs new file mode 100644 index 0000000..72af7cf --- /dev/null +++ b/src/dmp.rs @@ -0,0 +1,62 @@ + +/** + * The data structure representing a diff is an array of tuples: + * [[DIFF_DELETE, 'Hello'], [DIFF_INSERT, 'Goodbye'], [DIFF_EQUAL, ' world.']] + * which means: delete 'Hello', add 'Goodbye' and keep ' world.' + */ + +/// Enum representing the different ops of diff +#[derive(Debug, PartialEq, Eq)] +pub enum Ops { + Delete, + Insert, + Equal +} + +/// A structure representing a diff +/// (Ops::Delete, String::new("Hello")) means delete `Hello` +/// (Ops::Insert, String::new("Goodbye")) means add `Goodbye` +/// (Ops::Equal, String::new("World")) means keep world +#[derive(Debug, PartialEq, Eq)] +pub struct Diff(Ops, String); + +impl Diff { + /// Create a new diff object + pub fn new(op: Ops, text: &str) -> Self { + Self(op, text.to_string()) + } +} + +pub struct DiffMatchPatch { + checklines: Option<bool> +} + +impl DiffMatchPatch { + fn checklines(&self) -> bool { + self.checklines.map_or(true, |c| c) + } +} + +impl DiffMatchPatch { + pub fn diff_main(&self, old: &str, new: &str) -> Vec<Diff> { + // First, check if lhs and rhs are equal + if old == new { + if old.is_empty() { + return Vec::new(); + } + + return vec![Diff::new(Ops::Equal, old)]; + } + + if old.is_empty() { + return vec![Diff::new(Ops::Insert, new)] + } + + if new.is_empty() { + return vec![Diff::new(Ops::Delete, old)] + } + + + todo!() + } +}
\ No newline at end of file |