mirror of
https://github.com/hexedtech/codemp.git
synced 2024-11-22 07:14:50 +01:00
feat: added Operation Factory struct
This commit is contained in:
parent
ebbca24a99
commit
14e9a1e86e
2 changed files with 57 additions and 0 deletions
|
@ -1,4 +1,5 @@
|
|||
pub mod proto;
|
||||
pub mod opfactory;
|
||||
|
||||
pub use tonic;
|
||||
pub use tokio;
|
||||
|
|
56
src/lib/opfactory.rs
Normal file
56
src/lib/opfactory.rs
Normal 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(())
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue