my fork of dmp
First commit
Anubhab Bandyopadhyay 2024-08-04
parent 5591938 · commit 8fc7be0
-rw-r--r--.gitignore1
-rw-r--r--Cargo.lock7
-rw-r--r--Cargo.toml6
-rw-r--r--src/dmp.rs62
-rw-r--r--src/lib.rs1
5 files changed, 77 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..ea8c4bf
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+/target
diff --git a/Cargo.lock b/Cargo.lock
new file mode 100644
index 0000000..6e73aa2
--- /dev/null
+++ b/Cargo.lock
@@ -0,0 +1,7 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 3
+
+[[package]]
+name = "diff-match-patch-rs"
+version = "0.1.0"
diff --git a/Cargo.toml b/Cargo.toml
new file mode 100644
index 0000000..6c47647
--- /dev/null
+++ b/Cargo.toml
@@ -0,0 +1,6 @@
+[package]
+name = "diff-match-patch-rs"
+version = "0.1.0"
+edition = "2021"
+
+[dependencies]
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
diff --git a/src/lib.rs b/src/lib.rs
new file mode 100644
index 0000000..1d92220
--- /dev/null
+++ b/src/lib.rs
@@ -0,0 +1 @@
+pub mod dmp;