chore: rename fns

This commit is contained in:
appflowy 2022-08-02 09:11:04 +08:00
parent 00d81a329f
commit ff098c538e
29 changed files with 92 additions and 92 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_json_str()
initial_read_me().json_str()
} else {
initial_quill_delta_string()
};

View File

@ -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_json_bytes();
let delta_data = delta.json_bytes();
let revision = Revision::new(
&self.rev_manager.object_id,
base_rev_id,
@ -133,6 +133,6 @@ pub struct FolderRevisionCompactor();
impl RevisionCompactor for FolderRevisionCompactor {
fn bytes_from_revisions(&self, revisions: Vec<Revision>) -> FlowyResult<Bytes> {
let delta = make_delta_from_revisions::<PhantomAttributes>(revisions)?;
Ok(delta.to_json_bytes())
Ok(delta.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 = TextDeltaBuilder::new().insert(&json).build().to_json_bytes();
let delta_data = TextDeltaBuilder::new().insert(&json).build().json_bytes();
let revision = Revision::initial_revision(user_id, folder_id.as_ref(), delta_data);
let record = RevisionRecord {
revision,

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_json_bytes();
let block_delta_data = grid_block_delta.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_json_bytes();
let grid_delta_data = grid_meta_delta.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

@ -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_json_bytes();
let delta_data = delta.json_bytes();
let revision = Revision::new(
&self.rev_manager.object_id,
base_rev_id,
@ -201,6 +201,6 @@ pub struct GridBlockRevisionCompactor();
impl RevisionCompactor for GridBlockRevisionCompactor {
fn bytes_from_revisions(&self, revisions: Vec<Revision>) -> FlowyResult<Bytes> {
let delta = make_delta_from_revisions::<PhantomAttributes>(revisions)?;
Ok(delta.to_json_bytes())
Ok(delta.json_bytes())
}
}

View File

@ -572,7 +572,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_json_bytes();
let delta_data = delta.json_bytes();
let revision = Revision::new(
&self.rev_manager.object_id,
base_rev_id,
@ -664,7 +664,7 @@ pub struct GridRevisionCompactor();
impl RevisionCompactor for GridRevisionCompactor {
fn bytes_from_revisions(&self, revisions: Vec<Revision>) -> FlowyResult<Bytes> {
let delta = make_delta_from_revisions::<PhantomAttributes>(revisions)?;
Ok(delta.to_json_bytes())
Ok(delta.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 = TextDeltaBuilder::new().insert(&json).build().to_json_bytes();
let delta_data = TextDeltaBuilder::new().insert(&json).build().json_bytes();
let revision = Revision::initial_revision(&user_id, grid_id, delta_data);
let record = RevisionRecord::new(revision);
//

View File

@ -154,7 +154,7 @@ where
&rev_manager.object_id,
base_rev_id,
rev_id,
client_delta.to_json_bytes(),
client_delta.json_bytes(),
user_id,
md5.clone(),
);
@ -166,7 +166,7 @@ where
&rev_manager.object_id,
base_rev_id,
rev_id,
server_delta.to_json_bytes(),
server_delta.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_json_str(),
text: delta.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_json_bytes();
let delta_data = delta.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_json_bytes())
Ok(delta.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_json_str());
eprintln!("❌ receive: {}", delta.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_json_str(r#"[{"insert":"\n"}]"#).unwrap();
let mut delta = RichTextDelta::from_json(r#"[{"insert":"\n"}]"#).unwrap();
let deltas = vec![
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(),
RichTextDelta::from_json(r#"[{"retain":1,"attributes":{"list":"unchecked"}}]"#).unwrap(),
RichTextDelta::from_json(r#"[{"insert":"a"}]"#).unwrap(),
RichTextDelta::from_json(r#"[{"retain":1},{"insert":"\n","attributes":{"list":"unchecked"}}]"#).unwrap(),
RichTextDelta::from_json(r#"[{"retain":2},{"retain":1,"attributes":{"list":""}}]"#).unwrap(),
];
for d in deltas {
delta = delta.compose(&d).unwrap();
}
assert_eq!(
delta.to_json_str(),
delta.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_json_str());
tracing::debug!("Insert delta: {}", delta.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_json_str());
tracing::trace!("Delete delta: {}", delta.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_json_str());
tracing::trace!("Replace delta: {}", delta.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_json_str());
tracing::trace!("Bold delta: {}", delta.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_json_str());
tracing::trace!("Italic delta: {}", delta.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_json_str());
tracing::trace!("Header delta: {}", delta.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_json_str());
tracing::trace!("Link delta: {}", delta.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_json_str());
tracing::debug!("Bullet delta: {}", delta.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_json_str());
tracing::debug!("b: {}", delta_b.to_json_str());
tracing::debug!("a: {}", delta_a.json_str());
tracing::debug!("b: {}", delta_b.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_json_str());
tracing::debug!("undo delta: {}", undo.to_json_str());
tracing::debug!("new delta: {}", new_delta.json_str());
tracing::debug!("undo delta: {}", undo.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_json_str();
let prime_json = self.primes[*doc_i].as_ref().unwrap().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

@ -345,7 +345,7 @@ fn apply_test() {
let delta_a = TextDeltaBuilder::new().insert(s).build();
let delta_b = TextDeltaBuilder::new().retain(s.len()).insert(", AppFlowy").build();
let after_a = delta_a.content_str().unwrap();
let after_a = delta_a.content().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_json_str(&json).unwrap();
let delta_from_json = Delta::from_json(&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_json_str(json).unwrap();
let delta = RichTextDelta::from_json(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_json_str(json).unwrap();
let delta1 = RichTextDelta::from_json(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_json_str(), r#"[{"retain":7,"attributes":{"bold":""}}]"#);
assert_eq!(delta2.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_json_str()
initial_quill_delta().json_str()
}
#[inline]
pub fn initial_read_me() -> RichTextDelta {
let json = include_str!("READ_ME.json");
RichTextDelta::from_json_str(json).unwrap()
RichTextDelta::from_json(json).unwrap()
}
#[cfg(test)]
@ -22,6 +22,6 @@ mod tests {
#[test]
fn load_read_me() {
println!("{}", initial_read_me().to_json_str());
println!("{}", initial_read_me().json_str());
}
}

View File

@ -55,16 +55,16 @@ impl ClientDocument {
}
pub fn from_json(json: &str) -> Result<Self, CollaborateError> {
let delta = RichTextDelta::from_json_str(json)?;
let delta = RichTextDelta::from_json(json)?;
Ok(Self::from_delta(delta))
}
pub fn delta_str(&self) -> String {
self.delta.to_json_str()
self.delta.json_str()
}
pub fn to_bytes(&self) -> Bytes {
self.delta.to_json_bytes()
self.delta.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_json_str());
tracing::trace!("document: {}", data.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_json_str(), delta.to_json_str());
tracing::trace!("{} compose {}", &self.delta.json_str(), delta.json_str());
let composed_delta = self.delta.compose(&delta)?;
let mut undo_delta = delta.invert(&self.delta);

View File

@ -41,7 +41,7 @@ impl FolderPadBuilder {
}
// TODO: Reconvert from history if delta.to_str() failed.
let content = delta.content_str()?;
let content = delta.content()?;
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));

View File

@ -295,7 +295,7 @@ impl FolderPad {
}
pub fn md5(&self) -> String {
md5(&self.delta.to_json_bytes())
md5(&self.delta.json_bytes())
}
pub fn to_json(&self) -> CollaborateResult<String> {

View File

@ -46,7 +46,7 @@ impl GridBlockRevisionPad {
}
pub fn from_delta(delta: GridBlockRevisionDelta) -> CollaborateResult<Self> {
let s = delta.content_str()?;
let s = delta.content()?;
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);
@ -198,7 +198,7 @@ impl GridBlockRevisionPad {
match cal_diff::<PhantomAttributes>(old, new) {
None => Ok(None),
Some(delta) => {
tracing::trace!("[GridBlockMeta] Composing delta {}", delta.to_json_str());
tracing::trace!("[GridBlockMeta] Composing delta {}", delta.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_json_bytes())
md5(&self.delta.json_bytes())
}
pub fn delta_str(&self) -> String {
self.delta.to_json_str()
self.delta.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_json_bytes();
let bytes = delta.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_json_str(),
change.delta.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_json_str(),
change.delta.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_json_str(),
change.delta.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_json_str(),
change.delta.json_str(),
r#"[{"retain":157},{"insert":",{\"id\":\"3\",\"block_id\":\"1\",\"cells\":[],\"height\":0,\"visibility\":false}"},{"retain":2}]"#
);
@ -380,10 +380,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_json_str(),
r#"[{"retain":24},{"delete":66},{"retain":2}]"#
);
assert_eq!(change.delta.json_str(), r#"[{"retain":24},{"delete":66},{"retain":2}]"#);
assert_eq!(pad.delta_str(), pre_delta_str);
}
@ -410,7 +407,7 @@ mod tests {
let change = pad.update_row(changeset).unwrap().unwrap();
assert_eq!(
change.delta.to_json_str(),
change.delta.json_str(),
r#"[{"retain":69},{"insert":"10"},{"retain":15},{"insert":"tru"},{"delete":4},{"retain":4}]"#
);
@ -421,8 +418,7 @@ mod tests {
}
fn test_pad() -> GridBlockRevisionPad {
let delta =
GridBlockRevisionDelta::from_json_str(r#"[{"insert":"{\"block_id\":\"1\",\"rows\":[]}"}]"#).unwrap();
let delta = GridBlockRevisionDelta::from_json(r#"[{"insert":"{\"block_id\":\"1\",\"rows\":[]}"}]"#).unwrap();
GridBlockRevisionPad::from_delta(delta).unwrap()
}
}

View File

@ -52,7 +52,7 @@ impl GridRevisionPad {
}
pub fn from_delta(delta: GridRevisionDelta) -> CollaborateResult<Self> {
let content = delta.content_str()?;
let content = delta.content()?;
let grid: GridRevision = serde_json::from_str(&content)
.map_err(|e| CollaborateError::internal().context(format!("Deserialize delta to grid failed: {}", e)))?;
@ -457,15 +457,15 @@ impl GridRevisionPad {
}
pub fn md5(&self) -> String {
md5(&self.delta.to_json_bytes())
md5(&self.delta.json_bytes())
}
pub fn delta_str(&self) -> String {
self.delta.to_json_str()
self.delta.json_str()
}
pub fn delta_bytes(&self) -> Bytes {
self.delta.to_json_bytes()
self.delta.json_bytes()
}
pub fn fields(&self) -> &[Arc<FieldRevision>] {
@ -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_json_bytes();
let bytes = delta.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_json_str()))?;
let _ = f.write_fmt(format_args!("delta {:?}", delta.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_json_str();
let doc_json = delta.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_json_str()
self.delta.json_str()
}
fn set_delta(&mut self, new_delta: Delta<RichTextAttributes>) {

View File

@ -32,7 +32,7 @@ impl RevisionSyncObject<PhantomAttributes> for ServerFolder {
}
fn to_json(&self) -> String {
self.delta.to_json_str()
self.delta.json_str()
}
fn set_delta(&mut self, new_delta: TextDelta) {

View File

@ -149,7 +149,7 @@ pub fn make_folder_from_revisions_pb(
folder_delta = folder_delta.compose(&delta)?;
}
let text = folder_delta.to_json_str();
let text = folder_delta.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_json_str();
let text = delta.json_str();
Ok(Some(DocumentPB {
block_id: doc_id.to_owned(),

View File

@ -14,7 +14,7 @@ use crate::core::Operation;
/// let delta = TextDeltaBuilder::new()
/// .insert("AppFlowy")
/// .build();
/// assert_eq!(delta.content_str().unwrap(), "AppFlowy");
/// assert_eq!(delta.content().unwrap(), "AppFlowy");
/// ```
pub struct DeltaBuilder<T: Attributes> {
delta: Delta<T>,
@ -55,7 +55,7 @@ where
/// let mut attribute = RichTextAttribute::Bold(true);
/// let delta = RichTextDeltaBuilder::new().retain_with_attributes(7, attribute.into()).build();
///
/// assert_eq!(delta.to_json_str(), r#"[{"retain":7,"attributes":{"bold":true}}]"#);
/// assert_eq!(delta.json_str(), r#"[{"retain":7,"attributes":{"bold":true}}]"#);
/// ```
pub fn retain_with_attributes(mut self, n: usize, attrs: T) -> Self {
self.delta.retain(n, attrs);
@ -84,7 +84,7 @@ where
/// .build();
///
/// let new_delta = delta.compose(&changeset).unwrap();
/// assert_eq!(new_delta.content_str().unwrap(), "AppFlowy");
/// assert_eq!(new_delta.content().unwrap(), "AppFlowy");
/// ```
pub fn delete(mut self, n: usize) -> Self {
self.delta.delete(n);

View File

@ -188,7 +188,7 @@ where
/// .insert(", AppFlowy")
/// .build();
///
/// let after_a = delta_a.content_str().unwrap();
/// let after_a = delta_a.content().unwrap();
/// let after_b = delta_b.apply(&after_a).unwrap();
/// assert_eq!("hello, AppFlowy", &after_b);
/// ```
@ -559,7 +559,7 @@ fn transform_op_attribute<T: Attributes>(
}
let left = left.as_ref().unwrap().get_attributes();
let right = right.as_ref().unwrap().get_attributes();
// TODO: replace with anyhow and thiserror.
// TODO: replace with anyhow and this error.
Ok(left.transform(&right)?.0)
}
@ -575,10 +575,10 @@ where
/// let json = r#"[
/// {"retain":7,"attributes":{"bold":null}}
/// ]"#;
/// let delta = RichTextDelta::from_json_str(json).unwrap();
/// assert_eq!(delta.to_json_str(), r#"[{"retain":7,"attributes":{"bold":""}}]"#);
/// let delta = RichTextDelta::from_json(json).unwrap();
/// assert_eq!(delta.json_str(), r#"[{"retain":7,"attributes":{"bold":""}}]"#);
/// ```
pub fn from_json_str(json: &str) -> Result<Self, OTError> {
pub fn from_json(json: &str) -> Result<Self, OTError> {
let delta = serde_json::from_str(json).map_err(|e| {
tracing::trace!("Deserialize failed: {:?}", e);
tracing::trace!("{:?}", json);
@ -587,9 +587,10 @@ where
Ok(delta)
}
/// Deserialize the bytes into [Delta]. It requires the bytes is in utf8 format.
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_json_str(&json)?;
let val = Self::from_json(&json)?;
Ok(val)
}
}
@ -598,16 +599,19 @@ impl<T> Delta<T>
where
T: Attributes + serde::Serialize,
{
pub fn to_json_str(&self) -> String {
/// Serialize the [Delta] into a String in JSON format
pub fn json_str(&self) -> String {
serde_json::to_string(self).unwrap_or_else(|_| "".to_owned())
}
pub fn content_str(&self) -> Result<String, OTError> {
/// Get the content the [Delta] represents.
pub fn content(&self) -> Result<String, OTError> {
self.apply("")
}
pub fn to_json_bytes(&self) -> Bytes {
let json = self.to_json_str();
/// Serial the [Delta] into a String in Bytes format
pub fn json_bytes(&self) -> Bytes {
let json = self.json_str();
Bytes::from(json.into_bytes())
}
}

View File

@ -25,7 +25,7 @@ pub trait OperationTransform {
/// 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());
/// assert_eq!(new_document.content().unwrap(), "abc".to_owned());
/// ```
fn compose(&self, other: &Self) -> Result<Self, OTError>
where