mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
chore: replace try_from with from
This commit is contained in:
parent
2981b685bc
commit
e7c672cb7e
@ -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<String>) {
|
||||
}
|
||||
|
||||
#[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::<TextBlockInfoPB>()
|
||||
.parse::<DocumentPB>()
|
||||
}
|
||||
|
||||
pub async fn read_trash(sdk: &FlowySDKTest) -> RepeatedTrash {
|
||||
|
@ -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<Option<TextBlockInfoPB>, FlowyError> {
|
||||
fn read_block(&self, token: &str, params: TextBlockIdPB) -> FutureResult<Option<DocumentPB>, 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<Option<TextBlockInfoPB>, FlowyError> {
|
||||
) -> Result<Option<DocumentPB>, FlowyError> {
|
||||
let doc = request_builder()
|
||||
.get(&url.to_owned())
|
||||
.header(HEADER_TOKEN, token)
|
||||
|
@ -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<TextBlockInfoPB, CollaborateError> {
|
||||
fn read_text_block(&self, doc_id: &str) -> BoxResultFuture<DocumentPB, CollaborateError> {
|
||||
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<Option<TextBlockInfoPB>, CollaborateError> {
|
||||
) -> BoxResultFuture<Option<DocumentPB>, 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)
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -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<Option<TextBlockInfoPB>, FlowyError> {
|
||||
let doc = TextBlockInfoPB {
|
||||
fn read_block(&self, _token: &str, params: TextBlockIdPB) -> FutureResult<Option<DocumentPB>, FlowyError> {
|
||||
let doc = DocumentPB {
|
||||
block_id: params.value,
|
||||
text: initial_quill_delta_string(),
|
||||
rev_id: 0,
|
||||
|
@ -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<Revision>) -> FlowyResult<Self::Output> {
|
||||
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::<TextBlockInfoPB, FlowyError>::Ok(TextBlockInfoPB {
|
||||
Result::<DocumentPB, FlowyError>::Ok(DocumentPB {
|
||||
block_id: object_id.to_owned(),
|
||||
text: delta.to_delta_str(),
|
||||
rev_id,
|
||||
|
@ -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<Option<TextBlockInfoPB>, FlowyError>;
|
||||
fn read_block(&self, token: &str, params: TextBlockIdPB) -> FutureResult<Option<DocumentPB>, FlowyError>;
|
||||
|
||||
fn update_block(&self, token: &str, params: ResetTextBlockParams) -> FutureResult<(), FlowyError>;
|
||||
}
|
||||
|
@ -25,8 +25,15 @@ pub fn make_de_token_steam(ctxt: &Ctxt, ast: &ASTContainer) -> Option<TokenStrea
|
||||
impl std::convert::TryFrom<bytes::Bytes> for #struct_ident {
|
||||
type Error = ::protobuf::ProtobufError;
|
||||
fn try_from(bytes: bytes::Bytes) -> Result<Self, Self::Error> {
|
||||
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<Self, Self::Error> {
|
||||
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<TokenStrea
|
||||
type Error = ::protobuf::ProtobufError;
|
||||
fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
|
||||
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<crate::protobuf::#pb_ty> for #struct_ident {
|
||||
type Error = ::protobuf::ProtobufError;
|
||||
fn try_from(mut pb: crate::protobuf::#pb_ty) -> Result<Self, Self::Error> {
|
||||
impl std::convert::From<crate::protobuf::#pb_ty> 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<TokenStream>
|
||||
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<TokenStream>
|
||||
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<String, #ty> = 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;
|
||||
}),
|
||||
|
@ -20,21 +20,19 @@ pub fn make_enum_token_stream(_ctxt: &Ctxt, cont: &ASTContainer) -> Option<Token
|
||||
});
|
||||
|
||||
Some(quote! {
|
||||
impl std::convert::TryFrom<&crate::protobuf::#pb_enum> for #enum_ident {
|
||||
type Error = String;
|
||||
fn try_from(pb:&crate::protobuf::#pb_enum) -> Result<Self, Self::Error> {
|
||||
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<crate::protobuf::#pb_enum> for #enum_ident {
|
||||
type Error = String;
|
||||
fn try_into(self) -> Result<crate::protobuf::#pb_enum, Self::Error> {
|
||||
Ok(match self {
|
||||
impl std::convert::Into<crate::protobuf::#pb_enum> for #enum_ident {
|
||||
fn into(self) -> crate::protobuf::#pb_enum {
|
||||
match self {
|
||||
#(#build_to_pb_enum)*
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
@ -19,18 +19,17 @@ pub fn make_se_token_stream(ctxt: &Ctxt, ast: &ASTContainer) -> Option<TokenStre
|
||||
type Error = ::protobuf::ProtobufError;
|
||||
fn try_into(self) -> Result<bytes::Bytes, Self::Error> {
|
||||
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<crate::protobuf::#pb_ty> for #struct_ident {
|
||||
type Error = ::protobuf::ProtobufError;
|
||||
fn try_into(self) -> Result<crate::protobuf::#pb_ty, Self::Error> {
|
||||
impl std::convert::Into<crate::protobuf::#pb_ty> 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<TokenStream>
|
||||
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<String, crate::protobuf::#value_ty> = 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;
|
||||
}),
|
||||
|
@ -125,6 +125,12 @@ impl std::convert::From<Revision> for RepeatedRevision {
|
||||
}
|
||||
}
|
||||
|
||||
impl std::convert::From<Vec<Revision>> for RepeatedRevision {
|
||||
fn from(revisions: Vec<Revision>) -> Self {
|
||||
Self { items: revisions }
|
||||
}
|
||||
}
|
||||
|
||||
impl RepeatedRevision {
|
||||
pub fn new(mut items: Vec<Revision>) -> Self {
|
||||
items.sort_by(|a, b| a.rev_id.cmp(&b.rev_id));
|
||||
|
@ -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<RichTextDelta, OTError> {
|
||||
let delta = RichTextDelta::from_bytes(&self.text)?;
|
||||
Ok(delta)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::convert::TryFrom<Revision> for TextBlockInfoPB {
|
||||
impl std::convert::TryFrom<Revision> for DocumentPB {
|
||||
type Error = CollaborateError;
|
||||
|
||||
fn try_from(revision: Revision) -> Result<Self, Self::Error> {
|
||||
@ -48,7 +48,7 @@ impl std::convert::TryFrom<Revision> 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,
|
||||
|
@ -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<TextBlockInfoPB, CollaborateError>;
|
||||
fn read_text_block(&self, doc_id: &str) -> BoxResultFuture<DocumentPB, CollaborateError>;
|
||||
|
||||
fn create_text_block(
|
||||
&self,
|
||||
doc_id: &str,
|
||||
repeated_revision: RepeatedRevisionPB,
|
||||
) -> BoxResultFuture<Option<TextBlockInfoPB>, CollaborateError>;
|
||||
) -> BoxResultFuture<Option<DocumentPB>, 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<Arc<OpenDocumentHandler>, CollaborateError> {
|
||||
async fn create_document_handler(&self, doc: DocumentPB) -> Result<Arc<OpenDocumentHandler>, 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<dyn TextBlockCloudPersistence>) -> Result<Self, CollaborateError> {
|
||||
fn new(doc: DocumentPB, persistence: Arc<dyn TextBlockCloudPersistence>) -> Result<Self, CollaborateError> {
|
||||
let doc_id = doc.block_id.clone();
|
||||
let (sender, receiver) = mpsc::channel(1000);
|
||||
let users = DashMap::new();
|
||||
|
@ -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<RevisionPB>) -> CollaborateResult<RepeatedRevision> {
|
||||
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<Option<FolderInfo>, 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<Option<FolderInfoPB>, CollaborateError> {
|
||||
) -> Result<Option<FolderInfo>, 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<Option<TextBlockInfoPB>, 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<Option<crate::protobuf::TextBlockInfoPB>, CollaborateError> {
|
||||
) -> Result<Option<DocumentPB>, 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]
|
||||
|
Loading…
Reference in New Issue
Block a user