Add operational transform example

This commit is contained in:
Charlie Marsh 2022-11-08 19:09:55 -05:00
parent 6c17670aa5
commit d4dd7ef91d
3 changed files with 42 additions and 0 deletions

16
Cargo.lock generated
View File

@ -352,6 +352,12 @@ version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3b5ca7a04898ad4bcd41c90c5285445ff5b791899bb1b0abdd2a2aa791211d7" checksum = "e3b5ca7a04898ad4bcd41c90c5285445ff5b791899bb1b0abdd2a2aa791211d7"
[[package]]
name = "bytecount"
version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2c676a478f63e9fa2dd5368a42f28bba0d6c560b775f38583c8bbaa7fcd67c9c"
[[package]] [[package]]
name = "byteorder" name = "byteorder"
version = "1.4.3" version = "1.4.3"
@ -1725,6 +1731,15 @@ version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" 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]] [[package]]
name = "os_str_bytes" name = "os_str_bytes"
version = "6.3.1" version = "6.3.1"
@ -2258,6 +2273,7 @@ dependencies = [
"notify", "notify",
"num-bigint", "num-bigint",
"once_cell", "once_cell",
"operational-transform",
"path-absolutize", "path-absolutize",
"rayon", "rayon",
"regex", "regex",

View File

@ -29,6 +29,7 @@ log = { version = "0.4.17" }
notify = { version = "4.0.17" } notify = { version = "4.0.17" }
num-bigint = { version = "0.4.3" } num-bigint = { version = "0.4.3" }
once_cell = { version = "1.16.0" } 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"] } path-absolutize = { version = "3.0.14", features = ["once_cell_cache", "use_unix_paths_on_wasm"] }
rayon = { version = "1.5.3" } rayon = { version = "1.5.3" }
regex = { version = "1.6.0" } regex = { version = "1.6.0" }

25
examples/crdt.rs Normal file
View File

@ -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
}