mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
chore: update lib ot tests
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
use crate::core::delta::{trim, Delta};
|
||||
use crate::core::operation::{Attributes, PhantomAttributes};
|
||||
use crate::core::Operation;
|
||||
|
||||
pub type PlainTextDeltaBuilder = DeltaBuilder<PhantomAttributes>;
|
||||
|
||||
@ -7,6 +8,16 @@ pub type PlainTextDeltaBuilder = DeltaBuilder<PhantomAttributes>;
|
||||
///
|
||||
/// Note that all edit operations must be sorted; the start point of each
|
||||
/// interval must be no less than the end point of the previous one.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use lib_ot::core::PlainTextDeltaBuilder;
|
||||
/// let delta = PlainTextDeltaBuilder::new()
|
||||
/// .insert("AppFlowy")
|
||||
/// .build();
|
||||
/// assert_eq!(delta.content_str().unwrap(), "AppFlowy");
|
||||
/// ```
|
||||
pub struct DeltaBuilder<T: Attributes> {
|
||||
delta: Delta<T>,
|
||||
}
|
||||
@ -28,8 +39,26 @@ where
|
||||
DeltaBuilder::default()
|
||||
}
|
||||
|
||||
pub fn from_operations(operations: Vec<Operation<T>>) -> Delta<T> {
|
||||
let mut delta = DeltaBuilder::default().build();
|
||||
operations.into_iter().for_each(|operation| {
|
||||
delta.add(operation);
|
||||
});
|
||||
delta
|
||||
}
|
||||
|
||||
/// Retain the 'n' characters with the attributes. Use 'retain' instead if you don't
|
||||
/// need any attributes.
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use lib_ot::rich_text::{RichTextAttribute, RichTextDelta, RichTextDeltaBuilder};
|
||||
///
|
||||
/// 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}}]"#);
|
||||
/// ```
|
||||
pub fn retain_with_attributes(mut self, n: usize, attrs: T) -> Self {
|
||||
self.delta.retain(n, attrs);
|
||||
self
|
||||
@ -41,6 +70,24 @@ where
|
||||
}
|
||||
|
||||
/// Deletes the given interval. Panics if interval is not properly sorted.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use lib_ot::core::{OperationTransform, PlainTextDeltaBuilder};
|
||||
///
|
||||
/// let delta = PlainTextDeltaBuilder::new()
|
||||
/// .insert("AppFlowy...")
|
||||
/// .build();
|
||||
///
|
||||
/// let changeset = PlainTextDeltaBuilder::new()
|
||||
/// .retain(8)
|
||||
/// .delete(3)
|
||||
/// .build();
|
||||
///
|
||||
/// let new_delta = delta.compose(&changeset).unwrap();
|
||||
/// assert_eq!(new_delta.content_str().unwrap(), "AppFlowy");
|
||||
/// ```
|
||||
pub fn delete(mut self, n: usize) -> Self {
|
||||
self.delta.delete(n);
|
||||
self
|
||||
@ -58,6 +105,25 @@ where
|
||||
self
|
||||
}
|
||||
|
||||
/// Removes trailing retain operation with empty attributes
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use lib_ot::core::{OperationTransform, PlainTextDeltaBuilder};
|
||||
/// use lib_ot::rich_text::{RichTextAttribute, RichTextDeltaBuilder};
|
||||
/// let delta = PlainTextDeltaBuilder::new()
|
||||
/// .retain(3)
|
||||
/// .trim()
|
||||
/// .build();
|
||||
/// assert_eq!(delta.ops.len(), 0);
|
||||
///
|
||||
/// let delta = RichTextDeltaBuilder::new()
|
||||
/// .retain_with_attributes(3, RichTextAttribute::Bold(true).into())
|
||||
/// .trim()
|
||||
/// .build();
|
||||
/// assert_eq!(delta.ops.len(), 1);
|
||||
/// ```
|
||||
pub fn trim(mut self) -> Self {
|
||||
trim(&mut self.delta);
|
||||
self
|
||||
|
@ -3,7 +3,7 @@ use crate::errors::{ErrorBuilder, OTError, OTErrorCode};
|
||||
use crate::core::delta::{DeltaIterator, MAX_IV_LEN};
|
||||
use crate::core::flowy_str::FlowyStr;
|
||||
use crate::core::interval::Interval;
|
||||
use crate::core::operation::{Attributes, Operation, OperationBuilder, OperationTransformable, PhantomAttributes};
|
||||
use crate::core::operation::{Attributes, Operation, OperationBuilder, OperationTransform, PhantomAttributes};
|
||||
use bytes::Bytes;
|
||||
use serde::de::DeserializeOwned;
|
||||
use std::{
|
||||
@ -123,7 +123,7 @@ where
|
||||
return;
|
||||
}
|
||||
|
||||
self.utf16_target_len += s.utf16_size();
|
||||
self.utf16_target_len += s.utf16_len();
|
||||
let new_last = match self.ops.as_mut_slice() {
|
||||
[.., Operation::<T>::Insert(insert)] => {
|
||||
//
|
||||
@ -191,12 +191,12 @@ where
|
||||
/// ```
|
||||
pub fn apply(&self, applied_str: &str) -> Result<String, OTError> {
|
||||
let applied_str: FlowyStr = applied_str.into();
|
||||
if applied_str.utf16_size() != self.utf16_base_len {
|
||||
if applied_str.utf16_len() != self.utf16_base_len {
|
||||
return Err(ErrorBuilder::new(OTErrorCode::IncompatibleLength)
|
||||
.msg(format!(
|
||||
"Expected: {}, but received: {}",
|
||||
self.utf16_base_len,
|
||||
applied_str.utf16_size()
|
||||
applied_str.utf16_len()
|
||||
))
|
||||
.build());
|
||||
}
|
||||
@ -289,7 +289,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> OperationTransformable for Delta<T>
|
||||
impl<T> OperationTransform for Delta<T>
|
||||
where
|
||||
T: Attributes,
|
||||
{
|
||||
@ -568,6 +568,17 @@ impl<T> Delta<T>
|
||||
where
|
||||
T: Attributes + DeserializeOwned,
|
||||
{
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use lib_ot::core::DeltaBuilder;
|
||||
/// use lib_ot::rich_text::{RichTextDelta};
|
||||
/// let json = r#"[
|
||||
/// {"retain":7,"attributes":{"bold":null}}
|
||||
/// ]"#;
|
||||
/// let delta = RichTextDelta::from_json_str(json).unwrap();
|
||||
/// assert_eq!(delta.to_json_str(), r#"[{"retain":7,"attributes":{"bold":""}}]"#);
|
||||
/// ```
|
||||
pub fn from_json_str(json: &str) -> Result<Self, OTError> {
|
||||
let delta = serde_json::from_str(json).map_err(|e| {
|
||||
tracing::trace!("Deserialize failed: {:?}", e);
|
||||
|
@ -7,6 +7,18 @@ 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.
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use lib_ot::rich_text::{RichTextAttribute, RichTextDelta, RichTextDeltaBuilder};
|
||||
///
|
||||
/// 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}}]"#);
|
||||
/// ```
|
||||
pub struct DeltaIterator<'a, T: Attributes> {
|
||||
cursor: DeltaCursor<'a, T>,
|
||||
}
|
||||
|
@ -5,8 +5,24 @@ use std::{fmt, fmt::Formatter};
|
||||
pub struct FlowyStr(pub String);
|
||||
|
||||
impl FlowyStr {
|
||||
// https://stackoverflow.com/questions/2241348/what-is-unicode-utf-8-utf-16
|
||||
pub fn utf16_size(&self) -> usize {
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `delta`: The delta you want to iterate over.
|
||||
/// * `interval`: The range for the cursor movement.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use lib_ot::core::FlowyStr;
|
||||
/// let utf16_len = FlowyStr::from("👋").utf16_len();
|
||||
/// assert_eq!(utf16_len, 2);
|
||||
/// let bytes_len = String::from("👋").len();
|
||||
/// assert_eq!(bytes_len, 4);
|
||||
///
|
||||
/// ```
|
||||
/// https://stackoverflow.com/questions/2241348/what-is-unicode-utf-8-utf-16
|
||||
pub fn utf16_len(&self) -> usize {
|
||||
count_utf16_code_units(&self.0)
|
||||
}
|
||||
|
||||
@ -231,7 +247,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn flowy_str_code_unit() {
|
||||
let size = FlowyStr::from("👋").utf16_size();
|
||||
let size = FlowyStr::from("👋").utf16_len();
|
||||
assert_eq!(size, 2);
|
||||
|
||||
let s: FlowyStr = "👋 \n👋".into();
|
||||
@ -251,7 +267,7 @@ mod tests {
|
||||
#[test]
|
||||
fn flowy_str_sub_str_in_chinese() {
|
||||
let s: FlowyStr = "你好\n😁".into();
|
||||
let size = s.utf16_size();
|
||||
let size = s.utf16_len();
|
||||
assert_eq!(size, 5);
|
||||
|
||||
let output1 = s.sub_str(Interval::new(0, 2)).unwrap();
|
||||
@ -265,7 +281,7 @@ mod tests {
|
||||
#[test]
|
||||
fn flowy_str_sub_str_in_chinese2() {
|
||||
let s: FlowyStr = "😁 \n".into();
|
||||
let size = s.utf16_size();
|
||||
let size = s.utf16_len();
|
||||
assert_eq!(size, 4);
|
||||
|
||||
let output1 = s.sub_str(Interval::new(0, 3)).unwrap();
|
||||
@ -277,7 +293,7 @@ mod tests {
|
||||
#[test]
|
||||
fn flowy_str_sub_str_in_english() {
|
||||
let s: FlowyStr = "ab".into();
|
||||
let size = s.utf16_size();
|
||||
let size = s.utf16_len();
|
||||
assert_eq!(size, 2);
|
||||
|
||||
let output = s.sub_str(Interval::new(0, 2)).unwrap();
|
||||
|
@ -5,31 +5,43 @@ pub type RichTextOpBuilder = OperationBuilder<RichTextAttributes>;
|
||||
pub type PlainTextOpBuilder = OperationBuilder<PhantomAttributes>;
|
||||
|
||||
pub struct OperationBuilder<T: Attributes> {
|
||||
ty: Operation<T>,
|
||||
attrs: T,
|
||||
operations: Vec<Operation<T>>,
|
||||
}
|
||||
|
||||
impl<T> OperationBuilder<T>
|
||||
where
|
||||
T: Attributes,
|
||||
{
|
||||
pub fn new(ty: Operation<T>) -> OperationBuilder<T> {
|
||||
OperationBuilder {
|
||||
ty,
|
||||
attrs: T::default(),
|
||||
pub fn new() -> OperationBuilder<T> {
|
||||
OperationBuilder { operations: vec![] }
|
||||
}
|
||||
|
||||
pub fn retain(mut self, n: usize) -> OperationBuilder<T> {
|
||||
let mut retain = Operation::Retain(n.into());
|
||||
|
||||
if let Some(attributes) = attributes {
|
||||
if let Operation::Retain(r) = &mut retain {
|
||||
r.attributes = attributes;
|
||||
}
|
||||
}
|
||||
self.operations.push(retain);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn retain(n: usize) -> OperationBuilder<T> {
|
||||
OperationBuilder::new(Operation::Retain(n.into()))
|
||||
pub fn delete(mut self, n: usize) -> OperationBuilder<T> {
|
||||
self.operations.push(Operation::Delete(n));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn delete(n: usize) -> OperationBuilder<T> {
|
||||
OperationBuilder::new(Operation::Delete(n))
|
||||
}
|
||||
|
||||
pub fn insert(s: &str) -> OperationBuilder<T> {
|
||||
OperationBuilder::new(Operation::Insert(s.into()))
|
||||
pub fn insert(mut self, s: &str, attributes: Option<T>) -> OperationBuilder<T> {
|
||||
let mut insert = Operation::Insert(s.into());
|
||||
if let Some(attributes) = attributes {
|
||||
if let Operation::Retain(i) = &mut insert {
|
||||
i.attributes = attributes;
|
||||
}
|
||||
}
|
||||
self.operations.push(insert);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn attributes(mut self, attrs: T) -> OperationBuilder<T> {
|
||||
@ -37,13 +49,7 @@ where
|
||||
self
|
||||
}
|
||||
|
||||
pub fn build(self) -> Operation<T> {
|
||||
let mut operation = self.ty;
|
||||
match &mut operation {
|
||||
Operation::Delete(_) => {}
|
||||
Operation::Retain(retain) => retain.attributes = self.attrs,
|
||||
Operation::Insert(insert) => insert.attributes = self.attrs,
|
||||
}
|
||||
operation
|
||||
pub fn build(self) -> Vec<Operation<T>> {
|
||||
self.operations
|
||||
}
|
||||
}
|
||||
|
@ -11,7 +11,7 @@ use std::{
|
||||
ops::{Deref, DerefMut},
|
||||
};
|
||||
|
||||
pub trait OperationTransformable {
|
||||
pub trait OperationTransform {
|
||||
/// Merges the operation with `other` into one operation while preserving
|
||||
/// the changes of both.
|
||||
///
|
||||
@ -22,7 +22,7 @@ pub trait OperationTransformable {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use lib_ot::core::{OperationTransformable, PlainTextDeltaBuilder};
|
||||
/// use lib_ot::core::{OperationTransform, PlainTextDeltaBuilder};
|
||||
/// let document = PlainTextDeltaBuilder::new().build();
|
||||
/// let delta = PlainTextDeltaBuilder::new().insert("abc").build();
|
||||
/// let new_document = document.compose(&delta).unwrap();
|
||||
@ -51,7 +51,7 @@ pub trait OperationTransformable {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use lib_ot::core::{OperationTransformable, PlainTextDeltaBuilder};
|
||||
/// use lib_ot::core::{OperationTransform, PlainTextDeltaBuilder};
|
||||
/// let original_document = PlainTextDeltaBuilder::new().build();
|
||||
/// let delta = PlainTextDeltaBuilder::new().insert("abc").build();
|
||||
///
|
||||
@ -71,7 +71,7 @@ pub trait OperationTransformable {
|
||||
/// 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.
|
||||
///
|
||||
pub trait Attributes: Default + Display + Eq + PartialEq + Clone + Debug + OperationTransformable {
|
||||
pub trait Attributes: Default + Display + Eq + PartialEq + Clone + Debug + OperationTransform {
|
||||
fn is_empty(&self) -> bool {
|
||||
true
|
||||
}
|
||||
@ -360,7 +360,7 @@ where
|
||||
T: Attributes,
|
||||
{
|
||||
pub fn utf16_size(&self) -> usize {
|
||||
self.s.utf16_size()
|
||||
self.s.utf16_len()
|
||||
}
|
||||
|
||||
pub fn merge_or_new_op(&mut self, s: &str, attributes: T) -> Option<Operation<T>> {
|
||||
@ -420,7 +420,7 @@ impl fmt::Display for PhantomAttributes {
|
||||
|
||||
impl Attributes for PhantomAttributes {}
|
||||
|
||||
impl OperationTransformable for PhantomAttributes {
|
||||
impl OperationTransform for PhantomAttributes {
|
||||
fn compose(&self, _other: &Self) -> Result<Self, OTError> {
|
||||
Ok(self.clone())
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
#![allow(non_snake_case)]
|
||||
use crate::core::{Attributes, Operation, OperationTransformable};
|
||||
use crate::core::{Attributes, Operation, OperationTransform};
|
||||
use crate::{block_attribute, errors::OTError, ignore_attribute, inline_attribute, list_attribute};
|
||||
use lazy_static::lazy_static;
|
||||
use std::{
|
||||
@ -122,7 +122,7 @@ impl Attributes for RichTextAttributes {
|
||||
}
|
||||
}
|
||||
|
||||
impl OperationTransformable for RichTextAttributes {
|
||||
impl OperationTransform for RichTextAttributes {
|
||||
fn compose(&self, other: &Self) -> Result<Self, OTError>
|
||||
where
|
||||
Self: Sized,
|
||||
|
Reference in New Issue
Block a user