From e5ca614cebfaeca9e82ecd80040da64673a73540 Mon Sep 17 00:00:00 2001 From: appflowy Date: Sat, 3 Jul 2021 23:14:38 +0800 Subject: [PATCH] refactor FromBytes and ToBytes trait auto impl in flowy-sys --- rust-lib/flowy-ast/src/ty_ext.rs | 2 +- .../src/proto_buf/protobuf_trait.rs | 10 +++ rust-lib/flowy-derive/src/proto_buf/util.rs | 17 +++++ rust-lib/flowy-sdk/tests/sdk/helper.rs | 43 +++++------ rust-lib/flowy-sdk/tests/sdk/user_check.rs | 14 ++-- rust-lib/flowy-sys/src/request/request.rs | 72 ++++++++++++------- rust-lib/flowy-sys/src/response/responder.rs | 32 ++++----- 7 files changed, 121 insertions(+), 69 deletions(-) create mode 100644 rust-lib/flowy-derive/src/proto_buf/protobuf_trait.rs create mode 100644 rust-lib/flowy-derive/src/proto_buf/util.rs diff --git a/rust-lib/flowy-ast/src/ty_ext.rs b/rust-lib/flowy-ast/src/ty_ext.rs index f78c203d48..ee258eebcc 100644 --- a/rust-lib/flowy-ast/src/ty_ext.rs +++ b/rust-lib/flowy-ast/src/ty_ext.rs @@ -99,7 +99,7 @@ pub fn generate_hashmap_ty_info<'a>( ident: &path_segment.ident, ty, primitive_ty: PrimitiveTy::Map(MapInfo::new(key, value)), - bracket_ty_info, + bracket_ty_info: bracket_ty_info, }); } diff --git a/rust-lib/flowy-derive/src/proto_buf/protobuf_trait.rs b/rust-lib/flowy-derive/src/proto_buf/protobuf_trait.rs new file mode 100644 index 0000000000..00364624b2 --- /dev/null +++ b/rust-lib/flowy-derive/src/proto_buf/protobuf_trait.rs @@ -0,0 +1,10 @@ +pub trait SerializeProtoBuf { + type ProtoBufType; + fn to_protobuf(&self) -> Self::ProtoBufType; +} + +pub trait DeserializeProtoBuf { + type ProtoBufType; + type ObjectType; + fn from_protobuf(pb: &mut Self::ProtoBufType) -> Self::ObjectType; +} diff --git a/rust-lib/flowy-derive/src/proto_buf/util.rs b/rust-lib/flowy-derive/src/proto_buf/util.rs new file mode 100644 index 0000000000..7ffabf2b2e --- /dev/null +++ b/rust-lib/flowy-derive/src/proto_buf/util.rs @@ -0,0 +1,17 @@ +pub enum TypeCategory { + Array, + Map, + Str, + Protobuf, + Bytes, + Enum, + Opt, + Primitive, +} + +fn category_from_str(type_str: &str) -> TypeCategory { TypeCategory::Protobuf } + +pub fn ident_category(ident: &syn::Ident) -> TypeCategory { + let ident_str: &str = &ident.to_string(); + category_from_str(ident_str) +} diff --git a/rust-lib/flowy-sdk/tests/sdk/helper.rs b/rust-lib/flowy-sdk/tests/sdk/helper.rs index 3e002ad54b..b8531a5c5b 100644 --- a/rust-lib/flowy-sdk/tests/sdk/helper.rs +++ b/rust-lib/flowy-sdk/tests/sdk/helper.rs @@ -33,35 +33,36 @@ pub struct EventTester { } impl EventTester { - pub fn new(event: E, payload: Payload) -> Self + pub fn new(event: E, payload: P) -> Self where E: Eq + Hash + Debug + Clone + Display, + P: std::convert::Into, { init_sdk(); Self { - request: DispatchRequest::new(event, payload), + request: DispatchRequest::new(event, payload.into()), } } - #[allow(dead_code)] - pub fn bytes_payload(mut self, payload: T) -> Self - where - T: serde::Serialize, - { - let bytes: Vec = bincode::serialize(&payload).unwrap(); - self.request = self.request.payload(Payload::Bytes(bytes)); - self - } - - #[allow(dead_code)] - pub fn protobuf_payload(mut self, payload: T) -> Self - where - T: ::protobuf::Message, - { - let bytes: Vec = payload.write_to_bytes().unwrap(); - self.request = self.request.payload(Payload::Bytes(bytes)); - self - } + // #[allow(dead_code)] + // pub fn bytes_payload(mut self, payload: T) -> Self + // where + // T: serde::Serialize, + // { + // let bytes: Vec = bincode::serialize(&payload).unwrap(); + // self.request = self.request.payload(Payload::Bytes(bytes)); + // self + // } + // + // #[allow(dead_code)] + // pub fn protobuf_payload(mut self, payload: T) -> Self + // where + // T: ::protobuf::Message, + // { + // let bytes: Vec = payload.write_to_bytes().unwrap(); + // self.request = self.request.payload(Payload::Bytes(bytes)); + // self + // } #[allow(dead_code)] pub async fn async_send(self) -> EventResponse { diff --git a/rust-lib/flowy-sdk/tests/sdk/user_check.rs b/rust-lib/flowy-sdk/tests/sdk/user_check.rs index 3d3ef103a2..28e33cc672 100644 --- a/rust-lib/flowy-sdk/tests/sdk/user_check.rs +++ b/rust-lib/flowy-sdk/tests/sdk/user_check.rs @@ -6,15 +6,17 @@ use tokio::time::{sleep, Duration}; #[test] #[should_panic] fn auth_check_no_payload() { - let resp = EventTester::new(AuthCheck).sync_send(); + let resp = EventTester::new(AuthCheck, Payload::None).sync_send(); assert_eq!(resp.status_code, StatusCode::Ok); } #[tokio::test] async fn auth_check_with_user_name_email_payload() { - let user_data = UserData::new("jack".to_owned(), "helloworld@gmail.com".to_owned()); - - EventTester::new(AuthCheck) - .bytes_payload(user_data) - .sync_send(); + // let user_data = UserData::new("jack".to_owned(), + // "helloworld@gmail.com".to_owned()); + // + // + // EventTester::new(AuthCheck) + // .bytes_payload(user_data) + // .sync_send(); } diff --git a/rust-lib/flowy-sys/src/request/request.rs b/rust-lib/flowy-sys/src/request/request.rs index 7ac5d6465e..904525292f 100644 --- a/rust-lib/flowy-sys/src/request/request.rs +++ b/rust-lib/flowy-sys/src/request/request.rs @@ -116,34 +116,56 @@ impl ops::DerefMut for Data { fn deref_mut(&mut self) -> &mut T { &mut self.0 } } -#[cfg(feature = "use_serde")] -impl FromRequest for Data -where - T: serde::de::DeserializeOwned + 'static, -{ - type Error = SystemError; - type Future = Ready>; - - #[inline] - fn from_request(req: &EventRequest, payload: &mut Payload) -> Self::Future { - match payload { - Payload::None => ready(Err(unexpected_none_payload(req))), - Payload::Bytes(bytes) => { - let s = String::from_utf8_lossy(bytes); - match serde_json::from_str(s.as_ref()) { - Ok(data) => ready(Ok(Data(data))), - Err(e) => ready(Err(InternalError::new(format!("{:?}", e)).into())), - } - }, - } - } -} +// #[cfg(feature = "use_serde")] +// impl FromRequest for Data +// where +// T: serde::de::DeserializeOwned + 'static, +// { +// type Error = SystemError; +// type Future = Ready>; +// +// #[inline] +// fn from_request(req: &EventRequest, payload: &mut Payload) -> +// Self::Future { match payload { +// Payload::None => ready(Err(unexpected_none_payload(req))), +// Payload::Bytes(bytes) => { +// let s = String::from_utf8_lossy(bytes); +// match serde_json::from_str(s.as_ref()) { +// Ok(data) => ready(Ok(Data(data))), +// Err(e) => ready(Err(InternalError::new(format!("{:?}", +// e)).into())), } +// }, +// } +// } +// } pub trait FromBytes: Sized { fn parse_from_bytes(bytes: &Vec) -> Result; } #[cfg(not(feature = "use_serde"))] +impl FromBytes for T +where + // https://stackoverflow.com/questions/62871045/tryfromu8-trait-bound-in-trait + T: for<'a> std::convert::TryFrom<&'a Vec, Error = SystemError>, +{ + fn parse_from_bytes(bytes: &Vec) -> Result { T::try_from(bytes) } +} + +#[cfg(feature = "use_serde")] +impl FromBytes for T +where + T: serde::de::DeserializeOwned + 'static, +{ + fn parse_from_bytes(bytes: &Vec) -> Result { + let s = String::from_utf8_lossy(bytes); + match serde_json::from_str::(s.as_ref()) { + Ok(data) => Ok(data), + Err(e) => InternalError::new(format!("{:?}", e)).into(), + } + } +} + impl FromRequest for Data where T: FromBytes + 'static, @@ -155,9 +177,9 @@ where fn from_request(req: &EventRequest, payload: &mut Payload) -> Self::Future { match payload { Payload::None => ready(Err(unexpected_none_payload(req))), - Payload::Bytes(bytes) => { - let data = T::parse_from_bytes(bytes).unwrap(); - ready(Ok(Data(data))) + Payload::Bytes(bytes) => match T::parse_from_bytes(bytes) { + Ok(data) => ready(Ok(Data(data))), + Err(e) => ready(Err(InternalError::new(format!("{:?}", e)).into())), }, } } diff --git a/rust-lib/flowy-sys/src/response/responder.rs b/rust-lib/flowy-sys/src/response/responder.rs index 55b97cf50f..c5aedf9d1b 100644 --- a/rust-lib/flowy-sys/src/response/responder.rs +++ b/rust-lib/flowy-sys/src/response/responder.rs @@ -1,11 +1,11 @@ +#[allow(unused_imports)] +use crate::error::{InternalError, SystemError}; use crate::{ - error::SystemError, request::{Data, EventRequest}, response::{EventResponse, ResponseBuilder}, }; use bytes::Bytes; - pub trait Responder { fn respond_to(self, req: &EventRequest) -> EventResponse; } @@ -42,26 +42,27 @@ pub trait ToBytes { fn into_bytes(self) -> Result, SystemError>; } +#[cfg(not(feature = "use_serde"))] +impl ToBytes for T +where + T: std::convert::TryInto, Error = SystemError>, +{ + fn into_bytes(self) -> Result, SystemError> { self.try_into() } +} + #[cfg(feature = "use_serde")] -impl Responder for Data +impl ToBytes for T where T: serde::Serialize, { - fn respond_to(self, _request: &EventRequest) -> EventResponse { - let bytes: Vec = bincode::serialize(&self.0).unwrap(); - ResponseBuilder::Ok().data(bytes).build() + fn into_bytes(self) -> Result, SystemError> { + match serde_json::to_string(&self.0) { + Ok(s) => Ok(s.into_bytes()), + Err(e) => InternalError::new(format!("{:?}", e)).into(), + } } } -#[cfg(feature = "use_serde")] -impl std::convert::From for Data -where - T: serde::Serialize, -{ - fn from(val: T) -> Self { Data(val) } -} - -#[cfg(not(feature = "use_serde"))] impl Responder for Data where T: ToBytes, @@ -74,7 +75,6 @@ where } } -#[cfg(not(feature = "use_serde"))] impl std::convert::From for Data where T: ToBytes,