From ed151e2213253832a17906e31898839b2f5d4b49 Mon Sep 17 00:00:00 2001 From: alemi Date: Sun, 10 Sep 2023 04:21:56 +0200 Subject: [PATCH] fix: docs, tests, leftovers --- src/api/controller.rs | 3 +- src/api/factory.rs | 75 ++++++++++++++++++++++--------------------- src/client.rs | 2 +- src/lib.rs | 8 ++--- 4 files changed, 45 insertions(+), 43 deletions(-) diff --git a/src/api/controller.rs b/src/api/controller.rs index d844306..07a0d8f 100644 --- a/src/api/controller.rs +++ b/src/api/controller.rs @@ -5,7 +5,6 @@ use crate::Result; use std::sync::Arc; -use tokio::runtime::Runtime; #[async_trait::async_trait] pub(crate) trait ControllerWorker { @@ -60,7 +59,7 @@ pub trait Controller : Sized + Send + Sync { fn try_recv(&self) -> Result>; /// sync variant of [Self::recv], blocking invoking thread - fn blocking_recv(&self, rt: &Runtime) -> Result { + fn blocking_recv(&self, rt: &tokio::runtime::Handle) -> Result { rt.block_on(self.recv()) } diff --git a/src/api/factory.rs b/src/api/factory.rs index a41b383..6fec49e 100644 --- a/src/api/factory.rs +++ b/src/api/factory.rs @@ -40,64 +40,67 @@ pub fn op_effective_range(op: &OperationSeq) -> Range { /// ### examples /// /// ```rust -/// use codemp::buffer::factory::{OperationFactory, SimpleOperationFactory}; +/// use codemp::api::OperationFactory; /// -/// let mut factory = SimpleOperationFactory::from(""); -/// let op = factory.insert("asd", 0); -/// factory.update(op)?; -/// assert_eq!(factory.content(), "asd"); +/// let mut factory = String::new(); +/// let operation = factory.ins("asd", 0); +/// factory = operation.apply(&factory)?; +/// assert_eq!(factory, "asd"); /// # Ok::<(), codemp::ot::OTError>(()) /// ``` /// -/// use [OperationFactory::insert] to add new characters at a specific index +/// use [OperationFactory::ins] to add new characters at a specific index /// /// ```rust -/// # use codemp::buffer::factory::{OperationFactory, SimpleOperationFactory}; -/// # let mut factory = SimpleOperationFactory::from("asd"); -/// factory.update(factory.insert(" dsa", 3))?; -/// assert_eq!(factory.content(), "asd dsa"); +/// # use codemp::api::OperationFactory; +/// # let mut factory = String::from("asd"); +/// factory = factory.ins(" dsa", 3).apply(&factory)?; +/// assert_eq!(factory, "asd dsa"); /// # Ok::<(), codemp::ot::OTError>(()) /// ``` /// -/// use [OperationFactory::delta] to arbitrarily change text at any position +/// use [OperationFactory::diff] to arbitrarily change text at any position /// /// ```rust -/// # use codemp::buffer::factory::{OperationFactory, SimpleOperationFactory}; -/// # let mut factory = SimpleOperationFactory::from("asd dsa"); -/// let op = factory.delta(2, " xxx ", 5).expect("replaced region is equal to origin"); -/// factory.update(op)?; -/// assert_eq!(factory.content(), "as xxx sa"); +/// # use codemp::api::OperationFactory; +/// # let mut factory = String::from("asd dsa"); +/// factory = factory +/// .diff(2, " xxx ", 5) +/// .expect("replaced region is equal to origin") +/// .apply(&factory)?; +/// assert_eq!(factory, "as xxx sa"); /// # Ok::<(), codemp::ot::OTError>(()) /// ``` /// -/// use [OperationFactory::delete] to remove characters from given index +/// use [OperationFactory::del] to remove characters from given index /// /// ```rust -/// # use codemp::buffer::factory::{OperationFactory, SimpleOperationFactory}; -/// # let mut factory = SimpleOperationFactory::from("as xxx sa"); -/// factory.update(factory.delete(2, 5))?; -/// assert_eq!(factory.content(), "assa"); +/// # use codemp::api::OperationFactory; +/// # let mut factory = String::from("as xxx sa"); +/// factory = factory.del(2, 5).apply(&factory)?; +/// assert_eq!(factory, "assa"); /// # Ok::<(), codemp::ot::OTError>(()) /// ``` /// /// use [OperationFactory::replace] to completely replace buffer content /// /// ```rust -/// # use codemp::buffer::factory::{OperationFactory, SimpleOperationFactory}; -/// # let mut factory = SimpleOperationFactory::from("assa"); -/// let op = factory.replace("from scratch").expect("replace is equal to origin"); -/// factory.update(op)?; -/// assert_eq!(factory.content(), "from scratch"); +/// # use codemp::api::OperationFactory; +/// # let mut factory = String::from("assa"); +/// factory = factory.replace("from scratch") +/// .expect("replace is equal to origin") +/// .apply(&factory)?; +/// assert_eq!(factory, "from scratch"); /// # Ok::<(), codemp::ot::OTError>(()) /// ``` /// -/// use [OperationFactory::cancel] to remove characters at index, but backwards +/// use [OperationFactory::canc] to remove characters at index, but backwards /// /// ```rust -/// # use codemp::buffer::factory::{OperationFactory, SimpleOperationFactory}; -/// # let mut factory = SimpleOperationFactory::from("from scratch"); -/// factory.update(factory.cancel(12, 8))?; -/// assert_eq!(factory.content(), "from"); +/// # use codemp::api::OperationFactory; +/// # let mut factory = String::from("from scratch"); +/// factory = factory.canc(12, 8).apply(&factory)?; +/// assert_eq!(factory, "from"); /// # Ok::<(), codemp::ot::OTError>(()) /// ``` pub trait OperationFactory { @@ -106,11 +109,11 @@ pub trait OperationFactory { /// completely replace the buffer with given text fn replace(&self, txt: &str) -> Option { - self.delta(0, txt, self.content().len()) + self.diff(0, txt, self.content().len()) } /// transform buffer in range [start..end] with given text - fn delta(&self, start: usize, txt: &str, end: usize) -> Option { + fn diff(&self, start: usize, txt: &str, end: usize) -> Option { let mut out = OperationSeq::default(); let content = self.content(); let tail_skip = content.len() - end; // TODO len is number of bytes, not chars @@ -139,7 +142,7 @@ pub trait OperationFactory { } /// insert given chars at target position - fn insert(&self, txt: &str, pos: u64) -> OperationSeq { + fn ins(&self, txt: &str, pos: u64) -> OperationSeq { let mut out = OperationSeq::default(); let total = self.content().len() as u64; out.retain(pos); @@ -149,7 +152,7 @@ pub trait OperationFactory { } /// delete n characters forward at given position - fn delete(&self, pos: u64, count: u64) -> OperationSeq { + fn del(&self, pos: u64, count: u64) -> OperationSeq { let mut out = OperationSeq::default(); let len = self.content().len() as u64; out.retain(pos); @@ -159,7 +162,7 @@ pub trait OperationFactory { } /// delete n characters backwards at given position - fn cancel(&self, pos: u64, count: u64) -> OperationSeq { + fn canc(&self, pos: u64, count: u64) -> OperationSeq { let mut out = OperationSeq::default(); let len = self.content().len() as u64; out.retain(pos - count); diff --git a/src/client.rs b/src/client.rs index 1947856..7038dde 100644 --- a/src/client.rs +++ b/src/client.rs @@ -124,7 +124,7 @@ impl Client { /// or [crate::api::Controller::recv] for text events using its [crate::buffer::Controller]. /// to generate operation sequences use the [crate::api::OperationFactory] /// methods, which are implemented on [crate::buffer::Controller], such as - /// [crate::api::OperationFactory::delta]. + /// [crate::api::OperationFactory::diff]. pub async fn attach(&mut self, path: &str) -> Result, Error> { if let Some(workspace) = &mut self.workspace { let mut client = self.client.buffer.clone(); diff --git a/src/lib.rs b/src/lib.rs index d3afdd3..23e87a9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -40,8 +40,7 @@ //! [instance::a_sync::Instance] //! //! ```rust,no_run -//! use codemp::api::Controller; -//! use codemp::buffer::OperationFactory; +//! use codemp::api::{Controller, OperationFactory}; //! # use codemp::instance::a_sync::Instance; //! //! # async fn async_example() -> codemp::Result<()> { @@ -63,8 +62,9 @@ //! // attach to a new buffer and execute operations on it //! session.create("test.txt", None).await?; // create new buffer //! let buffer = session.attach("test.txt").await?; // attach to it -//! buffer.send(buffer.insert("hello", 0))?; // insert some text -//! if let Some(operation) = buffer.delta(4, "o world", 5) { +//! let text = buffer.content(); // any string can be used as operation factory +//! buffer.send(text.ins("hello", 0))?; // insert some text +//! if let Some(operation) = text.diff(4, "o world", 5) { //! buffer.send(operation)?; // replace with precision, if valid //! } //! assert_eq!(buffer.content(), "hello world");