diff --git a/frontend/rust-lib/flowy-folder/tests/workspace/script.rs b/frontend/rust-lib/flowy-folder/tests/workspace/script.rs index 7b6091b7b5..cdcd3cfa8e 100644 --- a/frontend/rust-lib/flowy-folder/tests/workspace/script.rs +++ b/frontend/rust-lib/flowy-folder/tests/workspace/script.rs @@ -17,7 +17,7 @@ use flowy_folder::{errors::ErrorCode, services::folder_editor::FolderEditor}; use flowy_revision::disk::RevisionState; use flowy_revision::REVISION_WRITE_INTERVAL_IN_MILLIS; -use flowy_sync::entities::text_block::TextBlockInfoPB; +use flowy_sync::entities::text_block::DocumentPB; use flowy_test::{event_builder::*, FlowySDKTest}; use std::{sync::Arc, time::Duration}; use tokio::time::sleep; @@ -399,14 +399,14 @@ pub async fn delete_view(sdk: &FlowySDKTest, view_ids: Vec) { } #[allow(dead_code)] -pub async fn set_latest_view(sdk: &FlowySDKTest, view_id: &str) -> TextBlockInfoPB { +pub async fn set_latest_view(sdk: &FlowySDKTest, view_id: &str) -> DocumentPB { let view_id: ViewId = view_id.into(); FolderEventBuilder::new(sdk.clone()) .event(SetLatestView) .payload(view_id) .async_send() .await - .parse::() + .parse::() } pub async fn read_trash(sdk: &FlowySDKTest) -> RepeatedTrash { diff --git a/frontend/rust-lib/flowy-net/src/http_server/document.rs b/frontend/rust-lib/flowy-net/src/http_server/document.rs index c8d18b07b2..ca9cb47955 100644 --- a/frontend/rust-lib/flowy-net/src/http_server/document.rs +++ b/frontend/rust-lib/flowy-net/src/http_server/document.rs @@ -3,7 +3,7 @@ use crate::{ request::{HttpRequestBuilder, ResponseMiddleware}, }; use flowy_error::FlowyError; -use flowy_sync::entities::text_block::{CreateTextBlockParams, ResetTextBlockParams, TextBlockIdPB, TextBlockInfoPB}; +use flowy_sync::entities::text_block::{CreateTextBlockParams, DocumentPB, ResetTextBlockParams, TextBlockIdPB}; use flowy_text_block::BlockCloudService; use http_flowy::response::FlowyResponse; use lazy_static::lazy_static; @@ -27,7 +27,7 @@ impl BlockCloudService for BlockHttpCloudService { FutureResult::new(async move { create_document_request(&token, params, &url).await }) } - fn read_block(&self, token: &str, params: TextBlockIdPB) -> FutureResult, FlowyError> { + fn read_block(&self, token: &str, params: TextBlockIdPB) -> FutureResult, FlowyError> { let token = token.to_owned(); let url = self.config.doc_url(); FutureResult::new(async move { read_document_request(&token, params, &url).await }) @@ -54,7 +54,7 @@ pub async fn read_document_request( token: &str, params: TextBlockIdPB, url: &str, -) -> Result, FlowyError> { +) -> Result, FlowyError> { let doc = request_builder() .get(&url.to_owned()) .header(HEADER_TOKEN, token) diff --git a/frontend/rust-lib/flowy-net/src/local_server/persistence.rs b/frontend/rust-lib/flowy-net/src/local_server/persistence.rs index d8bae35401..4cd305f2a6 100644 --- a/frontend/rust-lib/flowy-net/src/local_server/persistence.rs +++ b/frontend/rust-lib/flowy-net/src/local_server/persistence.rs @@ -1,10 +1,10 @@ use flowy_sync::{ - entities::{folder::FolderInfo, text_block::TextBlockInfoPB}, + entities::{folder::FolderInfo, text_block::DocumentPB}, errors::CollaborateError, protobuf::{RepeatedRevision as RepeatedRevisionPB, Revision as RevisionPB}, server_document::*, server_folder::FolderCloudPersistence, - util::{make_document_info_from_revisions_pb, make_folder_from_revisions_pb}, + util::{make_document_from_revision_pbs, make_folder_from_revisions_pb}, }; use lib_infra::future::BoxResultFuture; use std::{ @@ -111,12 +111,12 @@ impl FolderCloudPersistence for LocalTextBlockCloudPersistence { } impl TextBlockCloudPersistence for LocalTextBlockCloudPersistence { - fn read_text_block(&self, doc_id: &str) -> BoxResultFuture { + fn read_text_block(&self, doc_id: &str) -> BoxResultFuture { let storage = self.storage.clone(); let doc_id = doc_id.to_owned(); Box::pin(async move { let repeated_revision = storage.get_revisions(&doc_id, None).await?; - match make_document_info_from_revisions_pb(&doc_id, repeated_revision)? { + match make_document_from_revision_pbs(&doc_id, repeated_revision)? { Some(document_info) => Ok(document_info), None => Err(CollaborateError::record_not_found()), } @@ -127,12 +127,12 @@ impl TextBlockCloudPersistence for LocalTextBlockCloudPersistence { &self, doc_id: &str, repeated_revision: RepeatedRevisionPB, - ) -> BoxResultFuture, CollaborateError> { + ) -> BoxResultFuture, CollaborateError> { let doc_id = doc_id.to_owned(); let storage = self.storage.clone(); Box::pin(async move { let _ = storage.set_revisions(repeated_revision.clone()).await?; - make_document_info_from_revisions_pb(&doc_id, repeated_revision) + make_document_from_revision_pbs(&doc_id, repeated_revision) }) } diff --git a/frontend/rust-lib/flowy-net/src/local_server/server.rs b/frontend/rust-lib/flowy-net/src/local_server/server.rs index 66a7682584..7625a8ee84 100644 --- a/frontend/rust-lib/flowy-net/src/local_server/server.rs +++ b/frontend/rust-lib/flowy-net/src/local_server/server.rs @@ -6,7 +6,7 @@ use flowy_folder::event_map::FolderCouldServiceV1; use flowy_sync::{ client_document::default::initial_quill_delta_string, entities::{ - text_block::{CreateTextBlockParams, ResetTextBlockParams, TextBlockIdPB, TextBlockInfoPB}, + text_block::{CreateTextBlockParams, DocumentPB, ResetTextBlockParams, TextBlockIdPB}, ws_data::{ClientRevisionWSData, ClientRevisionWSDataType}, }, errors::CollaborateError, @@ -419,8 +419,8 @@ impl BlockCloudService for LocalServer { FutureResult::new(async { Ok(()) }) } - fn read_block(&self, _token: &str, params: TextBlockIdPB) -> FutureResult, FlowyError> { - let doc = TextBlockInfoPB { + fn read_block(&self, _token: &str, params: TextBlockIdPB) -> FutureResult, FlowyError> { + let doc = DocumentPB { block_id: params.value, text: initial_quill_delta_string(), rev_id: 0, diff --git a/frontend/rust-lib/flowy-text-block/src/editor.rs b/frontend/rust-lib/flowy-text-block/src/editor.rs index 84b30a0047..6fed85c913 100644 --- a/frontend/rust-lib/flowy-text-block/src/editor.rs +++ b/frontend/rust-lib/flowy-text-block/src/editor.rs @@ -9,7 +9,7 @@ use flowy_error::{internal_error, FlowyResult}; use flowy_revision::{RevisionCloudService, RevisionManager, RevisionObjectBuilder, RevisionWebSocket}; use flowy_sync::entities::ws_data::ServerRevisionWSData; use flowy_sync::{ - entities::{revision::Revision, text_block::TextBlockInfoPB}, + entities::{revision::Revision, text_block::DocumentPB}, errors::CollaborateResult, util::make_delta_from_revisions, }; @@ -229,14 +229,14 @@ impl TextBlockEditor { struct TextBlockInfoBuilder(); impl RevisionObjectBuilder for TextBlockInfoBuilder { - type Output = TextBlockInfoPB; + type Output = DocumentPB; fn build_object(object_id: &str, revisions: Vec) -> FlowyResult { let (base_rev_id, rev_id) = revisions.last().unwrap().pair_rev_id(); let mut delta = make_delta_from_revisions(revisions)?; correct_delta(&mut delta); - Result::::Ok(TextBlockInfoPB { + Result::::Ok(DocumentPB { block_id: object_id.to_owned(), text: delta.to_delta_str(), rev_id, diff --git a/frontend/rust-lib/flowy-text-block/src/lib.rs b/frontend/rust-lib/flowy-text-block/src/lib.rs index 808bc2d6f9..37ddf6ea1e 100644 --- a/frontend/rust-lib/flowy-text-block/src/lib.rs +++ b/frontend/rust-lib/flowy-text-block/src/lib.rs @@ -15,13 +15,13 @@ pub mod errors { pub const TEXT_BLOCK_SYNC_INTERVAL_IN_MILLIS: u64 = 1000; use crate::errors::FlowyError; -use flowy_sync::entities::text_block::{CreateTextBlockParams, ResetTextBlockParams, TextBlockIdPB, TextBlockInfoPB}; +use flowy_sync::entities::text_block::{CreateTextBlockParams, DocumentPB, ResetTextBlockParams, TextBlockIdPB}; use lib_infra::future::FutureResult; pub trait BlockCloudService: Send + Sync { fn create_block(&self, token: &str, params: CreateTextBlockParams) -> FutureResult<(), FlowyError>; - fn read_block(&self, token: &str, params: TextBlockIdPB) -> FutureResult, FlowyError>; + fn read_block(&self, token: &str, params: TextBlockIdPB) -> FutureResult, FlowyError>; fn update_block(&self, token: &str, params: ResetTextBlockParams) -> FutureResult<(), FlowyError>; } diff --git a/shared-lib/flowy-derive/src/proto_buf/deserialize.rs b/shared-lib/flowy-derive/src/proto_buf/deserialize.rs index f0325db3b0..4fbbb1b3a7 100644 --- a/shared-lib/flowy-derive/src/proto_buf/deserialize.rs +++ b/shared-lib/flowy-derive/src/proto_buf/deserialize.rs @@ -25,8 +25,15 @@ pub fn make_de_token_steam(ctxt: &Ctxt, ast: &ASTContainer) -> Option for #struct_ident { type Error = ::protobuf::ProtobufError; fn try_from(bytes: bytes::Bytes) -> Result { - let pb: crate::protobuf::#pb_ty = ::protobuf::Message::parse_from_bytes(&bytes)?; - #struct_ident::try_from(pb) + Self::try_from(&bytes) + } + } + + impl std::convert::TryFrom<&bytes::Bytes> for #struct_ident { + type Error = ::protobuf::ProtobufError; + fn try_from(bytes: &bytes::Bytes) -> Result { + let pb: crate::protobuf::#pb_ty = ::protobuf::Message::parse_from_bytes(bytes)?; + Ok(#struct_ident::from(pb)) } } @@ -34,16 +41,15 @@ pub fn make_de_token_steam(ctxt: &Ctxt, ast: &ASTContainer) -> Option Result { let pb: crate::protobuf::#pb_ty = ::protobuf::Message::parse_from_bytes(bytes)?; - #struct_ident::try_from(pb) + Ok(#struct_ident::from(pb)) } } - impl std::convert::TryFrom for #struct_ident { - type Error = ::protobuf::ProtobufError; - fn try_from(mut pb: crate::protobuf::#pb_ty) -> Result { + impl std::convert::From for #struct_ident { + fn from(mut pb: crate::protobuf::#pb_ty) -> Self { let mut o = Self::default(); #(#build_take_fields)* - Ok(o) + o } } }; @@ -70,7 +76,7 @@ fn token_stream_for_one_of(ctxt: &Ctxt, field: &ASTField) -> Option let ty = bracketed_ty_info.unwrap().ty; Some(quote! { if pb.#has_func() { - let enum_de_from_pb = #ty::try_from(&pb.#get_func()).unwrap(); + let enum_de_from_pb = #ty::from(&pb.#get_func()); o.#member = Some(enum_de_from_pb); } }) @@ -104,7 +110,7 @@ fn token_stream_for_one_of(ctxt: &Ctxt, field: &ASTField) -> Option let ty = bracketed_ty_info.unwrap().ty; Some(quote! { if pb.#has_func() { - let val = #ty::try_from(pb.#take_func()).unwrap(); + let val = #ty::from(pb.#take_func()); o.#member=Some(val); } }) @@ -138,7 +144,7 @@ fn token_stream_for_field(ctxt: &Ctxt, member: &syn::Member, ty: &syn::Type, is_ Some(quote! { let some_value = pb.#member.#take(); if some_value.is_some() { - let struct_de_from_pb = #ty::try_from(some_value.unwrap()).unwrap(); + let struct_de_from_pb = #ty::from(some_value.unwrap()); o.#member = struct_de_from_pb; } }) @@ -147,7 +153,7 @@ fn token_stream_for_field(ctxt: &Ctxt, member: &syn::Member, ty: &syn::Type, is_ TypeCategory::Enum => { let ty = ty_info.ty; Some(quote! { - let enum_de_from_pb = #ty::try_from(&pb.#member).unwrap(); + let enum_de_from_pb = #ty::from(&pb.#member); o.#member = enum_de_from_pb; }) @@ -192,7 +198,7 @@ fn token_stream_for_vec(ctxt: &Ctxt, member: &syn::Member, bracketed_type: &TyIn Some(quote! { o.#member = pb.#take_ident() .into_iter() - .map(|m| #ty::try_from(m).unwrap()) + .map(|m| #ty::from(m)) .collect(); }) } @@ -221,7 +227,7 @@ fn token_stream_for_map(ctxt: &Ctxt, member: &syn::Member, ty_info: &TyInfo) -> TypeCategory::Protobuf => Some(quote! { let mut m: std::collections::HashMap = std::collections::HashMap::new(); pb.#take_ident().into_iter().for_each(|(k,v)| { - m.insert(k.clone(), #ty::try_from(v).unwrap()); + m.insert(k.clone(), #ty::from(v)); }); o.#member = m; }), diff --git a/shared-lib/flowy-derive/src/proto_buf/enum_serde.rs b/shared-lib/flowy-derive/src/proto_buf/enum_serde.rs index 498be05e51..902af74ee8 100644 --- a/shared-lib/flowy-derive/src/proto_buf/enum_serde.rs +++ b/shared-lib/flowy-derive/src/proto_buf/enum_serde.rs @@ -20,21 +20,19 @@ pub fn make_enum_token_stream(_ctxt: &Ctxt, cont: &ASTContainer) -> Option for #enum_ident { - type Error = String; - fn try_from(pb:&crate::protobuf::#pb_enum) -> Result { - Ok(match pb { + impl std::convert::From<&crate::protobuf::#pb_enum> for #enum_ident { + fn from(pb:&crate::protobuf::#pb_enum) -> Self { + match pb { #(#build_from_pb_enum)* - }) + } } } - impl std::convert::TryInto for #enum_ident { - type Error = String; - fn try_into(self) -> Result { - Ok(match self { + impl std::convert::Into for #enum_ident { + fn into(self) -> crate::protobuf::#pb_enum { + match self { #(#build_to_pb_enum)* - }) + } } } }) diff --git a/shared-lib/flowy-derive/src/proto_buf/serialize.rs b/shared-lib/flowy-derive/src/proto_buf/serialize.rs index 324c26651b..0bf1469a3c 100644 --- a/shared-lib/flowy-derive/src/proto_buf/serialize.rs +++ b/shared-lib/flowy-derive/src/proto_buf/serialize.rs @@ -19,18 +19,17 @@ pub fn make_se_token_stream(ctxt: &Ctxt, ast: &ASTContainer) -> Option Result { use protobuf::Message; - let pb: crate::protobuf::#pb_ty = self.try_into()?; + let pb: crate::protobuf::#pb_ty = self.into(); let bytes = pb.write_to_bytes()?; Ok(bytes::Bytes::from(bytes)) } } - impl std::convert::TryInto for #struct_ident { - type Error = ::protobuf::ProtobufError; - fn try_into(self) -> Result { + impl std::convert::Into for #struct_ident { + fn into(self) -> crate::protobuf::#pb_ty { let mut pb = crate::protobuf::#pb_ty::new(); #(#build_set_pb_fields)* - Ok(pb) + pb } } }; @@ -67,13 +66,13 @@ fn token_stream_for_one_of(ctxt: &Ctxt, field: &ASTField) -> Option match ident_category(bracketed_ty_info.unwrap().ident) { TypeCategory::Protobuf => Some(quote! { match self.#member { - Some(s) => { pb.#set_func(s.try_into().unwrap()) } + Some(s) => { pb.#set_func(s.into()) } None => {} } }), TypeCategory::Enum => Some(quote! { match self.#member { - Some(s) => { pb.#set_func(s.try_into().unwrap()) } + Some(s) => { pb.#set_func(s.into()) } None => {} } }), @@ -110,7 +109,7 @@ fn gen_token_stream(ctxt: &Ctxt, member: &syn::Member, ty: &syn::Type, is_option } } TypeCategory::Protobuf => { - Some(quote! { pb.#member = ::protobuf::SingularPtrField::some(self.#member.try_into().unwrap()); }) + Some(quote! { pb.#member = ::protobuf::SingularPtrField::some(self.#member.into()); }) } TypeCategory::Opt => gen_token_stream(ctxt, member, ty_info.bracket_ty_info.unwrap().ty, true), TypeCategory::Enum => { @@ -119,7 +118,7 @@ fn gen_token_stream(ctxt: &Ctxt, member: &syn::Member, ty: &syn::Type, is_option // flowy_protobuf::#pb_enum_ident::from_i32(self.#member.value()).unwrap(); // }) Some(quote! { - pb.#member = self.#member.try_into().unwrap(); + pb.#member = self.#member.into(); }) } _ => Some(quote! { pb.#member = self.#member; }), @@ -141,7 +140,7 @@ fn token_stream_for_vec(ctxt: &Ctxt, member: &syn::Member, ty: &syn::Type) -> Op pb.#member = ::protobuf::RepeatedField::from_vec( self.#member .into_iter() - .map(|m| m.try_into().unwrap()) + .map(|m| m.into()) .collect()); }), TypeCategory::Bytes => Some(quote! { pb.#member = self.#member.clone(); }), @@ -167,7 +166,7 @@ fn token_stream_for_map(ctxt: &Ctxt, member: &syn::Member, ty: &syn::Type) -> Op TypeCategory::Protobuf => Some(quote! { let mut m: std::collections::HashMap = std::collections::HashMap::new(); self.#member.into_iter().for_each(|(k,v)| { - m.insert(k.clone(), v.try_into().unwrap()); + m.insert(k.clone(), v.into()); }); pb.#member = m; }), diff --git a/shared-lib/flowy-sync/src/entities/revision.rs b/shared-lib/flowy-sync/src/entities/revision.rs index 57276bba1b..d909efa3d5 100644 --- a/shared-lib/flowy-sync/src/entities/revision.rs +++ b/shared-lib/flowy-sync/src/entities/revision.rs @@ -125,6 +125,12 @@ impl std::convert::From for RepeatedRevision { } } +impl std::convert::From> for RepeatedRevision { + fn from(revisions: Vec) -> Self { + Self { items: revisions } + } +} + impl RepeatedRevision { pub fn new(mut items: Vec) -> Self { items.sort_by(|a, b| a.rev_id.cmp(&b.rev_id)); diff --git a/shared-lib/flowy-sync/src/entities/text_block.rs b/shared-lib/flowy-sync/src/entities/text_block.rs index cef3cd30f7..8753103369 100644 --- a/shared-lib/flowy-sync/src/entities/text_block.rs +++ b/shared-lib/flowy-sync/src/entities/text_block.rs @@ -15,7 +15,7 @@ pub struct CreateTextBlockParams { } #[derive(ProtoBuf, Default, Debug, Clone, Eq, PartialEq)] -pub struct TextBlockInfoPB { +pub struct DocumentPB { #[pb(index = 1)] pub block_id: String, @@ -29,14 +29,14 @@ pub struct TextBlockInfoPB { pub base_rev_id: i64, } -impl TextBlockInfoPB { +impl DocumentPB { pub fn delta(&self) -> Result { let delta = RichTextDelta::from_bytes(&self.text)?; Ok(delta) } } -impl std::convert::TryFrom for TextBlockInfoPB { +impl std::convert::TryFrom for DocumentPB { type Error = CollaborateError; fn try_from(revision: Revision) -> Result { @@ -48,7 +48,7 @@ impl std::convert::TryFrom for TextBlockInfoPB { let delta = RichTextDelta::from_bytes(&revision.delta_data)?; let doc_json = delta.to_delta_str(); - Ok(TextBlockInfoPB { + Ok(DocumentPB { block_id: revision.object_id, text: doc_json, rev_id: revision.rev_id, diff --git a/shared-lib/flowy-sync/src/server_document/document_manager.rs b/shared-lib/flowy-sync/src/server_document/document_manager.rs index 8aed39e993..9a5499f862 100644 --- a/shared-lib/flowy-sync/src/server_document/document_manager.rs +++ b/shared-lib/flowy-sync/src/server_document/document_manager.rs @@ -1,5 +1,5 @@ use crate::{ - entities::{text_block::TextBlockInfoPB, ws_data::ServerRevisionWSDataBuilder}, + entities::{text_block::DocumentPB, ws_data::ServerRevisionWSDataBuilder}, errors::{internal_error, CollaborateError, CollaborateResult}, protobuf::{ClientRevisionWSData, RepeatedRevision as RepeatedRevisionPB, Revision as RevisionPB}, server_document::document_pad::ServerDocument, @@ -18,13 +18,13 @@ use tokio::{ }; pub trait TextBlockCloudPersistence: Send + Sync + Debug { - fn read_text_block(&self, doc_id: &str) -> BoxResultFuture; + fn read_text_block(&self, doc_id: &str) -> BoxResultFuture; fn create_text_block( &self, doc_id: &str, repeated_revision: RepeatedRevisionPB, - ) -> BoxResultFuture, CollaborateError>; + ) -> BoxResultFuture, CollaborateError>; fn read_text_block_revisions( &self, @@ -182,10 +182,7 @@ impl ServerDocumentManager { } #[tracing::instrument(level = "debug", skip(self, doc), err)] - async fn create_document_handler( - &self, - doc: TextBlockInfoPB, - ) -> Result, CollaborateError> { + async fn create_document_handler(&self, doc: DocumentPB) -> Result, CollaborateError> { let persistence = self.persistence.clone(); let handle = spawn_blocking(|| OpenDocumentHandler::new(doc, persistence)) .await @@ -209,7 +206,7 @@ struct OpenDocumentHandler { } impl OpenDocumentHandler { - fn new(doc: TextBlockInfoPB, persistence: Arc) -> Result { + fn new(doc: DocumentPB, persistence: Arc) -> Result { let doc_id = doc.block_id.clone(); let (sender, receiver) = mpsc::channel(1000); let users = DashMap::new(); diff --git a/shared-lib/flowy-sync/src/util.rs b/shared-lib/flowy-sync/src/util.rs index 824f1f945b..e0c8e0a5fa 100644 --- a/shared-lib/flowy-sync/src/util.rs +++ b/shared-lib/flowy-sync/src/util.rs @@ -2,10 +2,10 @@ use crate::{ entities::{ folder::{FolderDelta, FolderInfo}, revision::{RepeatedRevision, Revision}, - text_block::TextBlockInfoPB, + text_block::DocumentPB, }, errors::{CollaborateError, CollaborateResult}, - protobuf::{FolderInfo as FolderInfoPB, RepeatedRevision as RepeatedRevisionPB, Revision as RevisionPB}, + protobuf::{RepeatedRevision as RepeatedRevisionPB, Revision as RevisionPB}, }; use dissimilar::Chunk; use lib_ot::core::{DeltaBuilder, FlowyStr}; @@ -102,6 +102,9 @@ where pub fn repeated_revision_from_revision_pbs(revisions: Vec) -> CollaborateResult { let repeated_revision_pb = repeated_revision_pb_from_revisions(revisions); + + // let repeated_revision: RepeatedRevision = revisions.into_iter().map(Revision::); + repeated_revision_from_repeated_revision_pb(repeated_revision_pb) } @@ -151,23 +154,9 @@ pub fn pair_rev_id_from_revisions(revisions: &[Revision]) -> (i64, i64) { #[inline] pub fn make_folder_from_revisions_pb( - folder_id: &str, - revisions: RepeatedRevisionPB, -) -> Result, CollaborateError> { - match make_folder_pb_from_revisions_pb(folder_id, revisions)? { - None => Ok(None), - Some(pb) => { - let folder_info: FolderInfo = pb.try_into().map_err(|e| CollaborateError::internal().context(e))?; - Ok(Some(folder_info)) - } - } -} - -#[inline] -pub fn make_folder_pb_from_revisions_pb( folder_id: &str, mut revisions: RepeatedRevisionPB, -) -> Result, CollaborateError> { +) -> Result, CollaborateError> { let revisions = revisions.take_items(); if revisions.is_empty() { return Ok(None); @@ -187,41 +176,25 @@ pub fn make_folder_pb_from_revisions_pb( } let text = folder_delta.to_delta_str(); - let mut folder_info = FolderInfoPB::new(); - folder_info.set_folder_id(folder_id.to_owned()); - folder_info.set_text(text); - folder_info.set_base_rev_id(base_rev_id); - folder_info.set_rev_id(rev_id); - Ok(Some(folder_info)) + Ok(Some(FolderInfo { + folder_id: folder_id.to_string(), + text, + rev_id, + base_rev_id, + })) } #[inline] -pub fn make_document_info_from_revisions_pb( - doc_id: &str, - revisions: RepeatedRevisionPB, -) -> Result, CollaborateError> { - match make_document_info_pb_from_revisions_pb(doc_id, revisions)? { - None => Ok(None), - Some(pb) => { - let document_info: TextBlockInfoPB = pb.try_into().map_err(|e| { - CollaborateError::internal().context(format!("Deserialize document info from pb failed: {}", e)) - })?; - Ok(Some(document_info)) - } - } -} - -#[inline] -pub fn make_document_info_pb_from_revisions_pb( +pub fn make_document_from_revision_pbs( doc_id: &str, mut revisions: RepeatedRevisionPB, -) -> Result, CollaborateError> { +) -> Result, CollaborateError> { let revisions = revisions.take_items(); if revisions.is_empty() { return Ok(None); } - let mut document_delta = RichTextDelta::new(); + let mut delta = RichTextDelta::new(); let mut base_rev_id = 0; let mut rev_id = 0; for revision in revisions { @@ -232,17 +205,18 @@ pub fn make_document_info_pb_from_revisions_pb( tracing::warn!("revision delta_data is empty"); } - let delta = RichTextDelta::from_bytes(revision.delta_data)?; - document_delta = document_delta.compose(&delta)?; + let new_delta = RichTextDelta::from_bytes(revision.delta_data)?; + delta = delta.compose(&new_delta)?; } - let text = document_delta.to_delta_str(); - let mut block_info = crate::protobuf::TextBlockInfoPB::new(); - block_info.set_block_id(doc_id.to_owned()); - block_info.set_text(text); - block_info.set_base_rev_id(base_rev_id); - block_info.set_rev_id(rev_id); - Ok(Some(block_info)) + let text = delta.to_delta_str(); + + Ok(Some(DocumentPB { + block_id: doc_id.to_owned(), + text, + rev_id, + base_rev_id, + })) } #[inline]