chore: rename some function & add more documentation

This commit is contained in:
appflowy 2022-07-24 18:10:29 +08:00
parent 3c5b9e6b50
commit dd71acf843
32 changed files with 139 additions and 127 deletions

View File

@ -215,7 +215,7 @@ impl DefaultFolderBuilder {
for app in workspace_rev.apps.iter() {
for (index, view) in app.belongings.iter().enumerate() {
let view_data = if index == 0 {
initial_read_me().to_delta_str()
initial_read_me().to_json_str()
} else {
initial_quill_delta_string()
};

View File

@ -10,7 +10,7 @@ use flowy_sync::{
entities::{revision::Revision, ws_data::ServerRevisionWSData},
};
use lib_infra::future::FutureResult;
use lib_ot::core::PlainTextAttributes;
use lib_ot::core::PhantomAttributes;
use parking_lot::RwLock;
use std::sync::Arc;
@ -80,7 +80,7 @@ impl FolderEditor {
pub(crate) fn apply_change(&self, change: FolderChange) -> FlowyResult<()> {
let FolderChange { delta, md5 } = change;
let (base_rev_id, rev_id) = self.rev_manager.next_rev_id_pair();
let delta_data = delta.to_delta_bytes();
let delta_data = delta.to_json_bytes();
let revision = Revision::new(
&self.rev_manager.object_id,
base_rev_id,
@ -132,7 +132,7 @@ impl FolderEditor {
pub struct FolderRevisionCompactor();
impl RevisionCompactor for FolderRevisionCompactor {
fn bytes_from_revisions(&self, revisions: Vec<Revision>) -> FlowyResult<Bytes> {
let delta = make_delta_from_revisions::<PlainTextAttributes>(revisions)?;
Ok(delta.to_delta_bytes())
let delta = make_delta_from_revisions::<PhantomAttributes>(revisions)?;
Ok(delta.to_json_bytes())
}
}

View File

@ -110,7 +110,7 @@ impl FolderPersistence {
pub async fn save_folder(&self, user_id: &str, folder_id: &FolderId, folder: FolderPad) -> FlowyResult<()> {
let pool = self.database.db_pool()?;
let json = folder.to_json()?;
let delta_data = PlainTextDeltaBuilder::new().insert(&json).build().to_delta_bytes();
let delta_data = PlainTextDeltaBuilder::new().insert(&json).build().to_json_bytes();
let revision = Revision::initial_revision(user_id, folder_id.as_ref(), delta_data);
let record = RevisionRecord {
revision,

View File

@ -10,7 +10,7 @@ use flowy_sync::{
},
};
use lib_infra::future::{BoxResultFuture, FutureResult};
use lib_ot::core::{OperationTransformable, PlainTextAttributes, PlainTextDelta};
use lib_ot::core::{OperationTransformable, PhantomAttributes, PlainTextDelta};
use parking_lot::RwLock;
use std::{sync::Arc, time::Duration};
@ -24,7 +24,7 @@ pub(crate) async fn make_folder_ws_manager(
) -> Arc<RevisionWebSocketManager> {
let ws_data_provider = Arc::new(WSDataProvider::new(folder_id, Arc::new(rev_manager.clone())));
let resolver = Arc::new(FolderConflictResolver { folder_pad });
let conflict_controller = ConflictController::<PlainTextAttributes>::new(
let conflict_controller = ConflictController::<PhantomAttributes>::new(
user_id,
resolver,
Arc::new(ws_data_provider.clone()),
@ -55,7 +55,7 @@ struct FolderConflictResolver {
folder_pad: Arc<RwLock<FolderPad>>,
}
impl ConflictResolver<PlainTextAttributes> for FolderConflictResolver {
impl ConflictResolver<PhantomAttributes> for FolderConflictResolver {
fn compose_delta(&self, delta: PlainTextDelta) -> BoxResultFuture<DeltaMD5, FlowyError> {
let folder_pad = self.folder_pad.clone();
Box::pin(async move {
@ -67,7 +67,7 @@ impl ConflictResolver<PlainTextAttributes> for FolderConflictResolver {
fn transform_delta(
&self,
delta: PlainTextDelta,
) -> BoxResultFuture<TransformDeltas<PlainTextAttributes>, FlowyError> {
) -> BoxResultFuture<TransformDeltas<PhantomAttributes>, FlowyError> {
let folder_pad = self.folder_pad.clone();
Box::pin(async move {
let read_guard = folder_pad.read();

View File

@ -192,7 +192,7 @@ pub async fn make_grid_view_data(
// Create grid's block
let grid_block_delta = make_grid_block_delta(block_meta_data);
let block_delta_data = grid_block_delta.to_delta_bytes();
let block_delta_data = grid_block_delta.to_json_bytes();
let repeated_revision: RepeatedRevision =
Revision::initial_revision(user_id, block_id, block_delta_data).into();
let _ = grid_manager.create_grid_block(&block_id, repeated_revision).await?;
@ -202,7 +202,7 @@ pub async fn make_grid_view_data(
// Create grid
let grid_meta_delta = make_grid_delta(&grid_rev);
let grid_delta_data = grid_meta_delta.to_delta_bytes();
let grid_delta_data = grid_meta_delta.to_json_bytes();
let repeated_revision: RepeatedRevision =
Revision::initial_revision(user_id, view_id, grid_delta_data.clone()).into();
let _ = grid_manager.create_grid(view_id, repeated_revision).await?;

View File

@ -7,7 +7,7 @@ use flowy_sync::client_grid::{GridBlockMetaChange, GridBlockRevisionPad};
use flowy_sync::entities::revision::Revision;
use flowy_sync::util::make_delta_from_revisions;
use lib_infra::future::FutureResult;
use lib_ot::core::PlainTextAttributes;
use lib_ot::core::PhantomAttributes;
use std::borrow::Cow;
use std::sync::Arc;
use tokio::sync::RwLock;
@ -161,7 +161,7 @@ impl GridBlockRevisionEditor {
let GridBlockMetaChange { delta, md5 } = change;
let user_id = self.user_id.clone();
let (base_rev_id, rev_id) = self.rev_manager.next_rev_id_pair();
let delta_data = delta.to_delta_bytes();
let delta_data = delta.to_json_bytes();
let revision = Revision::new(
&self.rev_manager.object_id,
base_rev_id,
@ -200,7 +200,7 @@ impl RevisionObjectBuilder for GridBlockMetaPadBuilder {
pub struct GridBlockRevisionCompactor();
impl RevisionCompactor for GridBlockRevisionCompactor {
fn bytes_from_revisions(&self, revisions: Vec<Revision>) -> FlowyResult<Bytes> {
let delta = make_delta_from_revisions::<PlainTextAttributes>(revisions)?;
Ok(delta.to_delta_bytes())
let delta = make_delta_from_revisions::<PhantomAttributes>(revisions)?;
Ok(delta.to_json_bytes())
}
}

View File

@ -21,7 +21,7 @@ use flowy_sync::entities::revision::Revision;
use flowy_sync::errors::CollaborateResult;
use flowy_sync::util::make_delta_from_revisions;
use lib_infra::future::FutureResult;
use lib_ot::core::PlainTextAttributes;
use lib_ot::core::PhantomAttributes;
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;
@ -573,7 +573,7 @@ impl GridRevisionEditor {
let GridChangeset { delta, md5 } = change;
let user_id = self.user.user_id()?;
let (base_rev_id, rev_id) = self.rev_manager.next_rev_id_pair();
let delta_data = delta.to_delta_bytes();
let delta_data = delta.to_json_bytes();
let revision = Revision::new(
&self.rev_manager.object_id,
base_rev_id,
@ -664,8 +664,8 @@ impl RevisionCloudService for GridRevisionCloudService {
pub struct GridRevisionCompactor();
impl RevisionCompactor for GridRevisionCompactor {
fn bytes_from_revisions(&self, revisions: Vec<Revision>) -> FlowyResult<Bytes> {
let delta = make_delta_from_revisions::<PlainTextAttributes>(revisions)?;
Ok(delta.to_delta_bytes())
let delta = make_delta_from_revisions::<PhantomAttributes>(revisions)?;
Ok(delta.to_json_bytes())
}
}

View File

@ -48,7 +48,7 @@ impl GridMigration {
let pool = self.database.db_pool()?;
let grid_rev_pad = self.get_grid_revision_pad(grid_id).await?;
let json = grid_rev_pad.json_str()?;
let delta_data = PlainTextDeltaBuilder::new().insert(&json).build().to_delta_bytes();
let delta_data = PlainTextDeltaBuilder::new().insert(&json).build().to_json_bytes();
let revision = Revision::initial_revision(&user_id, grid_id, delta_data);
let record = RevisionRecord::new(revision);
//

View File

@ -9,7 +9,7 @@ use flowy_sync::{
util::make_delta_from_revisions,
};
use lib_infra::future::BoxResultFuture;
use lib_ot::core::{Attributes, Delta, PlainTextAttributes};
use lib_ot::core::{Attributes, Delta, PhantomAttributes};
use lib_ot::rich_text::RichTextAttributes;
use serde::de::DeserializeOwned;
use std::{convert::TryFrom, sync::Arc};
@ -31,7 +31,7 @@ pub trait ConflictRevisionSink: Send + Sync + 'static {
}
pub type RichTextConflictController = ConflictController<RichTextAttributes>;
pub type PlainTextConflictController = ConflictController<PlainTextAttributes>;
pub type PlainTextConflictController = ConflictController<PhantomAttributes>;
pub struct ConflictController<T>
where
@ -154,7 +154,7 @@ where
&rev_manager.object_id,
base_rev_id,
rev_id,
client_delta.to_delta_bytes(),
client_delta.to_json_bytes(),
user_id,
md5.clone(),
);
@ -166,7 +166,7 @@ where
&rev_manager.object_id,
base_rev_id,
rev_id,
server_delta.to_delta_bytes(),
server_delta.to_json_bytes(),
user_id,
md5,
);

View File

@ -238,7 +238,7 @@ impl RevisionObjectBuilder for TextBlockInfoBuilder {
Result::<DocumentPB, FlowyError>::Ok(DocumentPB {
block_id: object_id.to_owned(),
text: delta.to_delta_str(),
text: delta.to_json_str(),
rev_id,
base_rev_id,
})

View File

@ -175,7 +175,7 @@ impl EditBlockQueue {
}
async fn save_local_delta(&self, delta: RichTextDelta, md5: String) -> Result<RevId, FlowyError> {
let delta_data = delta.to_delta_bytes();
let delta_data = delta.to_json_bytes();
let (base_rev_id, rev_id) = self.rev_manager.next_rev_id_pair();
let user_id = self.user.user_id()?;
let revision = Revision::new(
@ -195,7 +195,7 @@ pub(crate) struct TextBlockRevisionCompactor();
impl RevisionCompactor for TextBlockRevisionCompactor {
fn bytes_from_revisions(&self, revisions: Vec<Revision>) -> FlowyResult<Bytes> {
let delta = make_delta_from_revisions::<RichTextAttributes>(revisions)?;
Ok(delta.to_delta_bytes())
Ok(delta.to_json_bytes())
}
}

View File

@ -75,7 +75,7 @@ impl TextBlockEditorTest {
let delta = self.editor.text_block_delta().await.unwrap();
if expected_delta != delta {
eprintln!("✅ expect: {}", expected,);
eprintln!("❌ receive: {}", delta.to_delta_str());
eprintln!("❌ receive: {}", delta.to_json_str());
}
assert_eq!(expected_delta, delta);
}

View File

@ -762,19 +762,19 @@ fn attributes_preserve_list_format_on_merge() {
#[test]
fn delta_compose() {
let mut delta = RichTextDelta::from_delta_str(r#"[{"insert":"\n"}]"#).unwrap();
let mut delta = RichTextDelta::from_json_str(r#"[{"insert":"\n"}]"#).unwrap();
let deltas = vec![
RichTextDelta::from_delta_str(r#"[{"retain":1,"attributes":{"list":"unchecked"}}]"#).unwrap(),
RichTextDelta::from_delta_str(r#"[{"insert":"a"}]"#).unwrap(),
RichTextDelta::from_delta_str(r#"[{"retain":1},{"insert":"\n","attributes":{"list":"unchecked"}}]"#).unwrap(),
RichTextDelta::from_delta_str(r#"[{"retain":2},{"retain":1,"attributes":{"list":""}}]"#).unwrap(),
RichTextDelta::from_json_str(r#"[{"retain":1,"attributes":{"list":"unchecked"}}]"#).unwrap(),
RichTextDelta::from_json_str(r#"[{"insert":"a"}]"#).unwrap(),
RichTextDelta::from_json_str(r#"[{"retain":1},{"insert":"\n","attributes":{"list":"unchecked"}}]"#).unwrap(),
RichTextDelta::from_json_str(r#"[{"retain":2},{"retain":1,"attributes":{"list":""}}]"#).unwrap(),
];
for d in deltas {
delta = delta.compose(&d).unwrap();
}
assert_eq!(
delta.to_delta_str(),
delta.to_json_str(),
r#"[{"insert":"a"},{"insert":"\n","attributes":{"list":"unchecked"}},{"insert":"\n"}]"#
);

View File

@ -108,20 +108,20 @@ impl TestBuilder {
TestOp::Insert(delta_i, s, index) => {
let document = &mut self.documents[*delta_i];
let delta = document.insert(*index, s).unwrap();
tracing::debug!("Insert delta: {}", delta.to_delta_str());
tracing::debug!("Insert delta: {}", delta.to_json_str());
self.deltas.insert(*delta_i, Some(delta));
}
TestOp::Delete(delta_i, iv) => {
let document = &mut self.documents[*delta_i];
let delta = document.replace(*iv, "").unwrap();
tracing::trace!("Delete delta: {}", delta.to_delta_str());
tracing::trace!("Delete delta: {}", delta.to_json_str());
self.deltas.insert(*delta_i, Some(delta));
}
TestOp::Replace(delta_i, iv, s) => {
let document = &mut self.documents[*delta_i];
let delta = document.replace(*iv, s).unwrap();
tracing::trace!("Replace delta: {}", delta.to_delta_str());
tracing::trace!("Replace delta: {}", delta.to_json_str());
self.deltas.insert(*delta_i, Some(delta));
}
TestOp::InsertBold(delta_i, s, iv) => {
@ -133,7 +133,7 @@ impl TestBuilder {
let document = &mut self.documents[*delta_i];
let attribute = RichTextAttribute::Bold(*enable);
let delta = document.format(*iv, attribute).unwrap();
tracing::trace!("Bold delta: {}", delta.to_delta_str());
tracing::trace!("Bold delta: {}", delta.to_json_str());
self.deltas.insert(*delta_i, Some(delta));
}
TestOp::Italic(delta_i, iv, enable) => {
@ -143,28 +143,28 @@ impl TestBuilder {
false => RichTextAttribute::Italic(false),
};
let delta = document.format(*iv, attribute).unwrap();
tracing::trace!("Italic delta: {}", delta.to_delta_str());
tracing::trace!("Italic delta: {}", delta.to_json_str());
self.deltas.insert(*delta_i, Some(delta));
}
TestOp::Header(delta_i, iv, level) => {
let document = &mut self.documents[*delta_i];
let attribute = RichTextAttribute::Header(*level);
let delta = document.format(*iv, attribute).unwrap();
tracing::trace!("Header delta: {}", delta.to_delta_str());
tracing::trace!("Header delta: {}", delta.to_json_str());
self.deltas.insert(*delta_i, Some(delta));
}
TestOp::Link(delta_i, iv, link) => {
let document = &mut self.documents[*delta_i];
let attribute = RichTextAttribute::Link(link.to_owned());
let delta = document.format(*iv, attribute).unwrap();
tracing::trace!("Link delta: {}", delta.to_delta_str());
tracing::trace!("Link delta: {}", delta.to_json_str());
self.deltas.insert(*delta_i, Some(delta));
}
TestOp::Bullet(delta_i, iv, enable) => {
let document = &mut self.documents[*delta_i];
let attribute = RichTextAttribute::Bullet(*enable);
let delta = document.format(*iv, attribute).unwrap();
tracing::debug!("Bullet delta: {}", delta.to_delta_str());
tracing::debug!("Bullet delta: {}", delta.to_json_str());
self.deltas.insert(*delta_i, Some(delta));
}
@ -194,15 +194,15 @@ impl TestBuilder {
let delta_a = &self.documents[*delta_a_i].delta();
let delta_b = &self.documents[*delta_b_i].delta();
tracing::debug!("Invert: ");
tracing::debug!("a: {}", delta_a.to_delta_str());
tracing::debug!("b: {}", delta_b.to_delta_str());
tracing::debug!("a: {}", delta_a.to_json_str());
tracing::debug!("b: {}", delta_b.to_json_str());
let (_, b_prime) = delta_a.transform(delta_b).unwrap();
let undo = b_prime.invert(delta_a);
let new_delta = delta_a.compose(&b_prime).unwrap();
tracing::debug!("new delta: {}", new_delta.to_delta_str());
tracing::debug!("undo delta: {}", undo.to_delta_str());
tracing::debug!("new delta: {}", new_delta.to_json_str());
tracing::debug!("undo delta: {}", undo.to_json_str());
let new_delta_after_undo = new_delta.compose(&undo).unwrap();
@ -238,7 +238,7 @@ impl TestBuilder {
}
TestOp::AssertPrimeJson(doc_i, expected) => {
let prime_json = self.primes[*doc_i].as_ref().unwrap().to_delta_str();
let prime_json = self.primes[*doc_i].as_ref().unwrap().to_json_str();
let expected_prime: RichTextDelta = serde_json::from_str(expected).unwrap();
let target_prime: RichTextDelta = serde_json::from_str(&prime_json).unwrap();

View File

@ -378,7 +378,7 @@ fn apply_test() {
.insert(", AppFlowy")
.build();
let after_a = delta_a.to_str().unwrap();
let after_a = delta_a.content_str().unwrap();
let after_b = delta_b.apply(&after_a).unwrap();
assert_eq!("hello, AppFlowy", &after_b);
}

View File

@ -65,7 +65,7 @@ fn delta_serialize_multi_attribute_test() {
let json = serde_json::to_string(&delta).unwrap();
eprintln!("{}", json);
let delta_from_json = Delta::from_delta_str(&json).unwrap();
let delta_from_json = Delta::from_json_str(&json).unwrap();
assert_eq!(delta_from_json, delta);
}
@ -77,7 +77,7 @@ fn delta_deserialize_test() {
{"retain":2,"attributes":{"italic":"true","bold":"true"}},
{"retain":2,"attributes":{"italic":true,"bold":true}}
]"#;
let delta = RichTextDelta::from_delta_str(json).unwrap();
let delta = RichTextDelta::from_json_str(json).unwrap();
eprintln!("{}", delta);
}
@ -86,13 +86,13 @@ fn delta_deserialize_null_test() {
let json = r#"[
{"retain":7,"attributes":{"bold":null}}
]"#;
let delta1 = RichTextDelta::from_delta_str(json).unwrap();
let delta1 = RichTextDelta::from_json_str(json).unwrap();
let mut attribute = RichTextAttribute::Bold(true);
attribute.value = RichTextAttributeValue(None);
let delta2 = DeltaBuilder::new().retain_with_attributes(7, attribute.into()).build();
assert_eq!(delta2.to_delta_str(), r#"[{"retain":7,"attributes":{"bold":""}}]"#);
assert_eq!(delta2.to_json_str(), r#"[{"retain":7,"attributes":{"bold":""}}]"#);
assert_eq!(delta1, delta2);
}

View File

@ -7,13 +7,13 @@ pub fn initial_quill_delta() -> RichTextDelta {
#[inline]
pub fn initial_quill_delta_string() -> String {
initial_quill_delta().to_delta_str()
initial_quill_delta().to_json_str()
}
#[inline]
pub fn initial_read_me() -> RichTextDelta {
let json = include_str!("READ_ME.json");
RichTextDelta::from_delta_str(json).unwrap()
RichTextDelta::from_json_str(json).unwrap()
}
#[cfg(test)]
@ -22,6 +22,6 @@ mod tests {
#[test]
fn load_read_me() {
println!("{}", initial_read_me().to_delta_str());
println!("{}", initial_read_me().to_json_str());
}
}

View File

@ -55,16 +55,16 @@ impl ClientDocument {
}
pub fn from_json(json: &str) -> Result<Self, CollaborateError> {
let delta = RichTextDelta::from_delta_str(json)?;
let delta = RichTextDelta::from_json_str(json)?;
Ok(Self::from_delta(delta))
}
pub fn delta_str(&self) -> String {
self.delta.to_delta_str()
self.delta.to_json_str()
}
pub fn to_bytes(&self) -> Bytes {
self.delta.to_delta_bytes()
self.delta.to_json_bytes()
}
pub fn to_plain_string(&self) -> String {
@ -85,7 +85,7 @@ impl ClientDocument {
}
pub fn set_delta(&mut self, data: RichTextDelta) {
tracing::trace!("document: {}", data.to_delta_str());
tracing::trace!("document: {}", data.to_json_str());
self.delta = data;
match &self.notify {
@ -97,7 +97,7 @@ impl ClientDocument {
}
pub fn compose_delta(&mut self, delta: RichTextDelta) -> Result<(), CollaborateError> {
tracing::trace!("{} compose {}", &self.delta.to_delta_str(), delta.to_delta_str());
tracing::trace!("{} compose {}", &self.delta.to_json_str(), delta.to_json_str());
let composed_delta = self.delta.compose(&delta)?;
let mut undo_delta = delta.invert(&self.delta);

View File

@ -7,7 +7,7 @@ use crate::{
};
use flowy_folder_data_model::revision::{TrashRevision, WorkspaceRevision};
use lib_ot::core::{PlainTextAttributes, PlainTextDelta, PlainTextDeltaBuilder};
use lib_ot::core::{PhantomAttributes, PlainTextDelta, PlainTextDeltaBuilder};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
@ -41,9 +41,9 @@ impl FolderPadBuilder {
}
// TODO: Reconvert from history if delta.to_str() failed.
let folder_json = delta.to_str()?;
let mut folder: FolderPad = serde_json::from_str(&folder_json).map_err(|e| {
tracing::error!("Deserialize folder from json failed: {}", folder_json);
let content = delta.content_str()?;
let mut folder: FolderPad = serde_json::from_str(&content).map_err(|e| {
tracing::error!("Deserialize folder from {} failed", content);
return CollaborateError::internal().context(format!("Deserialize delta to folder failed: {}", e));
})?;
folder.delta = delta;
@ -51,7 +51,7 @@ impl FolderPadBuilder {
}
pub(crate) fn build_with_revisions(self, revisions: Vec<Revision>) -> CollaborateResult<FolderPad> {
let folder_delta: FolderDelta = make_delta_from_revisions::<PlainTextAttributes>(revisions)?;
let folder_delta: FolderDelta = make_delta_from_revisions::<PhantomAttributes>(revisions)?;
self.build_with_delta(folder_delta)
}

View File

@ -295,7 +295,7 @@ impl FolderPad {
}
pub fn md5(&self) -> String {
md5(&self.delta.to_delta_bytes())
md5(&self.delta.to_json_bytes())
}
pub fn to_json(&self) -> CollaborateResult<String> {
@ -315,7 +315,7 @@ impl FolderPad {
Some(_) => {
let old = cloned_self.to_json()?;
let new = self.to_json()?;
match cal_diff::<PlainTextAttributes>(old, new) {
match cal_diff::<PhantomAttributes>(old, new) {
None => Ok(None),
Some(delta) => {
self.delta = self.delta.compose(&delta)?;
@ -350,7 +350,7 @@ impl FolderPad {
Some(_) => {
let old = cloned_self.to_json()?;
let new = self.to_json()?;
match cal_diff::<PlainTextAttributes>(old, new) {
match cal_diff::<PhantomAttributes>(old, new) {
None => Ok(None),
Some(delta) => {
self.delta = self.delta.compose(&delta)?;

View File

@ -4,7 +4,7 @@ use crate::util::{cal_diff, make_delta_from_revisions};
use flowy_grid_data_model::revision::{
gen_block_id, gen_row_id, CellRevision, GridBlockRevision, RowMetaChangeset, RowRevision,
};
use lib_ot::core::{OperationTransformable, PlainTextAttributes, PlainTextDelta, PlainTextDeltaBuilder};
use lib_ot::core::{OperationTransformable, PhantomAttributes, PlainTextDelta, PlainTextDeltaBuilder};
use std::borrow::Cow;
use std::collections::HashMap;
use std::sync::Arc;
@ -46,7 +46,7 @@ impl GridBlockRevisionPad {
}
pub fn from_delta(delta: GridBlockRevisionDelta) -> CollaborateResult<Self> {
let s = delta.to_str()?;
let s = delta.content_str()?;
let block_revision: GridBlockRevision = serde_json::from_str(&s).map_err(|e| {
let msg = format!("Deserialize delta to block meta failed: {}", e);
tracing::error!("{}", s);
@ -56,7 +56,7 @@ impl GridBlockRevisionPad {
}
pub fn from_revisions(_grid_id: &str, revisions: Vec<Revision>) -> CollaborateResult<Self> {
let block_delta: GridBlockRevisionDelta = make_delta_from_revisions::<PlainTextAttributes>(revisions)?;
let block_delta: GridBlockRevisionDelta = make_delta_from_revisions::<PhantomAttributes>(revisions)?;
Self::from_delta(block_delta)
}
@ -195,10 +195,10 @@ impl GridBlockRevisionPad {
Some(_) => {
let old = cloned_self.to_json()?;
let new = self.to_json()?;
match cal_diff::<PlainTextAttributes>(old, new) {
match cal_diff::<PhantomAttributes>(old, new) {
None => Ok(None),
Some(delta) => {
tracing::trace!("[GridBlockMeta] Composing delta {}", delta.to_delta_str());
tracing::trace!("[GridBlockMeta] Composing delta {}", delta.to_json_str());
// tracing::debug!(
// "[GridBlockMeta] current delta: {}",
// self.delta.to_str().unwrap_or_else(|_| "".to_string())
@ -231,11 +231,11 @@ impl GridBlockRevisionPad {
}
pub fn md5(&self) -> String {
md5(&self.delta.to_delta_bytes())
md5(&self.delta.to_json_bytes())
}
pub fn delta_str(&self) -> String {
self.delta.to_delta_str()
self.delta.to_json_str()
}
}
@ -252,7 +252,7 @@ pub fn make_grid_block_delta(block_rev: &GridBlockRevision) -> GridBlockRevision
pub fn make_grid_block_revisions(user_id: &str, grid_block_meta_data: &GridBlockRevision) -> RepeatedRevision {
let delta = make_grid_block_delta(grid_block_meta_data);
let bytes = delta.to_delta_bytes();
let bytes = delta.to_json_bytes();
let revision = Revision::initial_revision(user_id, &grid_block_meta_data.block_id, bytes);
revision.into()
}
@ -289,7 +289,7 @@ mod tests {
let change = pad.add_row_rev(row.clone(), None).unwrap().unwrap();
assert_eq!(pad.rows.first().unwrap().as_ref(), &row);
assert_eq!(
change.delta.to_delta_str(),
change.delta.to_json_str(),
r#"[{"retain":24},{"insert":"{\"id\":\"1\",\"block_id\":\"1\",\"cells\":[],\"height\":0,\"visibility\":false}"},{"retain":2}]"#
);
}
@ -303,19 +303,19 @@ mod tests {
let change = pad.add_row_rev(row_1.clone(), None).unwrap().unwrap();
assert_eq!(
change.delta.to_delta_str(),
change.delta.to_json_str(),
r#"[{"retain":24},{"insert":"{\"id\":\"1\",\"block_id\":\"1\",\"cells\":[],\"height\":0,\"visibility\":false}"},{"retain":2}]"#
);
let change = pad.add_row_rev(row_2.clone(), None).unwrap().unwrap();
assert_eq!(
change.delta.to_delta_str(),
change.delta.to_json_str(),
r#"[{"retain":90},{"insert":",{\"id\":\"2\",\"block_id\":\"1\",\"cells\":[],\"height\":0,\"visibility\":false}"},{"retain":2}]"#
);
let change = pad.add_row_rev(row_3.clone(), Some("2".to_string())).unwrap().unwrap();
assert_eq!(
change.delta.to_delta_str(),
change.delta.to_json_str(),
r#"[{"retain":157},{"insert":",{\"id\":\"3\",\"block_id\":\"1\",\"cells\":[],\"height\":0,\"visibility\":false}"},{"retain":2}]"#
);
@ -381,7 +381,7 @@ mod tests {
let _ = pad.add_row_rev(row.clone(), None).unwrap().unwrap();
let change = pad.delete_rows(vec![Cow::Borrowed(&row.id)]).unwrap().unwrap();
assert_eq!(
change.delta.to_delta_str(),
change.delta.to_json_str(),
r#"[{"retain":24},{"delete":66},{"retain":2}]"#
);
@ -410,7 +410,7 @@ mod tests {
let change = pad.update_row(changeset).unwrap().unwrap();
assert_eq!(
change.delta.to_delta_str(),
change.delta.to_json_str(),
r#"[{"retain":69},{"insert":"10"},{"retain":15},{"insert":"tru"},{"delete":4},{"retain":4}]"#
);
@ -422,7 +422,7 @@ mod tests {
fn test_pad() -> GridBlockRevisionPad {
let delta =
GridBlockRevisionDelta::from_delta_str(r#"[{"insert":"{\"block_id\":\"1\",\"rows\":[]}"}]"#).unwrap();
GridBlockRevisionDelta::from_json_str(r#"[{"insert":"{\"block_id\":\"1\",\"rows\":[]}"}]"#).unwrap();
GridBlockRevisionPad::from_delta(delta).unwrap()
}
}

View File

@ -9,7 +9,7 @@ use flowy_grid_data_model::revision::{
GridLayoutRevision, GridRevision, GridSettingRevision, GridSortRevision,
};
use lib_infra::util::move_vec_element;
use lib_ot::core::{OperationTransformable, PlainTextAttributes, PlainTextDelta, PlainTextDeltaBuilder};
use lib_ot::core::{OperationTransformable, PhantomAttributes, PlainTextDelta, PlainTextDeltaBuilder};
use std::collections::HashMap;
use std::sync::Arc;
@ -52,8 +52,8 @@ impl GridRevisionPad {
}
pub fn from_delta(delta: GridRevisionDelta) -> CollaborateResult<Self> {
let s = delta.to_str()?;
let grid: GridRevision = serde_json::from_str(&s)
let content = delta.content_str()?;
let grid: GridRevision = serde_json::from_str(&content)
.map_err(|e| CollaborateError::internal().context(format!("Deserialize delta to grid failed: {}", e)))?;
Ok(Self {
@ -63,7 +63,7 @@ impl GridRevisionPad {
}
pub fn from_revisions(revisions: Vec<Revision>) -> CollaborateResult<Self> {
let grid_delta: GridRevisionDelta = make_delta_from_revisions::<PlainTextAttributes>(revisions)?;
let grid_delta: GridRevisionDelta = make_delta_from_revisions::<PhantomAttributes>(revisions)?;
Self::from_delta(grid_delta)
}
@ -457,15 +457,15 @@ impl GridRevisionPad {
}
pub fn md5(&self) -> String {
md5(&self.delta.to_delta_bytes())
md5(&self.delta.to_json_bytes())
}
pub fn delta_str(&self) -> String {
self.delta.to_delta_str()
self.delta.to_json_str()
}
pub fn delta_bytes(&self) -> Bytes {
self.delta.to_delta_bytes()
self.delta.to_json_bytes()
}
pub fn fields(&self) -> &[Arc<FieldRevision>] {
@ -482,7 +482,7 @@ impl GridRevisionPad {
Some(_) => {
let old = make_grid_rev_json_str(&cloned_grid)?;
let new = self.json_str()?;
match cal_diff::<PlainTextAttributes>(old, new) {
match cal_diff::<PhantomAttributes>(old, new) {
None => Ok(None),
Some(delta) => {
self.delta = self.delta.compose(&delta)?;
@ -553,7 +553,7 @@ pub fn make_grid_delta(grid_rev: &GridRevision) -> GridRevisionDelta {
pub fn make_grid_revisions(user_id: &str, grid_rev: &GridRevision) -> RepeatedRevision {
let delta = make_grid_delta(grid_rev);
let bytes = delta.to_delta_bytes();
let bytes = delta.to_json_bytes();
let revision = Revision::initial_revision(user_id, &grid_rev.grid_id, bytes);
revision.into()
}

View File

@ -89,7 +89,7 @@ impl std::fmt::Debug for Revision {
let _ = f.write_fmt(format_args!("rev_id {}, ", self.rev_id))?;
match RichTextDelta::from_bytes(&self.delta_data) {
Ok(delta) => {
let _ = f.write_fmt(format_args!("delta {:?}", delta.to_delta_str()))?;
let _ = f.write_fmt(format_args!("delta {:?}", delta.to_json_str()))?;
}
Err(e) => {
let _ = f.write_fmt(format_args!("delta {:?}", e))?;

View File

@ -46,7 +46,7 @@ impl std::convert::TryFrom<Revision> for DocumentPB {
}
let delta = RichTextDelta::from_bytes(&revision.delta_data)?;
let doc_json = delta.to_delta_str();
let doc_json = delta.to_json_str();
Ok(DocumentPB {
block_id: revision.object_id,

View File

@ -39,7 +39,7 @@ impl RevisionSyncObject<RichTextAttributes> for ServerDocument {
}
fn to_json(&self) -> String {
self.delta.to_delta_str()
self.delta.to_json_str()
}
fn set_delta(&mut self, new_delta: Delta<RichTextAttributes>) {

View File

@ -13,7 +13,7 @@ use crate::{
use async_stream::stream;
use futures::stream::StreamExt;
use lib_infra::future::BoxResultFuture;
use lib_ot::core::PlainTextAttributes;
use lib_ot::core::PhantomAttributes;
use std::{collections::HashMap, fmt::Debug, sync::Arc};
use tokio::{
sync::{mpsc, oneshot, RwLock},
@ -188,7 +188,7 @@ impl ServerFolderManager {
}
}
type FolderRevisionSynchronizer = RevisionSynchronizer<PlainTextAttributes>;
type FolderRevisionSynchronizer = RevisionSynchronizer<PhantomAttributes>;
struct OpenFolderHandler {
folder_id: String,

View File

@ -1,5 +1,5 @@
use crate::{entities::folder::FolderDelta, errors::CollaborateError, synchronizer::RevisionSyncObject};
use lib_ot::core::{OperationTransformable, PlainTextAttributes, PlainTextDelta};
use lib_ot::core::{OperationTransformable, PhantomAttributes, PlainTextDelta};
pub struct ServerFolder {
folder_id: String,
@ -15,7 +15,7 @@ impl ServerFolder {
}
}
impl RevisionSyncObject<PlainTextAttributes> for ServerFolder {
impl RevisionSyncObject<PhantomAttributes> for ServerFolder {
fn id(&self) -> &str {
&self.folder_id
}
@ -32,7 +32,7 @@ impl RevisionSyncObject<PlainTextAttributes> for ServerFolder {
}
fn to_json(&self) -> String {
self.delta.to_delta_str()
self.delta.to_json_str()
}
fn set_delta(&mut self, new_delta: PlainTextDelta) {

View File

@ -149,7 +149,7 @@ pub fn make_folder_from_revisions_pb(
folder_delta = folder_delta.compose(&delta)?;
}
let text = folder_delta.to_delta_str();
let text = folder_delta.to_json_str();
Ok(Some(FolderInfo {
folder_id: folder_id.to_string(),
text,
@ -183,7 +183,7 @@ pub fn make_document_from_revision_pbs(
delta = delta.compose(&new_delta)?;
}
let text = delta.to_delta_str();
let text = delta.to_json_str();
Ok(Some(DocumentPB {
block_id: doc_id.to_owned(),

View File

@ -1,7 +1,7 @@
use crate::core::delta::{trim, Delta};
use crate::core::operation::{Attributes, PlainTextAttributes};
use crate::core::operation::{Attributes, PhantomAttributes};
pub type PlainTextDeltaBuilder = DeltaBuilder<PlainTextAttributes>;
pub type PlainTextDeltaBuilder = DeltaBuilder<PhantomAttributes>;
/// A builder for creating new [Delta] objects.
///

View File

@ -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, PlainTextAttributes};
use crate::core::operation::{Attributes, Operation, OperationBuilder, OperationTransformable, PhantomAttributes};
use bytes::Bytes;
use serde::de::DeserializeOwned;
use std::{
@ -14,12 +14,15 @@ use std::{
str::FromStr,
};
pub type PlainTextDelta = Delta<PlainTextAttributes>;
pub type PlainTextDelta = Delta<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.
///
/// If the [T] supports 'serde', that will enable delta to serialize to JSON or deserialize from
/// a JSON string.
///
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Delta<T: Attributes> {
pub ops: Vec<Operation<T>>,
@ -182,7 +185,7 @@ where
/// .insert(", AppFlowy")
/// .build();
///
/// let after_a = delta_a.to_str().unwrap();
/// let after_a = delta_a.content_str().unwrap();
/// let after_b = delta_b.apply(&after_a).unwrap();
/// assert_eq!("hello, AppFlowy", &after_b);
/// ```
@ -565,7 +568,7 @@ impl<T> Delta<T>
where
T: Attributes + DeserializeOwned,
{
pub fn from_delta_str(json: &str) -> Result<Self, OTError> {
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);
tracing::trace!("{:?}", json);
@ -576,7 +579,7 @@ where
pub fn from_bytes<B: AsRef<[u8]>>(bytes: B) -> Result<Self, OTError> {
let json = str::from_utf8(bytes.as_ref())?.to_owned();
let val = Self::from_delta_str(&json)?;
let val = Self::from_json_str(&json)?;
Ok(val)
}
}
@ -585,16 +588,16 @@ impl<T> Delta<T>
where
T: Attributes + serde::Serialize,
{
pub fn to_delta_str(&self) -> String {
pub fn to_json_str(&self) -> String {
serde_json::to_string(self).unwrap_or_else(|_| "".to_owned())
}
pub fn to_str(&self) -> Result<String, OTError> {
pub fn content_str(&self) -> Result<String, OTError> {
self.apply("")
}
pub fn to_delta_bytes(&self) -> Bytes {
let json = self.to_delta_str();
pub fn to_json_bytes(&self) -> Bytes {
let json = self.to_json_str();
Bytes::from(json.into_bytes())
}
}

View File

@ -1,8 +1,8 @@
use crate::core::operation::{Attributes, Operation, PlainTextAttributes};
use crate::core::operation::{Attributes, Operation, PhantomAttributes};
use crate::rich_text::RichTextAttributes;
pub type RichTextOpBuilder = OperationBuilder<RichTextAttributes>;
pub type PlainTextOpBuilder = OperationBuilder<PlainTextAttributes>;
pub type PlainTextOpBuilder = OperationBuilder<PhantomAttributes>;
pub struct OperationBuilder<T: Attributes> {
ty: Operation<T>,

View File

@ -26,7 +26,7 @@ pub trait OperationTransformable {
/// let document = PlainTextDeltaBuilder::new().build();
/// let delta = PlainTextDeltaBuilder::new().insert("abc").build();
/// let new_document = document.compose(&delta).unwrap();
/// assert_eq!(new_document.to_str().unwrap(), "abc".to_owned());
/// assert_eq!(new_document.content_str().unwrap(), "abc".to_owned());
/// ```
fn compose(&self, other: &Self) -> Result<Self, OTError>
where
@ -65,6 +65,12 @@ pub trait OperationTransformable {
fn invert(&self, other: &Self) -> Self;
}
/// 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.
///
pub trait Attributes: Default + Display + Eq + PartialEq + Clone + Debug + OperationTransformable {
fn is_empty(&self) -> bool {
true
@ -80,6 +86,14 @@ pub trait Attributes: Default + Display + Eq + PartialEq + Clone + Debug + Opera
}
}
/// [Operation] consists of three types.
/// * Delete
/// * Retain
/// * Insert
///
/// 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.
///
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum Operation<T: Attributes> {
Delete(usize),
@ -241,9 +255,7 @@ where
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Retain<T: Attributes> {
// #[serde(rename(serialize = "retain", deserialize = "retain"))]
pub n: usize,
// #[serde(skip_serializing_if = "is_empty")]
pub attributes: T,
}
@ -318,10 +330,7 @@ where
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Insert<T: Attributes> {
// #[serde(rename(serialize = "insert", deserialize = "insert"))]
pub s: FlowyStr,
// #[serde(skip_serializing_if = "is_empty")]
pub attributes: T,
}
@ -402,16 +411,16 @@ where
}
#[derive(Debug, Clone, Eq, PartialEq, Default, Serialize, Deserialize)]
pub struct PlainTextAttributes();
impl fmt::Display for PlainTextAttributes {
pub struct PhantomAttributes();
impl fmt::Display for PhantomAttributes {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("PlainAttributes")
f.write_str("PhantomAttributes")
}
}
impl Attributes for PlainTextAttributes {}
impl Attributes for PhantomAttributes {}
impl OperationTransformable for PlainTextAttributes {
impl OperationTransformable for PhantomAttributes {
fn compose(&self, _other: &Self) -> Result<Self, OTError> {
Ok(self.clone())
}