feat: added Operation Factory struct

This commit is contained in:
əlemi 2023-04-10 01:41:22 +02:00
parent ebbca24a99
commit 14e9a1e86e
2 changed files with 57 additions and 0 deletions

View file

@ -1,4 +1,5 @@
pub mod proto;
pub mod opfactory;
pub use tonic;
pub use tokio;

56
src/lib/opfactory.rs Normal file
View file

@ -0,0 +1,56 @@
use operational_transform::{OperationSeq, OTError};
#[derive(Clone)]
pub struct OperationFactory {
content: String,
}
impl OperationFactory {
pub fn new(init: Option<String>) -> 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<OperationSeq, OTError> {
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<OperationSeq, OTError> {
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<OperationSeq, OTError> {
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(())
}
}