chore: update lib-ot documentation again and rename some structs

This commit is contained in:
appflowy
2022-08-02 08:55:33 +08:00
parent a47af561ab
commit 00d81a329f
18 changed files with 101 additions and 91 deletions

View File

@ -1,9 +1,7 @@
use crate::core::delta::{trim, Delta};
use crate::core::operation::{Attributes, PhantomAttributes};
use crate::core::operation::Attributes;
use crate::core::Operation;
pub type PlainTextDeltaBuilder = DeltaBuilder<PhantomAttributes>;
/// A builder for creating new [Delta] objects.
///
/// Note that all edit operations must be sorted; the start point of each
@ -12,8 +10,8 @@ pub type PlainTextDeltaBuilder = DeltaBuilder<PhantomAttributes>;
/// # Examples
///
/// ```
/// use lib_ot::core::PlainTextDeltaBuilder;
/// let delta = PlainTextDeltaBuilder::new()
/// use lib_ot::core::TextDeltaBuilder;
/// let delta = TextDeltaBuilder::new()
/// .insert("AppFlowy")
/// .build();
/// assert_eq!(delta.content_str().unwrap(), "AppFlowy");
@ -74,13 +72,13 @@ where
/// # Examples
///
/// ```
/// use lib_ot::core::{OperationTransform, PlainTextDeltaBuilder};
/// use lib_ot::core::{OperationTransform, TextDeltaBuilder};
///
/// let delta = PlainTextDeltaBuilder::new()
/// let delta = TextDeltaBuilder::new()
/// .insert("AppFlowy...")
/// .build();
///
/// let changeset = PlainTextDeltaBuilder::new()
/// let changeset = TextDeltaBuilder::new()
/// .retain(8)
/// .delete(3)
/// .build();
@ -110,9 +108,9 @@ where
/// # Examples
///
/// ```
/// use lib_ot::core::{OperationTransform, PlainTextDeltaBuilder};
/// use lib_ot::core::{OperationTransform, TextDeltaBuilder};
/// use lib_ot::rich_text::{RichTextAttribute, RichTextDeltaBuilder};
/// let delta = PlainTextDeltaBuilder::new()
/// let delta = TextDeltaBuilder::new()
/// .retain(3)
/// .trim()
/// .build();

View File

@ -29,14 +29,17 @@ where
/// # Examples
///
/// ```
/// use lib_ot::core::{DeltaIterator, Interval, Operation};
/// use lib_ot::core::{DeltaCursor, DeltaIterator, Interval, Operation};
/// use lib_ot::rich_text::RichTextDelta;
/// let mut delta = RichTextDelta::default();
/// let op_1 = Operation::insert("123");
/// let op_2 = Operation::insert("4");
/// delta.add(op_1.clone());
/// delta.add(op_2.clone());
/// assert_eq!(DeltaIterator::from_interval(&delta, Interval::new(0, 3)).ops(), vec![op_1.clone()]);
/// delta.add(Operation::insert("123"));
/// delta.add(Operation::insert("4"));
///
/// let mut cursor = DeltaCursor::new(&delta, Interval::new(0, 3));
/// assert_eq!(cursor.next_iv(), Interval::new(0,3));
/// assert_eq!(cursor.next_with_len(Some(2)).unwrap(), Operation::insert("12"));
/// assert_eq!(cursor.get_next_op().unwrap(), Operation::insert("3"));
/// assert_eq!(cursor.get_next_op(), None);
/// ```
pub fn new(delta: &'a Delta<T>, interval: Interval) -> DeltaCursor<'a, T> {
// debug_assert!(interval.start <= delta.target_len);

View File

@ -1,9 +1,10 @@
use crate::errors::{ErrorBuilder, OTError, OTErrorCode};
use crate::core::delta::{DeltaIterator, MAX_IV_LEN};
use crate::core::flowy_str::OTString;
use crate::core::interval::Interval;
use crate::core::operation::{Attributes, Operation, OperationTransform, PhantomAttributes};
use crate::core::ot_str::OTString;
use crate::core::DeltaBuilder;
use bytes::Bytes;
use serde::de::DeserializeOwned;
use std::{
@ -14,12 +15,15 @@ use std::{
str::FromStr,
};
pub type PlainTextDelta = Delta<PhantomAttributes>;
pub type TextDelta = Delta<PhantomAttributes>;
pub type TextDeltaBuilder = DeltaBuilder<PhantomAttributes>;
/// A [Delta] contains list of operations that consists of 'Retain', 'Delete' and 'Insert' operation.
/// Check out the [Operation] for more details. It describes the document as a sequence of
/// operations.
///
/// You could check [this](https://appflowy.gitbook.io/docs/essential-documentation/contribute-to-appflowy/architecture/backend/delta) out for more information.
///
/// If the [T] supports 'serde', that will enable delta to serialize to JSON or deserialize from
/// a JSON string.
///
@ -176,10 +180,10 @@ where
/// # Examples
///
/// ```
/// use lib_ot::core::PlainTextDeltaBuilder;
/// use lib_ot::core::TextDeltaBuilder;
/// let s = "hello";
/// let delta_a = PlainTextDeltaBuilder::new().insert(s).build();
/// let delta_b = PlainTextDeltaBuilder::new()
/// let delta_a = TextDeltaBuilder::new().insert(s).build();
/// let delta_b = TextDeltaBuilder::new()
/// .retain(s.len())
/// .insert(", AppFlowy")
/// .build();
@ -233,9 +237,9 @@ where
/// # Examples
///
/// ```
/// use lib_ot::core::PlainTextDeltaBuilder;
/// use lib_ot::core::TextDeltaBuilder;
/// let s = "hello world";
/// let delta = PlainTextDeltaBuilder::new().insert(s).build();
/// let delta = TextDeltaBuilder::new().insert(s).build();
/// let invert_delta = delta.invert_str(s);
/// assert_eq!(delta.utf16_base_len, invert_delta.utf16_target_len);
/// assert_eq!(delta.utf16_target_len, invert_delta.utf16_base_len);

View File

@ -7,17 +7,26 @@ use std::ops::{Deref, DerefMut};
pub(crate) const MAX_IV_LEN: usize = i32::MAX as usize;
/// Retain the 'n' characters with the attributes. Use 'retain' instead if you don't
/// need any attributes.
/// [DeltaIterator] is used to iterate over a delta.
/// # Examples
///
/// You could check [this](https://appflowy.gitbook.io/docs/essential-documentation/contribute-to-appflowy/architecture/backend/delta) out for more information.
///
/// ```
/// use lib_ot::rich_text::{RichTextAttribute, RichTextDelta, RichTextDeltaBuilder};
/// use lib_ot::core::{DeltaIterator, Interval, Operation};
/// use lib_ot::rich_text::RichTextDelta;
/// let mut delta = RichTextDelta::default();
/// delta.add(Operation::insert("123"));
/// delta.add(Operation::insert("4"));
/// assert_eq!(
/// DeltaIterator::from_interval(&delta, Interval::new(0, 2)).ops(),
/// vec![Operation::insert("12")]
/// );
///
/// let mut attribute = RichTextAttribute::Bold(true);
/// let delta = RichTextDeltaBuilder::new().retain_with_attributes(7, attribute.into()).build();
///
/// assert_eq!(delta.to_json_str(), r#"[{"retain":7,"attributes":{"bold":true}}]"#);
/// assert_eq!(
/// DeltaIterator::from_interval(&delta, Interval::new(1, 3)).ops(),
/// vec![Operation::insert("23")]
/// );
/// ```
pub struct DeltaIterator<'a, T: Attributes> {
cursor: DeltaCursor<'a, T>,

View File

@ -1,9 +1,9 @@
mod delta;
mod flowy_str;
mod interval;
mod operation;
mod ot_str;
pub use delta::*;
pub use flowy_str::*;
pub use interval::*;
pub use operation::*;
pub use ot_str::*;

View File

@ -1,5 +1,5 @@
use crate::core::flowy_str::OTString;
use crate::core::interval::Interval;
use crate::core::ot_str::OTString;
use crate::errors::OTError;
use serde::{Deserialize, Serialize, __private::Formatter};
use std::fmt::Display;
@ -21,9 +21,9 @@ pub trait OperationTransform {
/// # Examples
///
/// ```
/// use lib_ot::core::{OperationTransform, PlainTextDeltaBuilder};
/// let document = PlainTextDeltaBuilder::new().build();
/// let delta = PlainTextDeltaBuilder::new().insert("abc").build();
/// use lib_ot::core::{OperationTransform, TextDeltaBuilder};
/// let document = TextDeltaBuilder::new().build();
/// let delta = TextDeltaBuilder::new().insert("abc").build();
/// let new_document = document.compose(&delta).unwrap();
/// assert_eq!(new_document.content_str().unwrap(), "abc".to_owned());
/// ```
@ -50,9 +50,9 @@ pub trait OperationTransform {
/// # Examples
///
/// ```
/// use lib_ot::core::{OperationTransform, PlainTextDeltaBuilder};
/// let original_document = PlainTextDeltaBuilder::new().build();
/// let delta = PlainTextDeltaBuilder::new().insert("abc").build();
/// use lib_ot::core::{OperationTransform, TextDeltaBuilder};
/// let original_document = TextDeltaBuilder::new().build();
/// let delta = TextDeltaBuilder::new().insert("abc").build();
///
/// let undo_delta = delta.invert(&original_document);
/// let new_document = original_document.compose(&delta).unwrap();
@ -67,8 +67,8 @@ pub trait OperationTransform {
/// Each operation can carry attributes. For example, the [RichTextAttributes] has a list of key/value attributes.
/// Such as { bold: true, italic: true }.
///
/// Because [Operation] is generic over the T, so you must specify the T. For example, the [PlainTextDelta]. It use
/// use [PhantomAttributes] as the T. [PhantomAttributes] does nothing, just a phantom.
///Because [Operation] is generic over the T, so you must specify the T. For example, the [TextDelta] uses
///[PhantomAttributes] as the T. [PhantomAttributes] does nothing, just a phantom.
///
pub trait Attributes: Default + Display + Eq + PartialEq + Clone + Debug + OperationTransform {
fn is_empty(&self) -> bool {
@ -90,6 +90,8 @@ pub trait Attributes: Default + Display + Eq + PartialEq + Clone + Debug + Opera
/// * Retain
/// * Insert
///
/// You could check [this](https://appflowy.gitbook.io/docs/essential-documentation/contribute-to-appflowy/architecture/backend/delta) out for more information.
///
/// The [T] should support serde if you want to serialize/deserialize the operation
/// to json string. You could check out the operation_serde.rs for more information.
///

View File

@ -1,5 +1,5 @@
use crate::core::flowy_str::OTString;
use crate::core::operation::{Attributes, Insert, Operation, Retain};
use crate::core::ot_str::OTString;
use serde::{
de,
de::{MapAccess, SeqAccess, Visitor},

View File

@ -86,8 +86,8 @@ impl OTString {
/// assert_eq!(iter.skip(OTString::from("ab一二").utf16_len()).next().unwrap(), "👋".to_string());
/// ```
#[allow(dead_code)]
pub fn utf16_code_point_iter(&self) -> FlowyUtf16CodePointIterator {
FlowyUtf16CodePointIterator::new(self, 0)
pub fn utf16_code_point_iter(&self) -> OTUtf16CodePointIterator {
OTUtf16CodePointIterator::new(self, 0)
}
}
@ -218,21 +218,21 @@ impl<'a> Iterator for Utf16CodeUnitIterator<'a> {
}
}
pub struct FlowyUtf16CodePointIterator<'a> {
pub struct OTUtf16CodePointIterator<'a> {
s: &'a OTString,
offset: usize,
}
impl<'a> FlowyUtf16CodePointIterator<'a> {
impl<'a> OTUtf16CodePointIterator<'a> {
pub fn new(s: &'a OTString, offset: usize) -> Self {
FlowyUtf16CodePointIterator { s, offset }
OTUtf16CodePointIterator { s, offset }
}
}
use crate::core::interval::Interval;
use std::str;
impl<'a> Iterator for FlowyUtf16CodePointIterator<'a> {
impl<'a> Iterator for OTUtf16CodePointIterator<'a> {
type Item = String;
fn next(&mut self) -> Option<Self::Item> {
@ -278,8 +278,8 @@ pub fn len_utf8_from_first_byte(b: u8) -> usize {
#[cfg(test)]
mod tests {
use crate::core::flowy_str::OTString;
use crate::core::interval::Interval;
use crate::core::ot_str::OTString;
#[test]
fn flowy_str_code_unit() {