mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
chore: create attribute
This commit is contained in:
parent
944b2d97cd
commit
c552e958ce
@ -49,8 +49,8 @@ pub fn make_default_board() -> BuildGridContext {
|
||||
let done_option = SelectOptionPB::with_color("Done", SelectOptionColorPB::Yellow);
|
||||
let single_select_type_option = SingleSelectTypeOptionBuilder::default()
|
||||
.add_option(to_do_option.clone())
|
||||
.add_option(doing_option.clone())
|
||||
.add_option(done_option.clone());
|
||||
.add_option(doing_option)
|
||||
.add_option(done_option);
|
||||
let single_select_field = FieldBuilder::new(single_select_type_option)
|
||||
.name("Status")
|
||||
.visibility(true)
|
||||
|
@ -9,7 +9,7 @@ use flowy_sync::{
|
||||
util::make_delta_from_revisions,
|
||||
};
|
||||
use lib_infra::future::BoxResultFuture;
|
||||
use lib_ot::core::{Attributes, EmptyAttributes, Operations};
|
||||
use lib_ot::core::{EmptyAttributes, OperationAttributes, Operations};
|
||||
use lib_ot::text_delta::TextAttributes;
|
||||
use serde::de::DeserializeOwned;
|
||||
use std::{convert::TryFrom, sync::Arc};
|
||||
@ -18,7 +18,7 @@ pub type DeltaMD5 = String;
|
||||
|
||||
pub trait ConflictResolver<T>
|
||||
where
|
||||
T: Attributes + Send + Sync,
|
||||
T: OperationAttributes + Send + Sync,
|
||||
{
|
||||
fn compose_delta(&self, delta: Operations<T>) -> BoxResultFuture<DeltaMD5, FlowyError>;
|
||||
fn transform_delta(&self, delta: Operations<T>) -> BoxResultFuture<TransformDeltas<T>, FlowyError>;
|
||||
@ -35,7 +35,7 @@ pub type PlainTextConflictController = ConflictController<EmptyAttributes>;
|
||||
|
||||
pub struct ConflictController<T>
|
||||
where
|
||||
T: Attributes + Send + Sync,
|
||||
T: OperationAttributes + Send + Sync,
|
||||
{
|
||||
user_id: String,
|
||||
resolver: Arc<dyn ConflictResolver<T> + Send + Sync>,
|
||||
@ -45,7 +45,7 @@ where
|
||||
|
||||
impl<T> ConflictController<T>
|
||||
where
|
||||
T: Attributes + Send + Sync + DeserializeOwned + serde::Serialize,
|
||||
T: OperationAttributes + Send + Sync + DeserializeOwned + serde::Serialize,
|
||||
{
|
||||
pub fn new(
|
||||
user_id: &str,
|
||||
@ -147,7 +147,7 @@ fn make_client_and_server_revision<T>(
|
||||
md5: String,
|
||||
) -> (Revision, Option<Revision>)
|
||||
where
|
||||
T: Attributes + serde::Serialize,
|
||||
T: OperationAttributes + serde::Serialize,
|
||||
{
|
||||
let (base_rev_id, rev_id) = rev_manager.next_rev_id_pair();
|
||||
let client_revision = Revision::new(
|
||||
@ -179,7 +179,7 @@ pub type RichTextTransformDeltas = TransformDeltas<TextAttributes>;
|
||||
|
||||
pub struct TransformDeltas<T>
|
||||
where
|
||||
T: Attributes,
|
||||
T: OperationAttributes,
|
||||
{
|
||||
pub client_prime: Operations<T>,
|
||||
pub server_prime: Option<Operations<T>>,
|
||||
|
@ -5,7 +5,7 @@ use lib_ot::text_delta::TextDeltaBuilder;
|
||||
use lib_ot::{
|
||||
core::Interval,
|
||||
core::*,
|
||||
text_delta::{AttributeBuilder, TextAttribute, TextAttributes, TextDelta},
|
||||
text_delta::{TextAttribute, TextAttributes, TextDelta, TextDeltaAttributeBuilder},
|
||||
};
|
||||
|
||||
#[test]
|
||||
@ -206,7 +206,7 @@ fn delta_utf16_code_unit_seek() {
|
||||
#[test]
|
||||
fn delta_utf16_code_unit_seek_with_attributes() {
|
||||
let mut delta = TextDelta::default();
|
||||
let attributes = AttributeBuilder::new()
|
||||
let attributes = TextDeltaAttributeBuilder::new()
|
||||
.add_attr(TextAttribute::Bold(true))
|
||||
.add_attr(TextAttribute::Italic(true))
|
||||
.build();
|
||||
@ -492,7 +492,9 @@ fn transform_with_two_delta() {
|
||||
let mut a_s = String::new();
|
||||
a.insert(
|
||||
"123",
|
||||
AttributeBuilder::new().add_attr(TextAttribute::Bold(true)).build(),
|
||||
TextDeltaAttributeBuilder::new()
|
||||
.add_attr(TextAttribute::Bold(true))
|
||||
.build(),
|
||||
);
|
||||
a_s = a.apply(&a_s).unwrap();
|
||||
assert_eq!(&a_s, "123");
|
||||
|
@ -2,12 +2,12 @@ use flowy_sync::client_document::{ClientDocument, PlainDoc};
|
||||
use lib_ot::text_delta::RichTextOperation;
|
||||
use lib_ot::{
|
||||
core::*,
|
||||
text_delta::{AttributeBuilder, TextAttribute, TextAttributeValue, TextDelta},
|
||||
text_delta::{TextAttribute, TextAttributeValue, TextDelta, TextDeltaAttributeBuilder},
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn operation_insert_serialize_test() {
|
||||
let attributes = AttributeBuilder::new()
|
||||
let attributes = TextDeltaAttributeBuilder::new()
|
||||
.add_attr(TextAttribute::Bold(true))
|
||||
.add_attr(TextAttribute::Italic(true))
|
||||
.build();
|
||||
@ -38,7 +38,7 @@ fn operation_delete_serialize_test() {
|
||||
|
||||
#[test]
|
||||
fn attributes_serialize_test() {
|
||||
let attributes = AttributeBuilder::new()
|
||||
let attributes = TextDeltaAttributeBuilder::new()
|
||||
.add_attr(TextAttribute::Bold(true))
|
||||
.add_attr(TextAttribute::Italic(true))
|
||||
.build();
|
||||
@ -52,7 +52,7 @@ fn attributes_serialize_test() {
|
||||
fn delta_serialize_multi_attribute_test() {
|
||||
let mut delta = Operations::default();
|
||||
|
||||
let attributes = AttributeBuilder::new()
|
||||
let attributes = TextDeltaAttributeBuilder::new()
|
||||
.add_attr(TextAttribute::Bold(true))
|
||||
.add_attr(TextAttribute::Italic(true))
|
||||
.build();
|
||||
|
@ -1,6 +1,6 @@
|
||||
use crate::{client_document::DeleteExt, util::is_newline};
|
||||
use lib_ot::{
|
||||
core::{Attributes, Interval, OperationBuilder, OperationIterator, Utf16CodeUnitMetric, NEW_LINE},
|
||||
core::{Interval, OperationAttributes, OperationBuilder, OperationIterator, Utf16CodeUnitMetric, NEW_LINE},
|
||||
text_delta::{plain_attributes, TextDelta},
|
||||
};
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
use crate::client_document::InsertExt;
|
||||
use lib_ot::{
|
||||
core::{Attributes, OperationBuilder, OperationIterator, NEW_LINE},
|
||||
core::{OperationAttributes, OperationBuilder, OperationIterator, NEW_LINE},
|
||||
text_delta::{TextAttributeKey, TextAttributes, TextDelta},
|
||||
};
|
||||
|
||||
|
@ -9,7 +9,7 @@ use crate::{
|
||||
util::*,
|
||||
};
|
||||
use lib_infra::future::BoxResultFuture;
|
||||
use lib_ot::core::{Attributes, Operations};
|
||||
use lib_ot::core::{OperationAttributes, Operations};
|
||||
use parking_lot::RwLock;
|
||||
use serde::de::DeserializeOwned;
|
||||
use std::{
|
||||
@ -43,7 +43,7 @@ pub trait RevisionSyncPersistence: Send + Sync + 'static {
|
||||
) -> BoxResultFuture<(), CollaborateError>;
|
||||
}
|
||||
|
||||
pub trait RevisionSyncObject<T: Attributes>: Send + Sync + 'static {
|
||||
pub trait RevisionSyncObject<T: OperationAttributes>: Send + Sync + 'static {
|
||||
fn id(&self) -> &str;
|
||||
fn compose(&mut self, other: &Operations<T>) -> Result<(), CollaborateError>;
|
||||
fn transform(&self, other: &Operations<T>) -> Result<(Operations<T>, Operations<T>), CollaborateError>;
|
||||
@ -57,7 +57,7 @@ pub enum RevisionSyncResponse {
|
||||
Ack(ServerRevisionWSData),
|
||||
}
|
||||
|
||||
pub struct RevisionSynchronizer<T: Attributes> {
|
||||
pub struct RevisionSynchronizer<T: OperationAttributes> {
|
||||
object_id: String,
|
||||
rev_id: AtomicI64,
|
||||
object: Arc<RwLock<dyn RevisionSyncObject<T>>>,
|
||||
@ -66,7 +66,7 @@ pub struct RevisionSynchronizer<T: Attributes> {
|
||||
|
||||
impl<T> RevisionSynchronizer<T>
|
||||
where
|
||||
T: Attributes + DeserializeOwned + serde::Serialize + 'static,
|
||||
T: OperationAttributes + DeserializeOwned + serde::Serialize + 'static,
|
||||
{
|
||||
pub fn new<S, P>(rev_id: i64, sync_object: S, persistence: P) -> RevisionSynchronizer<T>
|
||||
where
|
||||
|
@ -7,9 +7,9 @@ use crate::{
|
||||
errors::{CollaborateError, CollaborateResult},
|
||||
};
|
||||
use dissimilar::Chunk;
|
||||
use lib_ot::core::{Delta, EmptyAttributes, OTString, OperationBuilder};
|
||||
use lib_ot::core::{Delta, EmptyAttributes, OTString, OperationAttributes, OperationBuilder};
|
||||
use lib_ot::{
|
||||
core::{Attributes, OperationTransform, Operations, NEW_LINE, WHITESPACE},
|
||||
core::{OperationTransform, Operations, NEW_LINE, WHITESPACE},
|
||||
text_delta::TextDelta,
|
||||
};
|
||||
use serde::de::DeserializeOwned;
|
||||
@ -64,7 +64,7 @@ impl RevIdCounter {
|
||||
#[tracing::instrument(level = "trace", skip(revisions), err)]
|
||||
pub fn make_delta_from_revisions<T>(revisions: Vec<Revision>) -> CollaborateResult<Operations<T>>
|
||||
where
|
||||
T: Attributes + DeserializeOwned,
|
||||
T: OperationAttributes + DeserializeOwned,
|
||||
{
|
||||
let mut delta = Operations::<T>::new();
|
||||
for revision in revisions {
|
||||
@ -87,7 +87,7 @@ pub fn make_text_delta_from_revisions(revisions: Vec<Revision>) -> CollaborateRe
|
||||
|
||||
pub fn make_delta_from_revision_pb<T>(revisions: Vec<Revision>) -> CollaborateResult<Operations<T>>
|
||||
where
|
||||
T: Attributes + DeserializeOwned,
|
||||
T: OperationAttributes + DeserializeOwned,
|
||||
{
|
||||
let mut new_delta = Operations::<T>::new();
|
||||
for revision in revisions {
|
||||
@ -206,7 +206,7 @@ pub fn rev_id_from_str(s: &str) -> Result<i64, CollaborateError> {
|
||||
Ok(rev_id)
|
||||
}
|
||||
|
||||
pub fn cal_diff<T: Attributes>(old: String, new: String) -> Option<Operations<T>> {
|
||||
pub fn cal_diff<T: OperationAttributes>(old: String, new: String) -> Option<Operations<T>> {
|
||||
let chunks = dissimilar::diff(&old, &new);
|
||||
let mut delta_builder = OperationBuilder::<T>::new();
|
||||
for chunk in &chunks {
|
||||
|
@ -113,7 +113,7 @@ pub fn markdown_encoder(delta: &Operations<TextAttributes>) -> String {
|
||||
while iterator.has_next() {
|
||||
let operation = iterator.next().unwrap();
|
||||
let operation_data = operation.get_data();
|
||||
if !operation_data.contains("\n") {
|
||||
if !operation_data.contains('\n') {
|
||||
handle_inline(
|
||||
&mut current_inline_style,
|
||||
&mut line_buffer,
|
||||
@ -196,7 +196,7 @@ fn handle_inline(
|
||||
|
||||
fn trim_right(buffer: &mut String) -> String {
|
||||
let text = buffer.clone();
|
||||
if !text.ends_with(" ") {
|
||||
if !text.ends_with(' ') {
|
||||
return String::from("");
|
||||
}
|
||||
let result = text.trim_end();
|
||||
@ -208,7 +208,7 @@ fn trim_right(buffer: &mut String) -> String {
|
||||
fn write_attribute(buffer: &mut String, key: &TextAttributeKey, value: &TextAttributeValue, close: bool) {
|
||||
match key {
|
||||
TextAttributeKey::Bold => buffer.push_str("**"),
|
||||
TextAttributeKey::Italic => buffer.push_str("_"),
|
||||
TextAttributeKey::Italic => buffer.push('_'),
|
||||
TextAttributeKey::Underline => {
|
||||
if close {
|
||||
buffer.push_str("</u>")
|
||||
@ -216,18 +216,12 @@ fn write_attribute(buffer: &mut String, key: &TextAttributeKey, value: &TextAttr
|
||||
buffer.push_str("<u>")
|
||||
}
|
||||
}
|
||||
TextAttributeKey::StrikeThrough => {
|
||||
if close {
|
||||
buffer.push_str("~~")
|
||||
} else {
|
||||
buffer.push_str("~~")
|
||||
}
|
||||
}
|
||||
TextAttributeKey::StrikeThrough => buffer.push_str("~~"),
|
||||
TextAttributeKey::Link => {
|
||||
if close {
|
||||
buffer.push_str(format!("]({})", value.0.as_ref().unwrap()).as_str())
|
||||
} else {
|
||||
buffer.push_str("[")
|
||||
buffer.push('[')
|
||||
}
|
||||
}
|
||||
TextAttributeKey::Background => {
|
||||
@ -244,13 +238,7 @@ fn write_attribute(buffer: &mut String, key: &TextAttributeKey, value: &TextAttr
|
||||
buffer.push_str("```\n")
|
||||
}
|
||||
}
|
||||
TextAttributeKey::InlineCode => {
|
||||
if close {
|
||||
buffer.push_str("`")
|
||||
} else {
|
||||
buffer.push_str("`")
|
||||
}
|
||||
}
|
||||
TextAttributeKey::InlineCode => buffer.push('`'),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
@ -277,13 +265,7 @@ fn handle_line(
|
||||
TextAttributes::default(),
|
||||
);
|
||||
|
||||
let line_block_key = attributes.keys().find(|key| {
|
||||
if is_block(*key) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
let line_block_key = attributes.keys().find(|key| is_block(*key));
|
||||
|
||||
match (line_block_key, ¤t_block_style) {
|
||||
(Some(line_block_key), Some(current_block_style))
|
||||
@ -347,7 +329,7 @@ fn handle_block(
|
||||
}
|
||||
Some(block_style) => {
|
||||
for line in current_block_lines {
|
||||
write_block_tag(markdown_buffer, &block_style, false);
|
||||
write_block_tag(markdown_buffer, block_style, false);
|
||||
markdown_buffer.push_str(line);
|
||||
markdown_buffer.push('\n');
|
||||
}
|
||||
|
@ -6,9 +6,9 @@ use std::collections::HashMap;
|
||||
pub type AttributeMap = HashMap<AttributeKey, AttributeValue>;
|
||||
|
||||
#[derive(Default, Clone, Serialize, Deserialize, Eq, PartialEq, Debug)]
|
||||
pub struct NodeAttributes(AttributeMap);
|
||||
pub struct Attributes(AttributeMap);
|
||||
|
||||
impl std::ops::Deref for NodeAttributes {
|
||||
impl std::ops::Deref for Attributes {
|
||||
type Target = AttributeMap;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
@ -16,15 +16,15 @@ impl std::ops::Deref for NodeAttributes {
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::DerefMut for NodeAttributes {
|
||||
impl std::ops::DerefMut for Attributes {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl NodeAttributes {
|
||||
pub fn new() -> NodeAttributes {
|
||||
NodeAttributes(HashMap::new())
|
||||
impl Attributes {
|
||||
pub fn new() -> Attributes {
|
||||
Attributes(HashMap::new())
|
||||
}
|
||||
|
||||
pub fn from_value(attribute_map: AttributeMap) -> Self {
|
||||
@ -44,7 +44,7 @@ impl NodeAttributes {
|
||||
}
|
||||
}
|
||||
|
||||
impl OperationTransform for NodeAttributes {
|
||||
impl OperationTransform for Attributes {
|
||||
fn compose(&self, other: &Self) -> Result<Self, OTError>
|
||||
where
|
||||
Self: Sized,
|
||||
@ -58,14 +58,14 @@ impl OperationTransform for NodeAttributes {
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
let a = self.iter().fold(NodeAttributes::new(), |mut new_attributes, (k, v)| {
|
||||
let a = self.iter().fold(Attributes::new(), |mut new_attributes, (k, v)| {
|
||||
if !other.contains_key(k) {
|
||||
new_attributes.insert(k.clone(), v.clone());
|
||||
}
|
||||
new_attributes
|
||||
});
|
||||
|
||||
let b = other.iter().fold(NodeAttributes::new(), |mut new_attributes, (k, v)| {
|
||||
let b = other.iter().fold(Attributes::new(), |mut new_attributes, (k, v)| {
|
||||
if !self.contains_key(k) {
|
||||
new_attributes.insert(k.clone(), v.clone());
|
||||
}
|
||||
@ -76,7 +76,7 @@ impl OperationTransform for NodeAttributes {
|
||||
}
|
||||
|
||||
fn invert(&self, other: &Self) -> Self {
|
||||
let base_inverted = other.iter().fold(NodeAttributes::new(), |mut attributes, (k, v)| {
|
||||
let base_inverted = other.iter().fold(Attributes::new(), |mut attributes, (k, v)| {
|
||||
if other.get(k) != self.get(k) && self.contains_key(k) {
|
||||
attributes.insert(k.clone(), v.clone());
|
||||
}
|
||||
@ -136,7 +136,7 @@ impl AttributeValue {
|
||||
value: Some(val.to_string()),
|
||||
}
|
||||
}
|
||||
pub fn from_str(s: &str) -> Self {
|
||||
pub fn from_string(s: &str) -> Self {
|
||||
let value = if s.is_empty() { None } else { Some(s.to_string()) };
|
||||
Self {
|
||||
ty: ValueType::StrType,
|
||||
@ -170,15 +170,14 @@ impl std::convert::From<bool> for AttributeValue {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct NodeAttributeBuilder {
|
||||
attributes: NodeAttributes,
|
||||
#[derive(Default)]
|
||||
pub struct AttributeBuilder {
|
||||
attributes: Attributes,
|
||||
}
|
||||
|
||||
impl NodeAttributeBuilder {
|
||||
impl AttributeBuilder {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
attributes: NodeAttributes::default(),
|
||||
}
|
||||
Self::default()
|
||||
}
|
||||
|
||||
pub fn insert<K: ToString, V: Into<AttributeValue>>(mut self, key: K, value: V) -> Self {
|
||||
@ -191,7 +190,7 @@ impl NodeAttributeBuilder {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn build(self) -> NodeAttributes {
|
||||
pub fn build(self) -> Attributes {
|
||||
self.attributes
|
||||
}
|
||||
}
|
@ -1,11 +1,9 @@
|
||||
use std::fmt;
|
||||
|
||||
use crate::core::attributes::AttributeValue;
|
||||
use serde::{
|
||||
de::{self, MapAccess, Visitor},
|
||||
Deserialize, Deserializer, Serialize, Serializer,
|
||||
};
|
||||
|
||||
use super::AttributeValue;
|
||||
use std::fmt;
|
||||
|
||||
impl Serialize for AttributeValue {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
@ -125,7 +123,7 @@ impl<'de> Deserialize<'de> for AttributeValue {
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
Ok(AttributeValue::from_str(s))
|
||||
Ok(AttributeValue::from_string(s))
|
||||
}
|
||||
|
||||
fn visit_none<E>(self) -> Result<Self::Value, E>
|
4
shared-lib/lib-ot/src/core/attributes/mod.rs
Normal file
4
shared-lib/lib-ot/src/core/attributes/mod.rs
Normal file
@ -0,0 +1,4 @@
|
||||
mod attribute;
|
||||
mod attribute_serde;
|
||||
|
||||
pub use attribute::*;
|
@ -1,4 +1,4 @@
|
||||
use crate::core::delta::operation::Attributes;
|
||||
use crate::core::delta::operation::OperationAttributes;
|
||||
use crate::core::delta::{trim, Operations};
|
||||
use crate::core::Operation;
|
||||
|
||||
@ -16,13 +16,13 @@ use crate::core::Operation;
|
||||
/// .build();
|
||||
/// assert_eq!(delta.content().unwrap(), "AppFlowy");
|
||||
/// ```
|
||||
pub struct OperationBuilder<T: Attributes> {
|
||||
pub struct OperationBuilder<T: OperationAttributes> {
|
||||
delta: Operations<T>,
|
||||
}
|
||||
|
||||
impl<T> std::default::Default for OperationBuilder<T>
|
||||
where
|
||||
T: Attributes,
|
||||
T: OperationAttributes,
|
||||
{
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
@ -33,7 +33,7 @@ where
|
||||
|
||||
impl<T> OperationBuilder<T>
|
||||
where
|
||||
T: Attributes,
|
||||
T: OperationAttributes,
|
||||
{
|
||||
pub fn new() -> Self {
|
||||
OperationBuilder::default()
|
||||
|
@ -1,5 +1,5 @@
|
||||
#![allow(clippy::while_let_on_iterator)]
|
||||
use crate::core::delta::operation::{Attributes, Operation};
|
||||
use crate::core::delta::operation::{Operation, OperationAttributes};
|
||||
use crate::core::delta::Operations;
|
||||
use crate::core::interval::Interval;
|
||||
use crate::errors::{ErrorBuilder, OTError, OTErrorCode};
|
||||
@ -7,7 +7,7 @@ use std::{cmp::min, iter::Enumerate, slice::Iter};
|
||||
|
||||
/// A [OperationsCursor] is used to iterate the delta and return the corresponding delta.
|
||||
#[derive(Debug)]
|
||||
pub struct OperationsCursor<'a, T: Attributes> {
|
||||
pub struct OperationsCursor<'a, T: OperationAttributes> {
|
||||
pub(crate) delta: &'a Operations<T>,
|
||||
pub(crate) origin_iv: Interval,
|
||||
pub(crate) consume_iv: Interval,
|
||||
@ -19,7 +19,7 @@ pub struct OperationsCursor<'a, T: Attributes> {
|
||||
|
||||
impl<'a, T> OperationsCursor<'a, T>
|
||||
where
|
||||
T: Attributes,
|
||||
T: OperationAttributes,
|
||||
{
|
||||
/// # Arguments
|
||||
///
|
||||
@ -184,7 +184,7 @@ where
|
||||
|
||||
fn find_next<'a, T>(cursor: &mut OperationsCursor<'a, T>) -> Option<&'a Operation<T>>
|
||||
where
|
||||
T: Attributes,
|
||||
T: OperationAttributes,
|
||||
{
|
||||
match cursor.iter.next() {
|
||||
None => None,
|
||||
@ -197,7 +197,7 @@ where
|
||||
|
||||
type SeekResult = Result<(), OTError>;
|
||||
pub trait Metric {
|
||||
fn seek<T: Attributes>(cursor: &mut OperationsCursor<T>, offset: usize) -> SeekResult;
|
||||
fn seek<T: OperationAttributes>(cursor: &mut OperationsCursor<T>, offset: usize) -> SeekResult;
|
||||
}
|
||||
|
||||
/// [OpMetric] is used by [DeltaIterator] for seeking operations
|
||||
@ -205,7 +205,7 @@ pub trait Metric {
|
||||
pub struct OpMetric();
|
||||
|
||||
impl Metric for OpMetric {
|
||||
fn seek<T: Attributes>(cursor: &mut OperationsCursor<T>, op_offset: usize) -> SeekResult {
|
||||
fn seek<T: OperationAttributes>(cursor: &mut OperationsCursor<T>, op_offset: usize) -> SeekResult {
|
||||
let _ = check_bound(cursor.op_offset, op_offset)?;
|
||||
let mut seek_cursor = OperationsCursor::new(cursor.delta, cursor.origin_iv);
|
||||
|
||||
@ -224,7 +224,7 @@ impl Metric for OpMetric {
|
||||
pub struct Utf16CodeUnitMetric();
|
||||
|
||||
impl Metric for Utf16CodeUnitMetric {
|
||||
fn seek<T: Attributes>(cursor: &mut OperationsCursor<T>, offset: usize) -> SeekResult {
|
||||
fn seek<T: OperationAttributes>(cursor: &mut OperationsCursor<T>, offset: usize) -> SeekResult {
|
||||
if offset > 0 {
|
||||
let _ = check_bound(cursor.consume_count, offset)?;
|
||||
let _ = cursor.next_with_len(Some(offset));
|
||||
|
@ -1,5 +1,5 @@
|
||||
use super::cursor::*;
|
||||
use crate::core::delta::operation::{Attributes, Operation};
|
||||
use crate::core::delta::operation::{Operation, OperationAttributes};
|
||||
use crate::core::delta::{Operations, NEW_LINE};
|
||||
use crate::core::interval::Interval;
|
||||
use crate::text_delta::TextAttributes;
|
||||
@ -28,13 +28,13 @@ pub(crate) const MAX_IV_LEN: usize = i32::MAX as usize;
|
||||
/// vec![Operation::insert("23")]
|
||||
/// );
|
||||
/// ```
|
||||
pub struct OperationIterator<'a, T: Attributes> {
|
||||
pub struct OperationIterator<'a, T: OperationAttributes> {
|
||||
cursor: OperationsCursor<'a, T>,
|
||||
}
|
||||
|
||||
impl<'a, T> OperationIterator<'a, T>
|
||||
where
|
||||
T: Attributes,
|
||||
T: OperationAttributes,
|
||||
{
|
||||
pub fn new(delta: &'a Operations<T>) -> Self {
|
||||
let interval = Interval::new(0, MAX_IV_LEN);
|
||||
@ -124,7 +124,7 @@ where
|
||||
|
||||
impl<'a, T> Iterator for OperationIterator<'a, T>
|
||||
where
|
||||
T: Attributes,
|
||||
T: OperationAttributes,
|
||||
{
|
||||
type Item = Operation<T>;
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
@ -148,13 +148,13 @@ pub fn is_empty_line_at_index(delta: &Operations<TextAttributes>, index: usize)
|
||||
OpNewline::parse(&prev).is_end() && OpNewline::parse(&next).is_start()
|
||||
}
|
||||
|
||||
pub struct AttributesIter<'a, T: Attributes> {
|
||||
pub struct AttributesIter<'a, T: OperationAttributes> {
|
||||
delta_iter: OperationIterator<'a, T>,
|
||||
}
|
||||
|
||||
impl<'a, T> AttributesIter<'a, T>
|
||||
where
|
||||
T: Attributes,
|
||||
T: OperationAttributes,
|
||||
{
|
||||
pub fn new(delta: &'a Operations<T>) -> Self {
|
||||
let interval = Interval::new(0, usize::MAX);
|
||||
@ -176,7 +176,7 @@ where
|
||||
|
||||
impl<'a, T> Deref for AttributesIter<'a, T>
|
||||
where
|
||||
T: Attributes,
|
||||
T: OperationAttributes,
|
||||
{
|
||||
type Target = OperationIterator<'a, T>;
|
||||
|
||||
@ -187,7 +187,7 @@ where
|
||||
|
||||
impl<'a, T> DerefMut for AttributesIter<'a, T>
|
||||
where
|
||||
T: Attributes,
|
||||
T: OperationAttributes,
|
||||
{
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.delta_iter
|
||||
@ -196,7 +196,7 @@ where
|
||||
|
||||
impl<'a, T> Iterator for AttributesIter<'a, T>
|
||||
where
|
||||
T: Attributes,
|
||||
T: OperationAttributes,
|
||||
{
|
||||
type Item = (usize, T);
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
@ -234,7 +234,7 @@ pub enum OpNewline {
|
||||
}
|
||||
|
||||
impl OpNewline {
|
||||
pub fn parse<T: Attributes>(op: &Operation<T>) -> OpNewline {
|
||||
pub fn parse<T: OperationAttributes>(op: &Operation<T>) -> OpNewline {
|
||||
let s = op.get_data();
|
||||
|
||||
if s == NEW_LINE {
|
||||
|
@ -1,17 +1,17 @@
|
||||
use crate::core::delta::operation::{Attributes, EmptyAttributes, Operation};
|
||||
use crate::core::delta::operation::{EmptyAttributes, Operation, OperationAttributes};
|
||||
use crate::text_delta::TextAttributes;
|
||||
|
||||
pub type RichTextOpBuilder = OperationsBuilder<TextAttributes>;
|
||||
pub type PlainTextOpBuilder = OperationsBuilder<EmptyAttributes>;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct OperationsBuilder<T: Attributes> {
|
||||
pub struct OperationsBuilder<T: OperationAttributes> {
|
||||
operations: Vec<Operation<T>>,
|
||||
}
|
||||
|
||||
impl<T> OperationsBuilder<T>
|
||||
where
|
||||
T: Attributes,
|
||||
T: OperationAttributes,
|
||||
{
|
||||
pub fn new() -> OperationsBuilder<T> {
|
||||
OperationsBuilder::default()
|
||||
|
@ -70,7 +70,7 @@ pub trait OperationTransform {
|
||||
///Because [Operation] is generic over the T, so you must specify the T. For example, the [Delta] uses
|
||||
///[EmptyAttributes] as the T. [EmptyAttributes] does nothing, just a phantom.
|
||||
///
|
||||
pub trait Attributes: Default + Display + Eq + PartialEq + Clone + Debug + OperationTransform {
|
||||
pub trait OperationAttributes: Default + Display + Eq + PartialEq + Clone + Debug + OperationTransform {
|
||||
fn is_empty(&self) -> bool {
|
||||
true
|
||||
}
|
||||
@ -96,7 +96,7 @@ pub trait Attributes: Default + Display + Eq + PartialEq + Clone + Debug + Opera
|
||||
/// to json string. You could check out the operation_serde.rs for more information.
|
||||
///
|
||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
||||
pub enum Operation<T: Attributes> {
|
||||
pub enum Operation<T: OperationAttributes> {
|
||||
Delete(usize),
|
||||
Retain(Retain<T>),
|
||||
Insert(Insert<T>),
|
||||
@ -104,7 +104,7 @@ pub enum Operation<T: Attributes> {
|
||||
|
||||
impl<T> Operation<T>
|
||||
where
|
||||
T: Attributes,
|
||||
T: OperationAttributes,
|
||||
{
|
||||
pub fn delete(n: usize) -> Self {
|
||||
Self::Delete(n)
|
||||
@ -281,7 +281,7 @@ where
|
||||
|
||||
impl<T> fmt::Display for Operation<T>
|
||||
where
|
||||
T: Attributes,
|
||||
T: OperationAttributes,
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.write_str("{")?;
|
||||
@ -302,14 +302,14 @@ where
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub struct Retain<T: Attributes> {
|
||||
pub struct Retain<T: OperationAttributes> {
|
||||
pub n: usize,
|
||||
pub attributes: T,
|
||||
}
|
||||
|
||||
impl<T> fmt::Display for Retain<T>
|
||||
where
|
||||
T: Attributes,
|
||||
T: OperationAttributes,
|
||||
{
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
||||
if self.attributes.is_empty() {
|
||||
@ -322,7 +322,7 @@ where
|
||||
|
||||
impl<T> Retain<T>
|
||||
where
|
||||
T: Attributes,
|
||||
T: OperationAttributes,
|
||||
{
|
||||
pub fn merge_or_new(&mut self, n: usize, attributes: T) -> Option<Operation<T>> {
|
||||
// tracing::trace!(
|
||||
@ -346,7 +346,7 @@ where
|
||||
|
||||
impl<T> std::convert::From<usize> for Retain<T>
|
||||
where
|
||||
T: Attributes,
|
||||
T: OperationAttributes,
|
||||
{
|
||||
fn from(n: usize) -> Self {
|
||||
Retain {
|
||||
@ -358,7 +358,7 @@ where
|
||||
|
||||
impl<T> Deref for Retain<T>
|
||||
where
|
||||
T: Attributes,
|
||||
T: OperationAttributes,
|
||||
{
|
||||
type Target = usize;
|
||||
|
||||
@ -369,7 +369,7 @@ where
|
||||
|
||||
impl<T> DerefMut for Retain<T>
|
||||
where
|
||||
T: Attributes,
|
||||
T: OperationAttributes,
|
||||
{
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.n
|
||||
@ -377,14 +377,14 @@ where
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub struct Insert<T: Attributes> {
|
||||
pub struct Insert<T: OperationAttributes> {
|
||||
pub s: OTString,
|
||||
pub attributes: T,
|
||||
}
|
||||
|
||||
impl<T> fmt::Display for Insert<T>
|
||||
where
|
||||
T: Attributes,
|
||||
T: OperationAttributes,
|
||||
{
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
||||
let mut s = self.s.clone();
|
||||
@ -405,7 +405,7 @@ where
|
||||
|
||||
impl<T> Insert<T>
|
||||
where
|
||||
T: Attributes,
|
||||
T: OperationAttributes,
|
||||
{
|
||||
pub fn utf16_size(&self) -> usize {
|
||||
self.s.utf16_len()
|
||||
@ -427,7 +427,7 @@ where
|
||||
|
||||
impl<T> std::convert::From<String> for Insert<T>
|
||||
where
|
||||
T: Attributes,
|
||||
T: OperationAttributes,
|
||||
{
|
||||
fn from(s: String) -> Self {
|
||||
Insert {
|
||||
@ -439,7 +439,7 @@ where
|
||||
|
||||
impl<T> std::convert::From<&str> for Insert<T>
|
||||
where
|
||||
T: Attributes,
|
||||
T: OperationAttributes,
|
||||
{
|
||||
fn from(s: &str) -> Self {
|
||||
Insert::from(s.to_owned())
|
||||
@ -448,7 +448,7 @@ where
|
||||
|
||||
impl<T> std::convert::From<OTString> for Insert<T>
|
||||
where
|
||||
T: Attributes,
|
||||
T: OperationAttributes,
|
||||
{
|
||||
fn from(s: OTString) -> Self {
|
||||
Insert {
|
||||
@ -466,7 +466,7 @@ impl fmt::Display for EmptyAttributes {
|
||||
}
|
||||
}
|
||||
|
||||
impl Attributes for EmptyAttributes {}
|
||||
impl OperationAttributes for EmptyAttributes {}
|
||||
|
||||
impl OperationTransform for EmptyAttributes {
|
||||
fn compose(&self, _other: &Self) -> Result<Self, OTError> {
|
||||
|
@ -1,4 +1,4 @@
|
||||
use crate::core::delta::operation::{Attributes, Insert, Operation, Retain};
|
||||
use crate::core::delta::operation::{Insert, Operation, OperationAttributes, Retain};
|
||||
use crate::core::ot_str::OTString;
|
||||
use serde::{
|
||||
de,
|
||||
@ -10,7 +10,7 @@ use std::{fmt, marker::PhantomData};
|
||||
|
||||
impl<T> Serialize for Operation<T>
|
||||
where
|
||||
T: Attributes + Serialize,
|
||||
T: OperationAttributes + Serialize,
|
||||
{
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
@ -30,7 +30,7 @@ where
|
||||
|
||||
impl<'de, T> Deserialize<'de> for Operation<T>
|
||||
where
|
||||
T: Attributes + Deserialize<'de>,
|
||||
T: OperationAttributes + Deserialize<'de>,
|
||||
{
|
||||
fn deserialize<D>(deserializer: D) -> Result<Operation<T>, D::Error>
|
||||
where
|
||||
@ -40,7 +40,7 @@ where
|
||||
|
||||
impl<'de, T> Visitor<'de> for OperationVisitor<T>
|
||||
where
|
||||
T: Attributes + Deserialize<'de>,
|
||||
T: OperationAttributes + Deserialize<'de>,
|
||||
{
|
||||
type Value = Operation<T>;
|
||||
|
||||
@ -105,7 +105,7 @@ where
|
||||
|
||||
impl<T> Serialize for Retain<T>
|
||||
where
|
||||
T: Attributes + Serialize,
|
||||
T: OperationAttributes + Serialize,
|
||||
{
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
@ -123,7 +123,7 @@ where
|
||||
|
||||
impl<'de, T> Deserialize<'de> for Retain<T>
|
||||
where
|
||||
T: Attributes + Deserialize<'de>,
|
||||
T: OperationAttributes + Deserialize<'de>,
|
||||
{
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, <D as Deserializer<'de>>::Error>
|
||||
where
|
||||
@ -133,7 +133,7 @@ where
|
||||
|
||||
impl<'de, T> Visitor<'de> for RetainVisitor<T>
|
||||
where
|
||||
T: Attributes + Deserialize<'de>,
|
||||
T: OperationAttributes + Deserialize<'de>,
|
||||
{
|
||||
type Value = Retain<T>;
|
||||
|
||||
@ -208,7 +208,7 @@ where
|
||||
|
||||
impl<T> Serialize for Insert<T>
|
||||
where
|
||||
T: Attributes + Serialize,
|
||||
T: OperationAttributes + Serialize,
|
||||
{
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
@ -226,7 +226,7 @@ where
|
||||
|
||||
impl<'de, T> Deserialize<'de> for Insert<T>
|
||||
where
|
||||
T: Attributes + Deserialize<'de>,
|
||||
T: OperationAttributes + Deserialize<'de>,
|
||||
{
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, <D as Deserializer<'de>>::Error>
|
||||
where
|
||||
@ -236,7 +236,7 @@ where
|
||||
|
||||
impl<'de, T> Visitor<'de> for InsertVisitor<T>
|
||||
where
|
||||
T: Attributes + Deserialize<'de>,
|
||||
T: OperationAttributes + Deserialize<'de>,
|
||||
{
|
||||
type Value = Insert<T>;
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
use crate::errors::{ErrorBuilder, OTError, OTErrorCode};
|
||||
|
||||
use crate::core::delta::operation::{Attributes, EmptyAttributes, Operation, OperationTransform};
|
||||
use crate::core::delta::operation::{EmptyAttributes, Operation, OperationAttributes, OperationTransform};
|
||||
use crate::core::delta::{OperationIterator, MAX_IV_LEN};
|
||||
use crate::core::interval::Interval;
|
||||
use crate::core::ot_str::OTString;
|
||||
@ -28,7 +28,7 @@ pub type DeltaBuilder = OperationBuilder<EmptyAttributes>;
|
||||
/// a JSON string.
|
||||
///
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct Operations<T: Attributes> {
|
||||
pub struct Operations<T: OperationAttributes> {
|
||||
pub ops: Vec<Operation<T>>,
|
||||
|
||||
/// 'Delete' and 'Retain' operation will update the [utf16_base_len]
|
||||
@ -42,7 +42,7 @@ pub struct Operations<T: Attributes> {
|
||||
|
||||
impl<T> Default for Operations<T>
|
||||
where
|
||||
T: Attributes,
|
||||
T: OperationAttributes,
|
||||
{
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
@ -55,7 +55,7 @@ where
|
||||
|
||||
impl<T> fmt::Display for Operations<T>
|
||||
where
|
||||
T: Attributes,
|
||||
T: OperationAttributes,
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
// f.write_str(&serde_json::to_string(self).unwrap_or("".to_owned()))?;
|
||||
@ -70,7 +70,7 @@ where
|
||||
|
||||
impl<T> FromIterator<Operation<T>> for Operations<T>
|
||||
where
|
||||
T: Attributes,
|
||||
T: OperationAttributes,
|
||||
{
|
||||
fn from_iter<I: IntoIterator<Item = Operation<T>>>(ops: I) -> Self {
|
||||
let mut operations = Operations::default();
|
||||
@ -83,7 +83,7 @@ where
|
||||
|
||||
impl<T> Operations<T>
|
||||
where
|
||||
T: Attributes,
|
||||
T: OperationAttributes,
|
||||
{
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
@ -294,7 +294,7 @@ where
|
||||
|
||||
impl<T> OperationTransform for Operations<T>
|
||||
where
|
||||
T: Attributes,
|
||||
T: OperationAttributes,
|
||||
{
|
||||
fn compose(&self, other: &Self) -> Result<Self, OTError>
|
||||
where
|
||||
@ -509,7 +509,7 @@ where
|
||||
/// Removes trailing retain operation with empty attributes, if present.
|
||||
pub fn trim<T>(delta: &mut Operations<T>)
|
||||
where
|
||||
T: Attributes,
|
||||
T: OperationAttributes,
|
||||
{
|
||||
if let Some(last) = delta.ops.last() {
|
||||
if last.is_retain() && last.is_plain() {
|
||||
@ -518,7 +518,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
fn invert_other<T: Attributes>(
|
||||
fn invert_other<T: OperationAttributes>(
|
||||
base: &mut Operations<T>,
|
||||
other: &Operations<T>,
|
||||
operation: &Operation<T>,
|
||||
@ -547,7 +547,7 @@ fn invert_other<T: Attributes>(
|
||||
});
|
||||
}
|
||||
|
||||
fn transform_op_attribute<T: Attributes>(
|
||||
fn transform_op_attribute<T: OperationAttributes>(
|
||||
left: &Option<Operation<T>>,
|
||||
right: &Option<Operation<T>>,
|
||||
) -> Result<T, OTError> {
|
||||
@ -565,7 +565,7 @@ fn transform_op_attribute<T: Attributes>(
|
||||
|
||||
impl<T> Operations<T>
|
||||
where
|
||||
T: Attributes + DeserializeOwned,
|
||||
T: OperationAttributes + DeserializeOwned,
|
||||
{
|
||||
/// # Examples
|
||||
///
|
||||
@ -597,7 +597,7 @@ where
|
||||
|
||||
impl<T> Operations<T>
|
||||
where
|
||||
T: Attributes + serde::Serialize,
|
||||
T: OperationAttributes + serde::Serialize,
|
||||
{
|
||||
/// Serialize the [Delta] into a String in JSON format
|
||||
pub fn json_str(&self) -> String {
|
||||
@ -618,7 +618,7 @@ where
|
||||
|
||||
impl<T> FromStr for Operations<T>
|
||||
where
|
||||
T: Attributes,
|
||||
T: OperationAttributes,
|
||||
{
|
||||
type Err = ();
|
||||
|
||||
@ -631,7 +631,7 @@ where
|
||||
|
||||
impl<T> std::convert::TryFrom<Vec<u8>> for Operations<T>
|
||||
where
|
||||
T: Attributes + DeserializeOwned,
|
||||
T: OperationAttributes + DeserializeOwned,
|
||||
{
|
||||
type Error = OTError;
|
||||
fn try_from(bytes: Vec<u8>) -> Result<Self, Self::Error> {
|
||||
@ -641,7 +641,7 @@ where
|
||||
|
||||
impl<T> std::convert::TryFrom<Bytes> for Operations<T>
|
||||
where
|
||||
T: Attributes + DeserializeOwned,
|
||||
T: OperationAttributes + DeserializeOwned,
|
||||
{
|
||||
type Error = OTError;
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
use crate::core::delta::operation::Attributes;
|
||||
use crate::core::delta::operation::OperationAttributes;
|
||||
use crate::core::delta::Operations;
|
||||
use serde::{
|
||||
de::{SeqAccess, Visitor},
|
||||
@ -9,7 +9,7 @@ use std::{fmt, marker::PhantomData};
|
||||
|
||||
impl<T> Serialize for Operations<T>
|
||||
where
|
||||
T: Attributes + Serialize,
|
||||
T: OperationAttributes + Serialize,
|
||||
{
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
@ -25,7 +25,7 @@ where
|
||||
|
||||
impl<'de, T> Deserialize<'de> for Operations<T>
|
||||
where
|
||||
T: Attributes + Deserialize<'de>,
|
||||
T: OperationAttributes + Deserialize<'de>,
|
||||
{
|
||||
fn deserialize<D>(deserializer: D) -> Result<Operations<T>, D::Error>
|
||||
where
|
||||
@ -35,7 +35,7 @@ where
|
||||
|
||||
impl<'de, T> Visitor<'de> for OperationSeqVisitor<T>
|
||||
where
|
||||
T: Attributes + Deserialize<'de>,
|
||||
T: OperationAttributes + Deserialize<'de>,
|
||||
{
|
||||
type Value = Operations<T>;
|
||||
|
||||
|
@ -1,6 +1,5 @@
|
||||
#![allow(clippy::module_inception)]
|
||||
mod attributes;
|
||||
mod attributes_serde;
|
||||
|
||||
mod node;
|
||||
mod node_serde;
|
||||
mod node_tree;
|
||||
@ -9,7 +8,6 @@ mod operation_serde;
|
||||
mod path;
|
||||
mod transaction;
|
||||
|
||||
pub use attributes::*;
|
||||
pub use node::*;
|
||||
pub use node_tree::*;
|
||||
pub use operation::*;
|
||||
|
@ -1,6 +1,7 @@
|
||||
use super::node_serde::*;
|
||||
use crate::core::attributes::{AttributeKey, AttributeValue, Attributes};
|
||||
use crate::core::NodeBody::Delta;
|
||||
use crate::core::{AttributeKey, AttributeValue, NodeAttributes, OperationTransform};
|
||||
use crate::core::OperationTransform;
|
||||
use crate::errors::OTError;
|
||||
use crate::text_delta::TextDelta;
|
||||
use serde::{Deserialize, Serialize};
|
||||
@ -10,9 +11,9 @@ pub struct NodeData {
|
||||
#[serde(rename = "type")]
|
||||
pub node_type: String,
|
||||
|
||||
#[serde(skip_serializing_if = "NodeAttributes::is_empty")]
|
||||
#[serde(skip_serializing_if = "Attributes::is_empty")]
|
||||
#[serde(default)]
|
||||
pub attributes: NodeAttributes,
|
||||
pub attributes: Attributes,
|
||||
|
||||
#[serde(serialize_with = "serialize_body")]
|
||||
#[serde(deserialize_with = "deserialize_body")]
|
||||
@ -104,10 +105,7 @@ impl std::default::Default for NodeBody {
|
||||
|
||||
impl NodeBody {
|
||||
fn is_empty(&self) -> bool {
|
||||
match self {
|
||||
NodeBody::Empty => true,
|
||||
_ => false,
|
||||
}
|
||||
matches!(self, NodeBody::Empty)
|
||||
}
|
||||
}
|
||||
|
||||
@ -118,7 +116,7 @@ impl OperationTransform for NodeBody {
|
||||
Self: Sized,
|
||||
{
|
||||
match (self, other) {
|
||||
(Delta(a), Delta(b)) => a.compose(b).map(|delta| Delta(delta)),
|
||||
(Delta(a), Delta(b)) => a.compose(b).map(Delta),
|
||||
(NodeBody::Empty, NodeBody::Empty) => Ok(NodeBody::Empty),
|
||||
(l, r) => {
|
||||
let msg = format!("{:?} can not compose {:?}", l, r);
|
||||
@ -181,21 +179,21 @@ impl NodeBodyChangeset {
|
||||
pub struct Node {
|
||||
pub node_type: String,
|
||||
pub body: NodeBody,
|
||||
pub attributes: NodeAttributes,
|
||||
pub attributes: Attributes,
|
||||
}
|
||||
|
||||
impl Node {
|
||||
pub fn new(node_type: &str) -> Node {
|
||||
Node {
|
||||
node_type: node_type.into(),
|
||||
attributes: NodeAttributes::new(),
|
||||
attributes: Attributes::new(),
|
||||
body: NodeBody::Empty,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn apply_body_changeset(&mut self, changeset: NodeBodyChangeset) {
|
||||
match changeset {
|
||||
NodeBodyChangeset::Delta { delta, inverted: _ } => match self.body.compose(&Delta(delta.clone())) {
|
||||
NodeBodyChangeset::Delta { delta, inverted: _ } => match self.body.compose(&Delta(delta)) {
|
||||
Ok(new_body) => self.body = new_body,
|
||||
Err(e) => tracing::error!("{:?}", e),
|
||||
},
|
||||
|
@ -64,8 +64,8 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
if delta.is_some() {
|
||||
return Ok(NodeBody::Delta(delta.unwrap()));
|
||||
if let Some(delta) = delta {
|
||||
return Ok(NodeBody::Delta(delta));
|
||||
}
|
||||
|
||||
Err(de::Error::missing_field("delta"))
|
||||
|
@ -1,5 +1,6 @@
|
||||
use crate::core::attributes::Attributes;
|
||||
use crate::core::document::path::Path;
|
||||
use crate::core::{Node, NodeAttributes, NodeBodyChangeset, NodeData, NodeOperation, OperationTransform, Transaction};
|
||||
use crate::core::{Node, NodeBodyChangeset, NodeData, NodeOperation, OperationTransform, Transaction};
|
||||
use crate::errors::{ErrorBuilder, OTError, OTErrorCode};
|
||||
use indextree::{Arena, Children, FollowingSiblings, NodeId};
|
||||
|
||||
@ -81,8 +82,8 @@ impl NodeTree {
|
||||
let mut path = vec![];
|
||||
let mut current_node = node_id;
|
||||
// Use .skip(1) on the ancestors iterator to skip the root node.
|
||||
let mut ancestors = node_id.ancestors(&self.arena).skip(1);
|
||||
while let Some(parent_node) = ancestors.next() {
|
||||
let ancestors = node_id.ancestors(&self.arena).skip(1);
|
||||
for parent_node in ancestors {
|
||||
let counter = self.index_of_node(parent_node, current_node);
|
||||
path.push(counter);
|
||||
current_node = parent_node;
|
||||
@ -93,8 +94,8 @@ impl NodeTree {
|
||||
|
||||
fn index_of_node(&self, parent_node: NodeId, child_node: NodeId) -> usize {
|
||||
let mut counter: usize = 0;
|
||||
let mut iter = parent_node.children(&self.arena);
|
||||
while let Some(node) = iter.next() {
|
||||
let iter = parent_node.children(&self.arena);
|
||||
for node in iter {
|
||||
if node == child_node {
|
||||
return counter;
|
||||
}
|
||||
@ -198,7 +199,7 @@ impl NodeTree {
|
||||
fn insert_nodes_before(&mut self, node_id: &NodeId, nodes: Vec<NodeData>) {
|
||||
for node in nodes {
|
||||
let (node, children) = node.split();
|
||||
let new_node_id = self.arena.new_node(node.into());
|
||||
let new_node_id = self.arena.new_node(node);
|
||||
if node_id.is_removed(&self.arena) {
|
||||
tracing::warn!("Node:{:?} is remove before insert", node_id);
|
||||
return;
|
||||
@ -231,16 +232,16 @@ impl NodeTree {
|
||||
fn append_nodes(&mut self, parent: &NodeId, nodes: Vec<NodeData>) {
|
||||
for node in nodes {
|
||||
let (node, children) = node.split();
|
||||
let new_node_id = self.arena.new_node(node.into());
|
||||
let new_node_id = self.arena.new_node(node);
|
||||
parent.append(new_node_id, &mut self.arena);
|
||||
|
||||
self.append_nodes(&new_node_id, children);
|
||||
}
|
||||
}
|
||||
|
||||
fn update_attributes(&mut self, path: &Path, attributes: NodeAttributes) -> Result<(), OTError> {
|
||||
fn update_attributes(&mut self, path: &Path, attributes: Attributes) -> Result<(), OTError> {
|
||||
self.mut_node_at_path(path, |node| {
|
||||
let new_attributes = NodeAttributes::compose(&node.attributes, &attributes)?;
|
||||
let new_attributes = Attributes::compose(&node.attributes, &attributes)?;
|
||||
node.attributes = new_attributes;
|
||||
Ok(())
|
||||
})
|
||||
|
@ -1,5 +1,6 @@
|
||||
use crate::core::attributes::Attributes;
|
||||
use crate::core::document::path::Path;
|
||||
use crate::core::{NodeAttributes, NodeBodyChangeset, NodeData};
|
||||
use crate::core::{NodeBodyChangeset, NodeData};
|
||||
use crate::errors::OTError;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
@ -12,9 +13,9 @@ pub enum NodeOperation {
|
||||
#[serde(rename = "update")]
|
||||
UpdateAttributes {
|
||||
path: Path,
|
||||
attributes: NodeAttributes,
|
||||
attributes: Attributes,
|
||||
#[serde(rename = "oldAttributes")]
|
||||
old_attributes: NodeAttributes,
|
||||
old_attributes: Attributes,
|
||||
},
|
||||
|
||||
#[serde(rename = "update-body")]
|
||||
|
@ -11,21 +11,21 @@ impl std::ops::Deref for Path {
|
||||
}
|
||||
}
|
||||
|
||||
impl std::convert::Into<Path> for usize {
|
||||
fn into(self) -> Path {
|
||||
Path(vec![self])
|
||||
impl std::convert::From<usize> for Path {
|
||||
fn from(val: usize) -> Self {
|
||||
Path(vec![val])
|
||||
}
|
||||
}
|
||||
|
||||
impl std::convert::Into<Path> for &usize {
|
||||
fn into(self) -> Path {
|
||||
Path(vec![*self])
|
||||
impl std::convert::From<&usize> for Path {
|
||||
fn from(val: &usize) -> Self {
|
||||
Path(vec![*val])
|
||||
}
|
||||
}
|
||||
|
||||
impl std::convert::Into<Path> for &Path {
|
||||
fn into(self) -> Path {
|
||||
self.clone()
|
||||
impl std::convert::From<&Path> for Path {
|
||||
fn from(path: &Path) -> Self {
|
||||
path.clone()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
use crate::core::attributes::Attributes;
|
||||
use crate::core::document::path::Path;
|
||||
use crate::core::{NodeAttributes, NodeData, NodeOperation, NodeTree};
|
||||
use crate::core::{NodeData, NodeOperation, NodeTree};
|
||||
use indextree::NodeId;
|
||||
|
||||
use super::{NodeBodyChangeset, NodeOperationList};
|
||||
@ -100,10 +101,10 @@ impl<'a> TransactionBuilder<'a> {
|
||||
self.insert_nodes_at_path(path, vec![node])
|
||||
}
|
||||
|
||||
pub fn update_attributes_at_path(mut self, path: &Path, attributes: NodeAttributes) -> Self {
|
||||
pub fn update_attributes_at_path(mut self, path: &Path, attributes: Attributes) -> Self {
|
||||
match self.node_tree.get_node_at_path(path) {
|
||||
Some(node) => {
|
||||
let mut old_attributes = NodeAttributes::new();
|
||||
let mut old_attributes = Attributes::new();
|
||||
for key in attributes.keys() {
|
||||
let old_attrs = &node.attributes;
|
||||
if let Some(value) = old_attrs.get(key.as_str()) {
|
||||
|
@ -1,8 +1,10 @@
|
||||
pub mod attributes;
|
||||
mod delta;
|
||||
mod document;
|
||||
mod interval;
|
||||
mod ot_str;
|
||||
|
||||
pub use attributes::*;
|
||||
pub use delta::operation::*;
|
||||
pub use delta::*;
|
||||
pub use document::*;
|
||||
|
@ -2,11 +2,11 @@
|
||||
#![allow(clippy::derivable_impls)]
|
||||
use crate::text_delta::{TextAttribute, TextAttributes};
|
||||
|
||||
pub struct AttributeBuilder {
|
||||
pub struct TextDeltaAttributeBuilder {
|
||||
inner: TextAttributes,
|
||||
}
|
||||
|
||||
impl std::default::Default for AttributeBuilder {
|
||||
impl std::default::Default for TextDeltaAttributeBuilder {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
inner: TextAttributes::default(),
|
||||
@ -14,9 +14,9 @@ impl std::default::Default for AttributeBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
impl AttributeBuilder {
|
||||
impl TextDeltaAttributeBuilder {
|
||||
pub fn new() -> Self {
|
||||
AttributeBuilder::default()
|
||||
TextDeltaAttributeBuilder::default()
|
||||
}
|
||||
|
||||
pub fn add_attr(mut self, attribute: TextAttribute) -> Self {
|
||||
|
@ -1,5 +1,5 @@
|
||||
#![allow(non_snake_case)]
|
||||
use crate::core::{Attributes, Operation, OperationTransform};
|
||||
use crate::core::{Operation, OperationAttributes, OperationTransform};
|
||||
use crate::{block_attribute, errors::OTError, ignore_attribute, inline_attribute, list_attribute};
|
||||
use lazy_static::lazy_static;
|
||||
use std::{
|
||||
@ -97,7 +97,7 @@ impl TextAttributes {
|
||||
}
|
||||
}
|
||||
|
||||
impl Attributes for TextAttributes {
|
||||
impl OperationAttributes for TextAttributes {
|
||||
fn is_empty(&self) -> bool {
|
||||
self.inner.is_empty()
|
||||
}
|
||||
|
@ -4,8 +4,8 @@ mod attributes_serde;
|
||||
|
||||
#[macro_use]
|
||||
mod macros;
|
||||
mod text_delta;
|
||||
mod delta;
|
||||
|
||||
pub use attribute_builder::*;
|
||||
pub use attributes::*;
|
||||
pub use text_delta::*;
|
||||
pub use delta::*;
|
||||
|
@ -1,7 +1,7 @@
|
||||
use super::script::{NodeScript::*, *};
|
||||
use lib_ot::{
|
||||
core::{NodeData, Path},
|
||||
text_delta::{AttributeBuilder, TextAttribute, TextAttributes, TextDeltaBuilder},
|
||||
text_delta::{TextAttribute, TextDeltaAttributeBuilder, TextDeltaBuilder},
|
||||
};
|
||||
|
||||
#[test]
|
||||
@ -14,17 +14,21 @@ fn editor_deserialize_node_test() {
|
||||
.insert("👋 ")
|
||||
.insert_with_attributes(
|
||||
"Welcome to ",
|
||||
AttributeBuilder::new().add_attr(TextAttribute::Bold(true)).build(),
|
||||
TextDeltaAttributeBuilder::new()
|
||||
.add_attr(TextAttribute::Bold(true))
|
||||
.build(),
|
||||
)
|
||||
.insert_with_attributes(
|
||||
"AppFlowy Editor",
|
||||
AttributeBuilder::new().add_attr(TextAttribute::Italic(true)).build(),
|
||||
TextDeltaAttributeBuilder::new()
|
||||
.add_attr(TextAttribute::Italic(true))
|
||||
.build(),
|
||||
)
|
||||
.build();
|
||||
|
||||
test.run_scripts(vec![
|
||||
InsertNode {
|
||||
path: path.clone(),
|
||||
path,
|
||||
node: node.clone(),
|
||||
},
|
||||
AssertNumberOfNodesAtPath { path: None, len: 1 },
|
||||
|
@ -1,5 +1,6 @@
|
||||
use lib_ot::core::AttributeBuilder;
|
||||
use lib_ot::{
|
||||
core::{NodeAttributeBuilder, NodeBodyChangeset, NodeData, NodeDataBuilder, NodeOperation, Path},
|
||||
core::{NodeBodyChangeset, NodeData, NodeDataBuilder, NodeOperation, Path},
|
||||
text_delta::TextDeltaBuilder,
|
||||
};
|
||||
|
||||
@ -32,8 +33,8 @@ fn operation_insert_node_with_children_serde_test() {
|
||||
fn operation_update_node_attributes_serde_test() {
|
||||
let operation = NodeOperation::UpdateAttributes {
|
||||
path: Path(vec![0, 1]),
|
||||
attributes: NodeAttributeBuilder::new().insert("bold", true).build(),
|
||||
old_attributes: NodeAttributeBuilder::new().insert("bold", false).build(),
|
||||
attributes: AttributeBuilder::new().insert("bold", true).build(),
|
||||
old_attributes: AttributeBuilder::new().insert("bold", false).build(),
|
||||
};
|
||||
|
||||
let result = serde_json::to_string(&operation).unwrap();
|
||||
|
@ -1,11 +1,12 @@
|
||||
use lib_ot::{
|
||||
core::{NodeAttributes, NodeBody, NodeBodyChangeset, NodeData, NodeTree, Path, TransactionBuilder},
|
||||
core::attributes::Attributes,
|
||||
core::{NodeBody, NodeBodyChangeset, NodeData, NodeTree, Path, TransactionBuilder},
|
||||
text_delta::TextDelta,
|
||||
};
|
||||
|
||||
pub enum NodeScript {
|
||||
InsertNode { path: Path, node: NodeData },
|
||||
UpdateAttributes { path: Path, attributes: NodeAttributes },
|
||||
UpdateAttributes { path: Path, attributes: Attributes },
|
||||
UpdateBody { path: Path, changeset: NodeBodyChangeset },
|
||||
DeleteNode { path: Path },
|
||||
AssertNumberOfNodesAtPath { path: Option<Path>, len: usize },
|
||||
@ -65,7 +66,7 @@ impl NodeTest {
|
||||
None => assert!(node_id.is_none()),
|
||||
Some(node_id) => {
|
||||
let node_data = self.node_tree.get_node(node_id).cloned();
|
||||
assert_eq!(node_data, expected.and_then(|e| Some(e.into())));
|
||||
assert_eq!(node_data, expected.map(|e| e.into()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -172,7 +172,7 @@ fn node_delete_test() {
|
||||
let scripts = vec![
|
||||
InsertNode {
|
||||
path: path.clone(),
|
||||
node: inserted_node.clone(),
|
||||
node: inserted_node,
|
||||
},
|
||||
DeleteNode { path: path.clone() },
|
||||
AssertNode { path, expected: None },
|
||||
@ -198,7 +198,7 @@ fn node_update_body_test() {
|
||||
let scripts = vec![
|
||||
InsertNode {
|
||||
path: path.clone(),
|
||||
node: node.clone(),
|
||||
node,
|
||||
},
|
||||
UpdateBody {
|
||||
path: path.clone(),
|
||||
|
Loading…
Reference in New Issue
Block a user