From 14e9a1e86eae4615093fcdf4f8a04300b0f5d55d Mon Sep 17 00:00:00 2001 From: alemi Date: Mon, 10 Apr 2023 01:41:22 +0200 Subject: [PATCH] feat: added Operation Factory struct --- src/lib/lib.rs | 1 + src/lib/opfactory.rs | 56 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 src/lib/opfactory.rs diff --git a/src/lib/lib.rs b/src/lib/lib.rs index b7446bb..97146d3 100644 --- a/src/lib/lib.rs +++ b/src/lib/lib.rs @@ -1,4 +1,5 @@ pub mod proto; +pub mod opfactory; pub use tonic; pub use tokio; diff --git a/src/lib/opfactory.rs b/src/lib/opfactory.rs new file mode 100644 index 0000000..2743eff --- /dev/null +++ b/src/lib/opfactory.rs @@ -0,0 +1,56 @@ +use operational_transform::{OperationSeq, OTError}; + + +#[derive(Clone)] +pub struct OperationFactory { + content: String, +} + +impl OperationFactory { + pub fn new(init: Option) -> Self { + OperationFactory { content: init.unwrap_or(String::new()) } + } + + pub fn check(&self, txt: &str) -> bool { + self.content == txt + } + + pub fn replace(&mut self, txt: &str) -> OperationSeq { + let out = OperationSeq::default(); + if self.content == txt { + return out; // nothing to do + } + + todo!() + } + + pub fn insert(&mut self, txt: &str, pos: u64) -> Result { + let mut out = OperationSeq::default(); + out.retain(pos); + out.insert(txt); + self.content = out.apply(&self.content)?; // TODO does aplying mutate the OpSeq itself? + Ok(out) + } + + pub fn delete(&mut self, pos: u64, count: u64) -> Result { + let mut out = OperationSeq::default(); + out.retain(pos - count); + out.delete(count); + self.content = out.apply(&self.content)?; // TODO does aplying mutate the OpSeq itself? + Ok(out) + } + + pub fn cancel(&mut self, pos: u64, count: u64) -> Result { + let mut out = OperationSeq::default(); + out.retain(pos); + out.delete(count); + self.content = out.apply(&self.content)?; // TODO does aplying mutate the OpSeq itself? + Ok(out) + } + + pub fn process(&mut self, op: OperationSeq) -> Result<(), OTError> { + self.content = op.apply(&self.content)?; + Ok(()) + } + +}