484 lines
13 KiB
Rust
Raw Normal View History

use crate::core::interval::Interval;
use crate::core::ot_str::OTString;
use crate::errors::OTError;
2022-01-16 13:44:14 +08:00
use serde::{Deserialize, Serialize, __private::Formatter};
use std::fmt::Display;
2021-08-05 20:05:40 +08:00
use std::{
2021-08-06 22:25:09 +08:00
cmp::min,
2021-08-05 20:05:40 +08:00
fmt,
2021-12-07 10:39:01 +08:00
fmt::Debug,
2021-08-05 20:05:40 +08:00
ops::{Deref, DerefMut},
};
2022-08-01 09:31:40 +08:00
pub trait OperationTransform {
/// Merges the operation with `other` into one operation while preserving
/// the changes of both.
///
/// # Arguments
///
/// * `other`: The delta gonna to merge.
///
/// # Examples
///
/// ```
/// 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());
/// ```
fn compose(&self, other: &Self) -> Result<Self, OTError>
where
Self: Sized;
/// Transforms two operations a and b that happened concurrently and
/// produces two operations a' and b'.
/// (a', b') = a.transform(b)
/// a.compose(b') = b.compose(a')
///
fn transform(&self, other: &Self) -> Result<(Self, Self), OTError>
where
Self: Sized;
2022-07-25 18:34:26 -04:00
/// Returns the invert delta from the other. It can be used to do the undo operation.
///
/// # Arguments
///
/// * `other`: Generate the undo delta for [Other]. [Other] can compose the undo delta to return
/// to the previous state.
///
/// # Examples
///
/// ```
/// 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();
/// let document = new_document.compose(&undo_delta).unwrap();
///
/// assert_eq!(original_document, document);
///
/// ```
fn invert(&self, other: &Self) -> Self;
}
2021-12-07 10:39:01 +08:00
/// 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 [TextDelta] uses
///[PhantomAttributes] as the T. [PhantomAttributes] does nothing, just a phantom.
///
2022-08-01 09:31:40 +08:00
pub trait Attributes: Default + Display + Eq + PartialEq + Clone + Debug + OperationTransform {
fn is_empty(&self) -> bool {
true
}
/// Remove the empty attribute which value is None.
fn remove_empty(&mut self) {
// Do nothing
}
2021-12-07 10:39:01 +08:00
fn extend_other(&mut self, _other: Self) {
// Do nothing
}
2021-12-07 10:39:01 +08:00
}
/// [Operation] consists of three types.
/// * Delete
/// * 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.
///
2021-09-22 14:42:14 +08:00
#[derive(Debug, Clone, Eq, PartialEq)]
2021-12-07 10:39:01 +08:00
pub enum Operation<T: Attributes> {
2021-08-06 23:06:27 +08:00
Delete(usize),
2021-12-07 10:39:01 +08:00
Retain(Retain<T>),
Insert(Insert<T>),
2021-08-05 20:05:40 +08:00
}
2021-12-07 10:39:01 +08:00
impl<T> Operation<T>
where
T: Attributes,
{
pub fn delete(n: usize) -> Self {
Self::Delete(n)
}
/// Create a [Retain] operation with the given attributes
pub fn retain_with_attributes(n: usize, attributes: T) -> Self {
Self::Retain(Retain { n, attributes })
}
/// Create a [Retain] operation without attributes
pub fn retain(n: usize) -> Self {
Self::Retain(Retain {
n,
attributes: T::default(),
})
}
/// Create a [Insert] operation with the given attributes
pub fn insert_with_attributes(s: &str, attributes: T) -> Self {
Self::Insert(Insert {
2022-08-01 11:32:25 +08:00
s: OTString::from(s),
attributes,
})
}
/// Create a [Insert] operation without attributes
pub fn insert(s: &str) -> Self {
Self::Insert(Insert {
2022-08-01 11:32:25 +08:00
s: OTString::from(s),
attributes: T::default(),
})
}
/// Return the String if the operation is [Insert] operation, otherwise return the empty string.
2021-08-11 23:34:35 +08:00
pub fn get_data(&self) -> &str {
2021-08-05 20:05:40 +08:00
match self {
2021-08-11 23:34:35 +08:00
Operation::Delete(_) => "",
Operation::Retain(_) => "",
Operation::Insert(insert) => &insert.s,
2021-08-05 20:05:40 +08:00
}
}
2021-12-07 10:39:01 +08:00
pub fn get_attributes(&self) -> T {
2021-08-05 20:05:40 +08:00
match self {
2021-12-07 10:39:01 +08:00
Operation::Delete(_) => T::default(),
2021-08-05 20:05:40 +08:00
Operation::Retain(retain) => retain.attributes.clone(),
Operation::Insert(insert) => insert.attributes.clone(),
}
}
2021-12-07 10:39:01 +08:00
pub fn set_attributes(&mut self, attributes: T) {
2021-08-05 20:05:40 +08:00
match self {
2021-08-15 22:08:03 +08:00
Operation::Delete(_) => log::error!("Delete should not contains attributes"),
Operation::Retain(retain) => retain.attributes = attributes,
Operation::Insert(insert) => insert.attributes = attributes,
2021-08-05 20:05:40 +08:00
}
}
2022-01-23 12:14:00 +08:00
pub fn has_attribute(&self) -> bool {
!self.get_attributes().is_empty()
}
2021-08-05 20:05:40 +08:00
2021-08-15 22:08:03 +08:00
pub fn len(&self) -> usize {
2021-11-27 19:19:41 +08:00
match self {
2021-08-05 20:05:40 +08:00
Operation::Delete(n) => *n,
Operation::Retain(r) => r.n,
Operation::Insert(i) => i.utf16_size(),
2021-11-27 19:19:41 +08:00
}
2021-08-05 20:05:40 +08:00
}
2022-01-23 12:14:00 +08:00
pub fn is_empty(&self) -> bool {
self.len() == 0
}
2021-08-06 22:25:09 +08:00
2021-08-14 16:44:39 +08:00
#[allow(dead_code)]
2021-12-07 10:39:01 +08:00
pub fn split(&self, index: usize) -> (Option<Operation<T>>, Option<Operation<T>>) {
2021-08-15 22:08:03 +08:00
debug_assert!(index < self.len());
2021-08-15 00:05:18 +08:00
let left;
let right;
2021-08-13 14:13:31 +08:00
match self {
Operation::Delete(n) => {
left = Some(Operation::<T>::delete(index));
right = Some(Operation::<T>::delete(*n - index));
2022-01-23 12:14:00 +08:00
}
2021-08-13 14:13:31 +08:00
Operation::Retain(retain) => {
left = Some(Operation::<T>::delete(index));
right = Some(Operation::<T>::delete(retain.n - index));
2022-01-23 12:14:00 +08:00
}
2021-08-13 14:13:31 +08:00
Operation::Insert(insert) => {
let attributes = self.get_attributes();
left = Some(Operation::<T>::insert_with_attributes(
&insert.s[0..index],
attributes.clone(),
));
right = Some(Operation::<T>::insert_with_attributes(
&insert.s[index..insert.utf16_size()],
attributes,
));
2022-01-23 12:14:00 +08:00
}
2021-08-13 14:13:31 +08:00
}
(left, right)
}
/// Returns an operation with the specified width.
/// # Arguments
///
/// * `interval`: Specify the shrink width of the operation.
///
/// # Examples
///
/// ```
/// use lib_ot::core::{Interval, Operation, PhantomAttributes};
/// let operation = Operation::<PhantomAttributes>::insert("1234");
///
/// let op1 = operation.shrink(Interval::new(0,3)).unwrap();
/// assert_eq!(op1 , Operation::insert("123"));
///
/// let op2= operation.shrink(Interval::new(3,4)).unwrap();
/// assert_eq!(op2, Operation::insert("4"));
/// ```
2021-12-07 10:39:01 +08:00
pub fn shrink(&self, interval: Interval) -> Option<Operation<T>> {
2021-08-08 22:29:16 +08:00
let op = match self {
Operation::Delete(n) => Operation::delete(min(*n, interval.size())),
Operation::Retain(retain) => {
Operation::retain_with_attributes(min(retain.n, interval.size()), retain.attributes.clone())
}
2021-08-06 22:25:09 +08:00
Operation::Insert(insert) => {
if interval.start > insert.utf16_size() {
Operation::insert("")
2021-08-08 22:29:16 +08:00
} else {
let s = insert.s.sub_str(interval).unwrap_or_else(|| "".to_owned());
Operation::insert_with_attributes(&s, insert.attributes.clone())
2021-08-06 22:25:09 +08:00
}
2022-01-23 12:14:00 +08:00
}
2021-08-08 22:29:16 +08:00
};
match op.is_empty() {
true => None,
false => Some(op),
2021-08-06 22:25:09 +08:00
}
}
2021-08-13 18:16:52 +08:00
pub fn is_delete(&self) -> bool {
if let Operation::Delete(_) = self {
return true;
}
false
}
pub fn is_insert(&self) -> bool {
if let Operation::Insert(_) = self {
return true;
}
false
}
pub fn is_retain(&self) -> bool {
if let Operation::Retain(_) = self {
return true;
}
false
}
2021-11-12 21:44:26 +08:00
pub fn is_plain(&self) -> bool {
match self {
Operation::Delete(_) => true,
Operation::Retain(retain) => retain.is_plain(),
Operation::Insert(insert) => insert.is_plain(),
}
}
2021-08-05 20:05:40 +08:00
}
2021-12-07 10:39:01 +08:00
impl<T> fmt::Display for Operation<T>
where
T: Attributes,
{
2021-08-05 20:05:40 +08:00
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2021-08-10 17:08:47 +08:00
f.write_str("{")?;
2021-08-05 20:05:40 +08:00
match self {
Operation::Delete(n) => {
f.write_fmt(format_args!("delete: {}", n))?;
2022-01-23 12:14:00 +08:00
}
2021-08-05 20:05:40 +08:00
Operation::Retain(r) => {
f.write_fmt(format_args!("{}", r))?;
2022-01-23 12:14:00 +08:00
}
2021-08-05 20:05:40 +08:00
Operation::Insert(i) => {
f.write_fmt(format_args!("{}", i))?;
2022-01-23 12:14:00 +08:00
}
2021-08-05 20:05:40 +08:00
}
2021-08-10 17:08:47 +08:00
f.write_str("}")?;
2021-08-05 20:05:40 +08:00
Ok(())
}
}
2021-12-07 10:39:01 +08:00
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Retain<T: Attributes> {
2021-08-06 23:06:27 +08:00
pub n: usize,
2021-12-07 10:39:01 +08:00
pub attributes: T,
2021-08-05 20:05:40 +08:00
}
2021-12-07 10:39:01 +08:00
impl<T> fmt::Display for Retain<T>
where
T: Attributes,
{
2021-09-21 15:07:07 +08:00
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
if self.attributes.is_empty() {
f.write_fmt(format_args!("retain: {}", self.n))
} else {
f.write_fmt(format_args!("retain: {}, attributes: {}", self.n, self.attributes))
}
}
}
2021-12-07 10:39:01 +08:00
impl<T> Retain<T>
where
T: Attributes,
{
pub fn merge_or_new(&mut self, n: usize, attributes: T) -> Option<Operation<T>> {
2022-01-12 17:08:50 +08:00
// tracing::trace!(
// "merge_retain_or_new_op: len: {:?}, l: {} - r: {}",
// n,
// self.attributes,
// attributes
// );
2021-08-11 17:18:10 +08:00
if self.attributes == attributes {
self.n += n;
None
} else {
Some(Operation::retain_with_attributes(n, attributes))
2021-08-05 20:05:40 +08:00
}
}
2022-01-23 12:14:00 +08:00
pub fn is_plain(&self) -> bool {
self.attributes.is_empty()
}
2021-08-05 20:05:40 +08:00
}
2021-12-07 10:39:01 +08:00
impl<T> std::convert::From<usize> for Retain<T>
where
T: Attributes,
{
2021-08-06 23:06:27 +08:00
fn from(n: usize) -> Self {
2021-08-05 20:05:40 +08:00
Retain {
n,
2021-12-07 10:39:01 +08:00
attributes: T::default(),
2021-08-05 20:05:40 +08:00
}
}
}
2021-12-07 10:39:01 +08:00
impl<T> Deref for Retain<T>
where
T: Attributes,
{
2021-08-06 23:06:27 +08:00
type Target = usize;
2021-08-05 20:05:40 +08:00
2022-01-23 12:14:00 +08:00
fn deref(&self) -> &Self::Target {
&self.n
}
2021-08-05 20:05:40 +08:00
}
2021-12-07 10:39:01 +08:00
impl<T> DerefMut for Retain<T>
where
T: Attributes,
{
2022-01-23 12:14:00 +08:00
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.n
}
2021-08-05 20:05:40 +08:00
}
2021-12-07 10:39:01 +08:00
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Insert<T: Attributes> {
2022-08-01 11:32:25 +08:00
pub s: OTString,
2021-12-07 10:39:01 +08:00
pub attributes: T,
2021-08-05 20:05:40 +08:00
}
2021-12-07 10:39:01 +08:00
impl<T> fmt::Display for Insert<T>
where
T: Attributes,
{
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let mut s = self.s.clone();
2021-11-27 19:19:41 +08:00
if s.ends_with('\n') {
s.pop();
if s.is_empty() {
2021-11-12 21:44:26 +08:00
s = "new_line".into();
}
}
2021-09-21 15:07:07 +08:00
if self.attributes.is_empty() {
f.write_fmt(format_args!("insert: {}", s))
} else {
f.write_fmt(format_args!("insert: {}, attributes: {}", s, self.attributes))
}
}
}
2021-12-07 10:39:01 +08:00
impl<T> Insert<T>
where
T: Attributes,
{
2022-01-23 12:14:00 +08:00
pub fn utf16_size(&self) -> usize {
2022-08-01 09:31:40 +08:00
self.s.utf16_len()
2022-01-23 12:14:00 +08:00
}
2021-08-05 20:05:40 +08:00
2021-12-07 10:39:01 +08:00
pub fn merge_or_new_op(&mut self, s: &str, attributes: T) -> Option<Operation<T>> {
2021-08-11 17:18:10 +08:00
if self.attributes == attributes {
self.s += s;
None
} else {
Some(Operation::<T>::insert_with_attributes(s, attributes))
2021-08-05 20:05:40 +08:00
}
}
2021-11-12 21:44:26 +08:00
2022-01-23 12:14:00 +08:00
pub fn is_plain(&self) -> bool {
self.attributes.is_empty()
}
2021-08-05 20:05:40 +08:00
}
2021-12-07 10:39:01 +08:00
impl<T> std::convert::From<String> for Insert<T>
where
T: Attributes,
{
2021-08-05 20:05:40 +08:00
fn from(s: String) -> Self {
Insert {
2021-11-12 21:44:26 +08:00
s: s.into(),
2021-12-07 10:39:01 +08:00
attributes: T::default(),
2021-08-05 20:05:40 +08:00
}
}
}
2021-12-07 10:39:01 +08:00
impl<T> std::convert::From<&str> for Insert<T>
where
T: Attributes,
{
2022-01-23 12:14:00 +08:00
fn from(s: &str) -> Self {
Insert::from(s.to_owned())
}
2021-08-05 20:05:40 +08:00
}
2021-11-12 21:44:26 +08:00
2022-08-01 11:32:25 +08:00
impl<T> std::convert::From<OTString> for Insert<T>
2021-12-07 10:39:01 +08:00
where
T: Attributes,
{
2022-08-01 11:32:25 +08:00
fn from(s: OTString) -> Self {
2021-11-12 21:44:26 +08:00
Insert {
s,
2021-12-07 10:39:01 +08:00
attributes: T::default(),
2021-11-12 21:44:26 +08:00
}
}
}
2022-01-15 23:58:36 +08:00
2022-01-16 13:44:14 +08:00
#[derive(Debug, Clone, Eq, PartialEq, Default, Serialize, Deserialize)]
pub struct PhantomAttributes();
impl fmt::Display for PhantomAttributes {
2022-01-24 17:35:58 +08:00
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("PhantomAttributes")
2022-01-24 17:35:58 +08:00
}
2022-01-15 23:58:36 +08:00
}
impl Attributes for PhantomAttributes {}
2022-01-15 23:58:36 +08:00
2022-08-01 09:31:40 +08:00
impl OperationTransform for PhantomAttributes {
2022-01-24 17:35:58 +08:00
fn compose(&self, _other: &Self) -> Result<Self, OTError> {
Ok(self.clone())
}
2022-01-15 23:58:36 +08:00
2022-01-24 17:35:58 +08:00
fn transform(&self, other: &Self) -> Result<(Self, Self), OTError> {
Ok((self.clone(), other.clone()))
}
2022-01-15 23:58:36 +08:00
2022-01-24 17:35:58 +08:00
fn invert(&self, _other: &Self) -> Self {
self.clone()
}
2022-01-15 23:58:36 +08:00
}