mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
refactor FromBytes and ToBytes trait auto impl in flowy-sys
This commit is contained in:
parent
4a82d7157c
commit
e5ca614ceb
@ -99,7 +99,7 @@ pub fn generate_hashmap_ty_info<'a>(
|
|||||||
ident: &path_segment.ident,
|
ident: &path_segment.ident,
|
||||||
ty,
|
ty,
|
||||||
primitive_ty: PrimitiveTy::Map(MapInfo::new(key, value)),
|
primitive_ty: PrimitiveTy::Map(MapInfo::new(key, value)),
|
||||||
bracket_ty_info,
|
bracket_ty_info: bracket_ty_info,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
10
rust-lib/flowy-derive/src/proto_buf/protobuf_trait.rs
Normal file
10
rust-lib/flowy-derive/src/proto_buf/protobuf_trait.rs
Normal file
@ -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;
|
||||||
|
}
|
17
rust-lib/flowy-derive/src/proto_buf/util.rs
Normal file
17
rust-lib/flowy-derive/src/proto_buf/util.rs
Normal file
@ -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)
|
||||||
|
}
|
@ -33,35 +33,36 @@ pub struct EventTester {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl EventTester {
|
impl EventTester {
|
||||||
pub fn new<E>(event: E, payload: Payload) -> Self
|
pub fn new<E, P>(event: E, payload: P) -> Self
|
||||||
where
|
where
|
||||||
E: Eq + Hash + Debug + Clone + Display,
|
E: Eq + Hash + Debug + Clone + Display,
|
||||||
|
P: std::convert::Into<Payload>,
|
||||||
{
|
{
|
||||||
init_sdk();
|
init_sdk();
|
||||||
Self {
|
Self {
|
||||||
request: DispatchRequest::new(event, payload),
|
request: DispatchRequest::new(event, payload.into()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
// #[allow(dead_code)]
|
||||||
pub fn bytes_payload<T>(mut self, payload: T) -> Self
|
// pub fn bytes_payload<T>(mut self, payload: T) -> Self
|
||||||
where
|
// where
|
||||||
T: serde::Serialize,
|
// T: serde::Serialize,
|
||||||
{
|
// {
|
||||||
let bytes: Vec<u8> = bincode::serialize(&payload).unwrap();
|
// let bytes: Vec<u8> = bincode::serialize(&payload).unwrap();
|
||||||
self.request = self.request.payload(Payload::Bytes(bytes));
|
// self.request = self.request.payload(Payload::Bytes(bytes));
|
||||||
self
|
// self
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
#[allow(dead_code)]
|
// #[allow(dead_code)]
|
||||||
pub fn protobuf_payload<T>(mut self, payload: T) -> Self
|
// pub fn protobuf_payload<T>(mut self, payload: T) -> Self
|
||||||
where
|
// where
|
||||||
T: ::protobuf::Message,
|
// T: ::protobuf::Message,
|
||||||
{
|
// {
|
||||||
let bytes: Vec<u8> = payload.write_to_bytes().unwrap();
|
// let bytes: Vec<u8> = payload.write_to_bytes().unwrap();
|
||||||
self.request = self.request.payload(Payload::Bytes(bytes));
|
// self.request = self.request.payload(Payload::Bytes(bytes));
|
||||||
self
|
// self
|
||||||
}
|
// }
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub async fn async_send(self) -> EventResponse {
|
pub async fn async_send(self) -> EventResponse {
|
||||||
|
@ -6,15 +6,17 @@ use tokio::time::{sleep, Duration};
|
|||||||
#[test]
|
#[test]
|
||||||
#[should_panic]
|
#[should_panic]
|
||||||
fn auth_check_no_payload() {
|
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);
|
assert_eq!(resp.status_code, StatusCode::Ok);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn auth_check_with_user_name_email_payload() {
|
async fn auth_check_with_user_name_email_payload() {
|
||||||
let user_data = UserData::new("jack".to_owned(), "helloworld@gmail.com".to_owned());
|
// let user_data = UserData::new("jack".to_owned(),
|
||||||
|
// "helloworld@gmail.com".to_owned());
|
||||||
EventTester::new(AuthCheck)
|
//
|
||||||
.bytes_payload(user_data)
|
//
|
||||||
.sync_send();
|
// EventTester::new(AuthCheck)
|
||||||
|
// .bytes_payload(user_data)
|
||||||
|
// .sync_send();
|
||||||
}
|
}
|
||||||
|
@ -116,34 +116,56 @@ impl<T> ops::DerefMut for Data<T> {
|
|||||||
fn deref_mut(&mut self) -> &mut T { &mut self.0 }
|
fn deref_mut(&mut self) -> &mut T { &mut self.0 }
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "use_serde")]
|
// #[cfg(feature = "use_serde")]
|
||||||
impl<T> FromRequest for Data<T>
|
// impl<T> FromRequest for Data<T>
|
||||||
where
|
// where
|
||||||
T: serde::de::DeserializeOwned + 'static,
|
// T: serde::de::DeserializeOwned + 'static,
|
||||||
{
|
// {
|
||||||
type Error = SystemError;
|
// type Error = SystemError;
|
||||||
type Future = Ready<Result<Self, SystemError>>;
|
// type Future = Ready<Result<Self, SystemError>>;
|
||||||
|
//
|
||||||
#[inline]
|
// #[inline]
|
||||||
fn from_request(req: &EventRequest, payload: &mut Payload) -> Self::Future {
|
// fn from_request(req: &EventRequest, payload: &mut Payload) ->
|
||||||
match payload {
|
// Self::Future { match payload {
|
||||||
Payload::None => ready(Err(unexpected_none_payload(req))),
|
// Payload::None => ready(Err(unexpected_none_payload(req))),
|
||||||
Payload::Bytes(bytes) => {
|
// Payload::Bytes(bytes) => {
|
||||||
let s = String::from_utf8_lossy(bytes);
|
// let s = String::from_utf8_lossy(bytes);
|
||||||
match serde_json::from_str(s.as_ref()) {
|
// match serde_json::from_str(s.as_ref()) {
|
||||||
Ok(data) => ready(Ok(Data(data))),
|
// Ok(data) => ready(Ok(Data(data))),
|
||||||
Err(e) => ready(Err(InternalError::new(format!("{:?}", e)).into())),
|
// Err(e) => ready(Err(InternalError::new(format!("{:?}",
|
||||||
}
|
// e)).into())), }
|
||||||
},
|
// },
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
pub trait FromBytes: Sized {
|
pub trait FromBytes: Sized {
|
||||||
fn parse_from_bytes(bytes: &Vec<u8>) -> Result<Self, SystemError>;
|
fn parse_from_bytes(bytes: &Vec<u8>) -> Result<Self, SystemError>;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "use_serde"))]
|
#[cfg(not(feature = "use_serde"))]
|
||||||
|
impl<T> FromBytes for T
|
||||||
|
where
|
||||||
|
// https://stackoverflow.com/questions/62871045/tryfromu8-trait-bound-in-trait
|
||||||
|
T: for<'a> std::convert::TryFrom<&'a Vec<u8>, Error = SystemError>,
|
||||||
|
{
|
||||||
|
fn parse_from_bytes(bytes: &Vec<u8>) -> Result<Self, SystemError> { T::try_from(bytes) }
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "use_serde")]
|
||||||
|
impl<T> FromBytes for T
|
||||||
|
where
|
||||||
|
T: serde::de::DeserializeOwned + 'static,
|
||||||
|
{
|
||||||
|
fn parse_from_bytes(bytes: &Vec<u8>) -> Result<Self, SystemError> {
|
||||||
|
let s = String::from_utf8_lossy(bytes);
|
||||||
|
match serde_json::from_str::<T>(s.as_ref()) {
|
||||||
|
Ok(data) => Ok(data),
|
||||||
|
Err(e) => InternalError::new(format!("{:?}", e)).into(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<T> FromRequest for Data<T>
|
impl<T> FromRequest for Data<T>
|
||||||
where
|
where
|
||||||
T: FromBytes + 'static,
|
T: FromBytes + 'static,
|
||||||
@ -155,9 +177,9 @@ where
|
|||||||
fn from_request(req: &EventRequest, payload: &mut Payload) -> Self::Future {
|
fn from_request(req: &EventRequest, payload: &mut Payload) -> Self::Future {
|
||||||
match payload {
|
match payload {
|
||||||
Payload::None => ready(Err(unexpected_none_payload(req))),
|
Payload::None => ready(Err(unexpected_none_payload(req))),
|
||||||
Payload::Bytes(bytes) => {
|
Payload::Bytes(bytes) => match T::parse_from_bytes(bytes) {
|
||||||
let data = T::parse_from_bytes(bytes).unwrap();
|
Ok(data) => ready(Ok(Data(data))),
|
||||||
ready(Ok(Data(data)))
|
Err(e) => ready(Err(InternalError::new(format!("{:?}", e)).into())),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
|
#[allow(unused_imports)]
|
||||||
|
use crate::error::{InternalError, SystemError};
|
||||||
use crate::{
|
use crate::{
|
||||||
error::SystemError,
|
|
||||||
request::{Data, EventRequest},
|
request::{Data, EventRequest},
|
||||||
response::{EventResponse, ResponseBuilder},
|
response::{EventResponse, ResponseBuilder},
|
||||||
};
|
};
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
|
|
||||||
|
|
||||||
pub trait Responder {
|
pub trait Responder {
|
||||||
fn respond_to(self, req: &EventRequest) -> EventResponse;
|
fn respond_to(self, req: &EventRequest) -> EventResponse;
|
||||||
}
|
}
|
||||||
@ -42,26 +42,27 @@ pub trait ToBytes {
|
|||||||
fn into_bytes(self) -> Result<Vec<u8>, SystemError>;
|
fn into_bytes(self) -> Result<Vec<u8>, SystemError>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(not(feature = "use_serde"))]
|
||||||
|
impl<T> ToBytes for T
|
||||||
|
where
|
||||||
|
T: std::convert::TryInto<Vec<u8>, Error = SystemError>,
|
||||||
|
{
|
||||||
|
fn into_bytes(self) -> Result<Vec<u8>, SystemError> { self.try_into() }
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(feature = "use_serde")]
|
#[cfg(feature = "use_serde")]
|
||||||
impl<T> Responder for Data<T>
|
impl<T> ToBytes for T
|
||||||
where
|
where
|
||||||
T: serde::Serialize,
|
T: serde::Serialize,
|
||||||
{
|
{
|
||||||
fn respond_to(self, _request: &EventRequest) -> EventResponse {
|
fn into_bytes(self) -> Result<Vec<u8>, SystemError> {
|
||||||
let bytes: Vec<u8> = bincode::serialize(&self.0).unwrap();
|
match serde_json::to_string(&self.0) {
|
||||||
ResponseBuilder::Ok().data(bytes).build()
|
Ok(s) => Ok(s.into_bytes()),
|
||||||
|
Err(e) => InternalError::new(format!("{:?}", e)).into(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "use_serde")]
|
|
||||||
impl<T> std::convert::From<T> for Data<T>
|
|
||||||
where
|
|
||||||
T: serde::Serialize,
|
|
||||||
{
|
|
||||||
fn from(val: T) -> Self { Data(val) }
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(not(feature = "use_serde"))]
|
|
||||||
impl<T> Responder for Data<T>
|
impl<T> Responder for Data<T>
|
||||||
where
|
where
|
||||||
T: ToBytes,
|
T: ToBytes,
|
||||||
@ -74,7 +75,6 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "use_serde"))]
|
|
||||||
impl<T> std::convert::From<T> for Data<T>
|
impl<T> std::convert::From<T> for Data<T>
|
||||||
where
|
where
|
||||||
T: ToBytes,
|
T: ToBytes,
|
||||||
|
Loading…
Reference in New Issue
Block a user