diff --git a/Cargo.lock b/Cargo.lock index 549f1e34ab..816c5294dd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -352,6 +352,12 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e3b5ca7a04898ad4bcd41c90c5285445ff5b791899bb1b0abdd2a2aa791211d7" +[[package]] +name = "bytecount" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c676a478f63e9fa2dd5368a42f28bba0d6c560b775f38583c8bbaa7fcd67c9c" + [[package]] name = "byteorder" version = "1.4.3" @@ -1725,6 +1731,15 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" +[[package]] +name = "operational-transform" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fcdc542261d37288fd18ccb2eb1fdf333d8d10d12cce21d3f586d6e74a5511df" +dependencies = [ + "bytecount", +] + [[package]] name = "os_str_bytes" version = "6.3.1" @@ -2258,6 +2273,7 @@ dependencies = [ "notify", "num-bigint", "once_cell", + "operational-transform", "path-absolutize", "rayon", "regex", diff --git a/Cargo.toml b/Cargo.toml index 24ef2369c8..3f68be7a36 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,6 +29,7 @@ log = { version = "0.4.17" } notify = { version = "4.0.17" } num-bigint = { version = "0.4.3" } once_cell = { version = "1.16.0" } +operational-transform = "0.6.1" path-absolutize = { version = "3.0.14", features = ["once_cell_cache", "use_unix_paths_on_wasm"] } rayon = { version = "1.5.3" } regex = { version = "1.6.0" } diff --git a/examples/crdt.rs b/examples/crdt.rs new file mode 100644 index 0000000000..a22bf130c6 --- /dev/null +++ b/examples/crdt.rs @@ -0,0 +1,25 @@ +use operational_transform::OperationSeq; + +pub fn main() { + let s = "Union[int, Option[str]]"; + let mut a = OperationSeq::default(); + a.delete("Union[".len() as u64); + a.retain("int".len() as u64); + a.delete(",".len() as u64); + a.insert(" |"); + a.retain(" Option[str]".len() as u64); + a.delete("]".len() as u64); + + let mut b = OperationSeq::default(); + b.retain("Union[int, ".len() as u64); + b.delete("Option[".len() as u64); + b.retain("str".len() as u64); + b.insert(" | None"); + b.delete("]".len() as u64); + b.retain("]".len() as u64); + + let (_, b_prime) = a.transform(&b).unwrap(); + let ab_prime = a.compose(&b_prime).unwrap(); + let s = ab_prime.apply(s).unwrap(); + println!("{}", s); // int | str | None +}