diff --git a/rust-lib/dart-ffi/Cargo.toml b/rust-lib/dart-ffi/Cargo.toml index 9ca697fef4..91c33cf435 100644 --- a/rust-lib/dart-ffi/Cargo.toml +++ b/rust-lib/dart-ffi/Cargo.toml @@ -22,6 +22,8 @@ protobuf = {version = "2.20.0"} lazy_static = {version = "1.4.0"} tokio = { version = "1", features = ["sync"] } log = "0.4.14" +serde = { version = "1.0", features = ["derive"] } +serde_json = {version = "1.0"} flowy-sys = {path = "../flowy-sys"} flowy-sdk = {path = "../flowy-sdk"} \ No newline at end of file diff --git a/rust-lib/dart-ffi/src/lib.rs b/rust-lib/dart-ffi/src/lib.rs index cdf7d33d57..27d803128c 100644 --- a/rust-lib/dart-ffi/src/lib.rs +++ b/rust-lib/dart-ffi/src/lib.rs @@ -36,7 +36,7 @@ pub extern "C" fn async_command(port: i64, input: *const u8, len: usize) { }); if !payload.is_empty() { - request = request.payload(Payload::Bytes(bytes)); + request = request.payload(Payload::Bytes(payload)); } async_send(request); diff --git a/rust-lib/flowy-sys/Cargo.toml b/rust-lib/flowy-sys/Cargo.toml index c8633a83da..c108b0e74a 100644 --- a/rust-lib/flowy-sys/Cargo.toml +++ b/rust-lib/flowy-sys/Cargo.toml @@ -22,11 +22,11 @@ thread-id = "3.3.0" lazy_static = "1.4.0" dyn-clone = "1.0" derivative = "2.2.0" +serde_json = {version = "1.0"} +serde = { version = "1.0", features = ["derive"] } #optional crate bincode = { version = "1.3", optional = true} -serde = { version = "1.0", features = ["derive"], optional = true } -serde_json = {version = "1.0", optional = true} protobuf = {version = "2.24.1", optional = true} [dev-dependencies] @@ -34,5 +34,5 @@ tokio = { version = "1", features = ["full"] } futures-util = "0.3.15" [features] -use_serde = ["bincode", "serde", "serde_json"] +use_serde = ["bincode"] use_protobuf= ["protobuf"] diff --git a/rust-lib/flowy-sys/src/error/error.rs b/rust-lib/flowy-sys/src/error/error.rs index 1068dfecbe..671ab7c982 100644 --- a/rust-lib/flowy-sys/src/error/error.rs +++ b/rust-lib/flowy-sys/src/error/error.rs @@ -1,14 +1,12 @@ use crate::{ request::EventRequest, - response::{EventResponse, EventResponseBuilder, StatusCode}, + response::{EventResponse, ResponseBuilder, StatusCode}, }; use dyn_clone::DynClone; +use serde::{Serialize, Serializer}; use std::{fmt, option::NoneError}; use tokio::sync::mpsc::error::SendError; -#[cfg(feature = "use_serde")] -use serde::{Serialize, Serializer}; - pub trait Error: fmt::Debug + fmt::Display + DynClone { fn status_code(&self) -> StatusCode; @@ -109,11 +107,10 @@ where } .into(); - EventResponseBuilder::Err().error(error).build() + ResponseBuilder::Err().error(error).build() } } -#[cfg(feature = "use_serde")] impl Serialize for SystemError { fn serialize(&self, serializer: S) -> Result<::Ok, ::Error> where diff --git a/rust-lib/flowy-sys/src/error/mod.rs b/rust-lib/flowy-sys/src/error/mod.rs index 3925f93ff7..feb53da209 100644 --- a/rust-lib/flowy-sys/src/error/mod.rs +++ b/rust-lib/flowy-sys/src/error/mod.rs @@ -1,3 +1,5 @@ mod error; +pub type ResponseResult = std::result::Result, E>; + pub use error::*; diff --git a/rust-lib/flowy-sys/src/request/request.rs b/rust-lib/flowy-sys/src/request/request.rs index d75999e4a0..dac95ade6d 100644 --- a/rust-lib/flowy-sys/src/request/request.rs +++ b/rust-lib/flowy-sys/src/request/request.rs @@ -8,6 +8,7 @@ use crate::{ util::ready::{ready, Ready}, }; +use crate::response::{EventResponse, ResponseBuilder}; use futures_core::ready; use std::{ fmt::Debug, @@ -105,24 +106,24 @@ where } } -pub struct In(pub T); +pub struct Data(pub T); -impl In { +impl Data { pub fn into_inner(self) -> T { self.0 } } -impl ops::Deref for In { +impl ops::Deref for Data { type Target = T; fn deref(&self) -> &T { &self.0 } } -impl ops::DerefMut for In { +impl ops::DerefMut for Data { fn deref_mut(&mut self) -> &mut T { &mut self.0 } } #[cfg(feature = "use_serde")] -impl FromRequest for In +impl FromRequest for Data where T: serde::de::DeserializeOwned + 'static, { @@ -135,16 +136,20 @@ where Payload::None => ready(Err(unexpected_none_payload(req))), Payload::Bytes(bytes) => { let data: T = bincode::deserialize(bytes).unwrap(); - ready(Ok(In(data))) + ready(Ok(Data(data))) }, } } } -#[cfg(feature = "use_protobuf")] -impl FromRequest for In +pub trait FromBytes: Sized { + fn parse_from_bytes(bytes: &Vec) -> Result; +} + +#[cfg(not(feature = "use_serde"))] +impl FromRequest for Data where - T: ::protobuf::Message + 'static, + T: FromBytes + 'static, { type Error = SystemError; type Future = Ready>; @@ -154,8 +159,8 @@ where match payload { Payload::None => ready(Err(unexpected_none_payload(req))), Payload::Bytes(bytes) => { - let data: T = ::protobuf::Message::parse_from_bytes(bytes).unwrap(); - ready(Ok(In(data))) + let data = T::parse_from_bytes(bytes).unwrap(); + ready(Ok(Data(data))) }, } } diff --git a/rust-lib/flowy-sys/src/response/builder.rs b/rust-lib/flowy-sys/src/response/builder.rs index 72603537f3..4daedf36da 100644 --- a/rust-lib/flowy-sys/src/response/builder.rs +++ b/rust-lib/flowy-sys/src/response/builder.rs @@ -6,19 +6,19 @@ use crate::{ macro_rules! static_response { ($name:ident, $status:expr) => { #[allow(non_snake_case, missing_docs)] - pub fn $name() -> EventResponseBuilder { EventResponseBuilder::new($status) } + pub fn $name() -> ResponseBuilder { ResponseBuilder::new($status) } }; } -pub struct EventResponseBuilder { +pub struct ResponseBuilder { pub data: T, pub status: StatusCode, pub error: Option, } -impl EventResponseBuilder { +impl ResponseBuilder { pub fn new(status: StatusCode) -> Self { - EventResponseBuilder { + ResponseBuilder { data: ResponseData::None, status, error: None, diff --git a/rust-lib/flowy-sys/src/response/data.rs b/rust-lib/flowy-sys/src/response/data.rs index c0402d8ae9..9115174204 100644 --- a/rust-lib/flowy-sys/src/response/data.rs +++ b/rust-lib/flowy-sys/src/response/data.rs @@ -1,10 +1,9 @@ use bytes::{Buf, Bytes}; -#[cfg(feature = "use_serde")] use serde::{Deserialize, Serialize}; use std::{fmt, fmt::Formatter}; #[derive(Debug, Clone)] -#[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))] +// #[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))] pub enum ResponseData { Bytes(Vec), None, diff --git a/rust-lib/flowy-sys/src/response/responder.rs b/rust-lib/flowy-sys/src/response/responder.rs index a705bf6230..fb889778db 100644 --- a/rust-lib/flowy-sys/src/response/responder.rs +++ b/rust-lib/flowy-sys/src/response/responder.rs @@ -1,7 +1,7 @@ use crate::{ error::SystemError, - request::EventRequest, - response::{EventResponse, EventResponseBuilder}, + request::{Data, EventRequest}, + response::{EventResponse, ResponseBuilder}, }; use bytes::Bytes; use std::ops; @@ -14,7 +14,7 @@ macro_rules! impl_responder { ($res: ty) => { impl Responder for $res { fn respond_to(self, _: &EventRequest) -> EventResponse { - EventResponseBuilder::Ok().data(self).build() + ResponseBuilder::Ok().data(self).build() } } }; @@ -38,56 +38,46 @@ where } } -pub struct Out(pub T); - -impl Out { - pub fn into_inner(self) -> T { self.0 } -} - -impl ops::Deref for Out { - type Target = T; - - fn deref(&self) -> &T { &self.0 } -} - -impl ops::DerefMut for Out { - fn deref_mut(&mut self) -> &mut T { &mut self.0 } +pub trait ToBytes { + fn into_bytes(self) -> Result, SystemError>; } #[cfg(feature = "use_serde")] -impl Responder for Out +impl Responder for Data where T: serde::Serialize, -{ - fn respond_to(self, request: &EventRequest) -> EventResponse { - let bytes: Vec = bincode::serialize(&self.0).unwrap(); - EventResponseBuilder::Ok().data(bytes).build() - } -} - -#[cfg(feature = "use_serde")] -impl std::convert::From for Out -where - T: serde::Serialize, -{ - fn from(val: T) -> Self { Out(val) } -} - -#[cfg(feature = "use_protobuf")] -impl Responder for Out -where - T: ::protobuf::Message, { fn respond_to(self, _request: &EventRequest) -> EventResponse { - let bytes: Vec = self.write_to_bytes().unwrap(); - EventResponseBuilder::Ok().data(bytes).build() + let bytes: Vec = bincode::serialize(&self.0).unwrap(); + ResponseBuilder::Ok().data(bytes).build() } } -#[cfg(feature = "use_protobuf")] -impl std::convert::From for Out +#[cfg(feature = "use_serde")] +impl std::convert::From for Data where - T: ::protobuf::Message, + T: serde::Serialize, { - fn from(val: T) -> Self { Out(val) } + fn from(val: T) -> Self { Data(val) } +} + +#[cfg(not(feature = "use_serde"))] +impl Responder for Data +where + T: ToBytes, +{ + fn respond_to(self, _request: &EventRequest) -> EventResponse { + match self.into_inner().into_bytes() { + Ok(bytes) => ResponseBuilder::Ok().data(bytes.to_vec()).build(), + Err(e) => e.into(), + } + } +} + +#[cfg(not(feature = "use_serde"))] +impl std::convert::From for Data +where + T: ToBytes, +{ + fn from(val: T) -> Self { Data(val) } } diff --git a/rust-lib/flowy-sys/src/response/response.rs b/rust-lib/flowy-sys/src/response/response.rs index 2cee8cb151..6c0324feee 100644 --- a/rust-lib/flowy-sys/src/response/response.rs +++ b/rust-lib/flowy-sys/src/response/response.rs @@ -4,12 +4,12 @@ use crate::{ response::{data::ResponseData, Responder}, }; -#[cfg(feature = "use_serde")] +use crate::request::Data; use serde::{Deserialize, Serialize, Serializer}; use std::{fmt, fmt::Formatter}; #[derive(Clone, Debug, Eq, PartialEq)] -#[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))] +// #[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))] pub enum StatusCode { Ok = 0, Err = 1, @@ -17,7 +17,7 @@ pub enum StatusCode { // serde user guide: https://serde.rs/field-attrs.html #[derive(Debug, Clone)] -#[cfg_attr(feature = "use_serde", derive(Serialize))] +// #[cfg_attr(feature = "use_serde", derive(Serialize))] pub struct EventResponse { pub data: ResponseData, pub status: StatusCode, @@ -79,3 +79,10 @@ where ResponseData::None => serializer.serialize_str(""), } } + +pub fn response_ok(data: T) -> Result, E> +where + E: Into, +{ + Ok(Data(data)) +} diff --git a/rust-lib/flowy-user/Cargo.toml b/rust-lib/flowy-user/Cargo.toml index 1964be2777..b44094a8a1 100644 --- a/rust-lib/flowy-user/Cargo.toml +++ b/rust-lib/flowy-user/Cargo.toml @@ -7,7 +7,7 @@ edition = "2018" [dependencies] derive_more = {version = "0.99", features = ["display"]} -flowy-sys = { path = "../flowy-sys", features = ["use_serde"] } +flowy-sys = { path = "../flowy-sys" } flowy-log = { path = "../flowy-log" } tracing = { version = "0.1", features = ["log"] } bytes = "0.5" diff --git a/rust-lib/flowy-user/src/handlers/auth.rs b/rust-lib/flowy-user/src/handlers/auth.rs index a0f0970141..be4f2aa8e4 100644 --- a/rust-lib/flowy-user/src/handlers/auth.rs +++ b/rust-lib/flowy-user/src/handlers/auth.rs @@ -1,6 +1,6 @@ use crate::domain::{User, UserEmail, UserName}; use bytes::Bytes; -use flowy_sys::prelude::{In, Out}; +use flowy_sys::prelude::{response_ok, Data, FromBytes, ResponseResult, SystemError, ToBytes}; use std::convert::TryInto; // tracing instrument 👉🏻 https://docs.rs/tracing/0.1.26/tracing/attr.instrument.html @@ -12,10 +12,10 @@ use std::convert::TryInto; name = %data.name ) )] -pub async fn user_check(data: In) -> Result, String> { +pub async fn user_check(data: Data) -> ResponseResult { let user: User = data.into_inner().try_into()?; - Ok(UserStatus { is_login: false }.into()) + response_ok(UserStatus { is_login: false }) } #[derive(serde::Serialize)] @@ -23,6 +23,14 @@ pub struct UserStatus { is_login: bool, } +impl FromBytes for UserData { + fn parse_from_bytes(bytes: &Vec) -> Result { unimplemented!() } +} + +impl ToBytes for UserStatus { + fn into_bytes(self) -> Result, SystemError> { unimplemented!() } +} + #[derive(Debug, serde::Deserialize, serde::Serialize)] pub struct UserData { name: String,