From 4a82d7157ce71c7daf69e3e4ef29032e265ad9f3 Mon Sep 17 00:00:00 2001 From: appflowy Date: Sat, 3 Jul 2021 22:24:02 +0800 Subject: [PATCH] replace response data with payload --- rust-lib/dart-ffi/src/lib.rs | 52 ++++++++++++++------ rust-lib/flowy-sdk/tests/sdk/helper.rs | 7 ++- rust-lib/flowy-sdk/tests/sdk/user_check.rs | 2 +- rust-lib/flowy-sys/src/dispatch.rs | 15 ++---- rust-lib/flowy-sys/src/request/payload.rs | 37 +++++++++++++- rust-lib/flowy-sys/src/response/builder.rs | 17 +++---- rust-lib/flowy-sys/src/response/data.rs | 42 ---------------- rust-lib/flowy-sys/src/response/mod.rs | 2 - rust-lib/flowy-sys/src/response/response.rs | 53 +++++---------------- rust-lib/flowy-sys/tests/api/module.rs | 2 +- 10 files changed, 104 insertions(+), 125 deletions(-) delete mode 100644 rust-lib/flowy-sys/src/response/data.rs diff --git a/rust-lib/dart-ffi/src/lib.rs b/rust-lib/dart-ffi/src/lib.rs index 290826fe0a..5614b1f08a 100644 --- a/rust-lib/dart-ffi/src/lib.rs +++ b/rust-lib/dart-ffi/src/lib.rs @@ -1,6 +1,6 @@ mod c; -use crate::c::forget_rust; +use crate::c::{extend_front_four_bytes_into_bytes, forget_rust}; use flowy_sdk::*; use flowy_sys::prelude::*; use lazy_static::lazy_static; @@ -25,22 +25,18 @@ pub extern "C" fn init_sdk(path: *mut c_char) -> i64 { #[no_mangle] pub extern "C" fn async_command(port: i64, input: *const u8, len: usize) { - let FFICommand { event, payload } = FFICommand::from_u8_pointer(input, len); - let mut request = DispatchRequest::new(event); + let mut request: DispatchRequest = FFIRequest::from_u8_pointer(input, len).into(); log::trace!( "[FFI]: {} Async Event: {:?} with {} port", &request.id, &request.event, port ); - if !payload.is_empty() { - request = request.payload(Payload::Bytes(payload)); - } request = request.callback(Box::new(move |resp: EventResponse| { - let bytes = match resp.data { - ResponseData::Bytes(bytes) => bytes, - ResponseData::None => vec![], + let bytes = match resp.payload { + Payload::Bytes(bytes) => bytes, + Payload::None => vec![], }; log::trace!("[FFI]: Post data to dart through {} port", port); Box::pin(spawn_future(async { bytes }, port)) @@ -50,26 +46,42 @@ pub extern "C" fn async_command(port: i64, input: *const u8, len: usize) { } #[no_mangle] -pub extern "C" fn sync_command(input: *const u8, len: usize) -> *const u8 { unimplemented!() } +pub extern "C" fn sync_command(input: *const u8, len: usize) -> *const u8 { + let mut request: DispatchRequest = FFIRequest::from_u8_pointer(input, len).into(); + log::trace!("[FFI]: {} Sync Event: {:?}", &request.id, &request.event,); + let response = EventDispatch::sync_send(request); + + // FFIResponse { } + let response_bytes = vec![]; + let result = extend_front_four_bytes_into_bytes(&response_bytes); + forget_rust(result) +} #[inline(never)] #[no_mangle] pub extern "C" fn link_me_please() {} #[derive(serde::Deserialize)] -pub struct FFICommand { +pub struct FFIRequest { event: String, payload: Vec, } -impl FFICommand { +impl FFIRequest { pub fn from_u8_pointer(pointer: *const u8, len: usize) -> Self { let bytes = unsafe { std::slice::from_raw_parts(pointer, len) }.to_vec(); - let command: FFICommand = serde_json::from_slice(&bytes).unwrap(); - command + let request: FFIRequest = serde_json::from_slice(&bytes).unwrap(); + request } } +#[derive(serde::Serialize)] +pub struct FFIResponse { + event: String, + payload: Vec, + error: String, +} + #[inline(always)] async fn spawn_future(future: F, port: i64) where @@ -89,3 +101,15 @@ where }, } } + +impl std::convert::From for DispatchRequest { + fn from(ffi_request: FFIRequest) -> Self { + let payload = if !ffi_request.payload.is_empty() { + Payload::Bytes(ffi_request.payload) + } else { + Payload::None + }; + let mut request = DispatchRequest::new(ffi_request.event, payload); + request + } +} diff --git a/rust-lib/flowy-sdk/tests/sdk/helper.rs b/rust-lib/flowy-sdk/tests/sdk/helper.rs index 85e258f16d..3e002ad54b 100644 --- a/rust-lib/flowy-sdk/tests/sdk/helper.rs +++ b/rust-lib/flowy-sdk/tests/sdk/helper.rs @@ -33,12 +33,13 @@ pub struct EventTester { } impl EventTester { - pub fn new(event: E) -> Self + pub fn new(event: E, payload: Payload) -> Self where E: Eq + Hash + Debug + Clone + Display, { + init_sdk(); Self { - request: DispatchRequest::new(event), + request: DispatchRequest::new(event, payload), } } @@ -64,7 +65,6 @@ impl EventTester { #[allow(dead_code)] pub async fn async_send(self) -> EventResponse { - init_sdk(); let resp = async_send(self.request).await; dbg!(&resp); resp @@ -72,7 +72,6 @@ impl EventTester { #[allow(dead_code)] pub fn sync_send(self) -> EventResponse { - init_sdk(); let resp = sync_send(self.request); dbg!(&resp); resp diff --git a/rust-lib/flowy-sdk/tests/sdk/user_check.rs b/rust-lib/flowy-sdk/tests/sdk/user_check.rs index 75777eb92c..3d3ef103a2 100644 --- a/rust-lib/flowy-sdk/tests/sdk/user_check.rs +++ b/rust-lib/flowy-sdk/tests/sdk/user_check.rs @@ -7,7 +7,7 @@ use tokio::time::{sleep, Duration}; #[should_panic] fn auth_check_no_payload() { let resp = EventTester::new(AuthCheck).sync_send(); - assert_eq!(resp.status, StatusCode::Ok); + assert_eq!(resp.status_code, StatusCode::Ok); } #[tokio::test] diff --git a/rust-lib/flowy-sys/src/dispatch.rs b/rust-lib/flowy-sys/src/dispatch.rs index 46bb79297a..b7367b9a9c 100644 --- a/rust-lib/flowy-sys/src/dispatch.rs +++ b/rust-lib/flowy-sys/src/dispatch.rs @@ -16,12 +16,8 @@ use std::{ future::Future, hash::Hash, sync::RwLock, - thread::JoinHandle, -}; -use tokio::{ - macros::support::{Pin, Poll}, - task::JoinError, }; +use tokio::macros::support::{Pin, Poll}; lazy_static! { pub static ref EVENT_DISPATCH: RwLock> = RwLock::new(None); @@ -119,23 +115,18 @@ pub struct DispatchRequest { } impl DispatchRequest { - pub fn new(event: E) -> Self + pub fn new(event: E, payload: Payload) -> Self where E: Eq + Hash + Debug + Clone + Display, { Self { - payload: Payload::None, + payload, event: event.into(), id: uuid::Uuid::new_v4().to_string(), callback: None, } } - pub fn payload(mut self, payload: Payload) -> Self { - self.payload = payload; - self - } - pub fn callback(mut self, callback: BoxFutureCallback) -> Self { self.callback = Some(callback); self diff --git a/rust-lib/flowy-sys/src/request/payload.rs b/rust-lib/flowy-sys/src/request/payload.rs index ff3a6a4216..19b3ffdf5b 100644 --- a/rust-lib/flowy-sys/src/request/payload.rs +++ b/rust-lib/flowy-sys/src/request/payload.rs @@ -1,8 +1,43 @@ +use bytes::Bytes; +use std::{fmt, fmt::Formatter}; + pub enum PayloadError {} // TODO: support stream data -#[derive(Clone, Debug)] +#[derive(Clone, Debug, serde::Serialize)] pub enum Payload { None, Bytes(Vec), } + +impl std::fmt::Display for Payload { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + match self { + Payload::Bytes(bytes) => f.write_fmt(format_args!("{} bytes", bytes.len())), + Payload::None => f.write_str("Empty"), + } + } +} + +impl std::convert::Into for String { + fn into(self) -> Payload { Payload::Bytes(self.into_bytes()) } +} + +impl std::convert::Into for &'_ String { + fn into(self) -> Payload { Payload::Bytes(self.to_owned().into_bytes()) } +} + +impl std::convert::Into for Bytes { + fn into(self) -> Payload { + // Opti(nathan): do not copy the bytes? + Payload::Bytes(self.as_ref().to_vec()) + } +} + +impl std::convert::Into for Vec { + fn into(self) -> Payload { Payload::Bytes(self) } +} + +impl std::convert::Into for &str { + fn into(self) -> Payload { self.to_string().into() } +} diff --git a/rust-lib/flowy-sys/src/response/builder.rs b/rust-lib/flowy-sys/src/response/builder.rs index 4daedf36da..6834529593 100644 --- a/rust-lib/flowy-sys/src/response/builder.rs +++ b/rust-lib/flowy-sys/src/response/builder.rs @@ -1,6 +1,7 @@ use crate::{ error::SystemError, - response::{data::ResponseData, EventResponse, StatusCode}, + request::Payload, + response::{EventResponse, StatusCode}, }; macro_rules! static_response { @@ -10,8 +11,8 @@ macro_rules! static_response { }; } -pub struct ResponseBuilder { - pub data: T, +pub struct ResponseBuilder { + pub payload: T, pub status: StatusCode, pub error: Option, } @@ -19,14 +20,14 @@ pub struct ResponseBuilder { impl ResponseBuilder { pub fn new(status: StatusCode) -> Self { ResponseBuilder { - data: ResponseData::None, + payload: Payload::None, status, error: None, } } - pub fn data>(mut self, data: D) -> Self { - self.data = data.into(); + pub fn data>(mut self, data: D) -> Self { + self.payload = data.into(); self } @@ -37,8 +38,8 @@ impl ResponseBuilder { pub fn build(self) -> EventResponse { EventResponse { - data: self.data, - status: self.status, + payload: self.payload, + status_code: self.status, error: self.error, } } diff --git a/rust-lib/flowy-sys/src/response/data.rs b/rust-lib/flowy-sys/src/response/data.rs deleted file mode 100644 index 1d748f25b7..0000000000 --- a/rust-lib/flowy-sys/src/response/data.rs +++ /dev/null @@ -1,42 +0,0 @@ -use bytes::{Bytes}; - -use std::{fmt, fmt::Formatter}; - -#[derive(Debug, Clone)] -// #[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))] -pub enum ResponseData { - Bytes(Vec), - None, -} - -impl std::fmt::Display for ResponseData { - fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - match self { - ResponseData::Bytes(bytes) => f.write_fmt(format_args!("{} bytes", bytes.len())), - ResponseData::None => f.write_str("Empty"), - } - } -} - -impl std::convert::Into for String { - fn into(self) -> ResponseData { ResponseData::Bytes(self.into_bytes()) } -} - -impl std::convert::Into for &'_ String { - fn into(self) -> ResponseData { ResponseData::Bytes(self.to_owned().into_bytes()) } -} - -impl std::convert::Into for Bytes { - fn into(self) -> ResponseData { - // Opti(nathan): do not copy the bytes? - ResponseData::Bytes(self.as_ref().to_vec()) - } -} - -impl std::convert::Into for Vec { - fn into(self) -> ResponseData { ResponseData::Bytes(self) } -} - -impl std::convert::Into for &str { - fn into(self) -> ResponseData { self.to_string().into() } -} diff --git a/rust-lib/flowy-sys/src/response/mod.rs b/rust-lib/flowy-sys/src/response/mod.rs index 86a66482fc..bec9b1d796 100644 --- a/rust-lib/flowy-sys/src/response/mod.rs +++ b/rust-lib/flowy-sys/src/response/mod.rs @@ -1,9 +1,7 @@ pub use builder::*; -pub use data::*; pub use responder::*; pub use response::*; mod builder; -mod data; mod responder; mod response; diff --git a/rust-lib/flowy-sys/src/response/response.rs b/rust-lib/flowy-sys/src/response/response.rs index 5b0f10f225..3b9ef88fd3 100644 --- a/rust-lib/flowy-sys/src/response/response.rs +++ b/rust-lib/flowy-sys/src/response/response.rs @@ -1,34 +1,29 @@ use crate::{ error::SystemError, - request::EventRequest, - response::{data::ResponseData, Responder}, + request::{Data, EventRequest, Payload}, + response::Responder, }; - -use crate::request::Data; - use std::{fmt, fmt::Formatter}; -#[derive(Clone, Debug, Eq, PartialEq)] -// #[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))] +#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize)] pub enum StatusCode { Ok = 0, Err = 1, } // serde user guide: https://serde.rs/field-attrs.html -#[derive(Debug, Clone)] -// #[cfg_attr(feature = "use_serde", derive(Serialize))] +#[derive(Debug, Clone, serde::Serialize)] pub struct EventResponse { - pub data: ResponseData, - pub status: StatusCode, + pub payload: Payload, + pub status_code: StatusCode, pub error: Option, } impl EventResponse { - pub fn new(status: StatusCode) -> Self { + pub fn new(status_code: StatusCode) -> Self { EventResponse { - data: ResponseData::None, - status, + payload: Payload::None, + status_code, error: None, } } @@ -36,11 +31,11 @@ impl EventResponse { impl std::fmt::Display for EventResponse { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - f.write_fmt(format_args!("Status_Code: {:?}", self.status))?; + f.write_fmt(format_args!("Status_Code: {:?}", self.status_code))?; - match &self.data { - ResponseData::Bytes(b) => f.write_fmt(format_args!("Data: {} bytes", b.len()))?, - ResponseData::None => f.write_fmt(format_args!("Data: Empty"))?, + match &self.payload { + Payload::Bytes(b) => f.write_fmt(format_args!("Data: {} bytes", b.len()))?, + Payload::None => f.write_fmt(format_args!("Data: Empty"))?, } match &self.error { Some(e) => f.write_fmt(format_args!("Error: {:?}", e))?, @@ -56,28 +51,6 @@ impl Responder for EventResponse { fn respond_to(self, _: &EventRequest) -> EventResponse { self } } -#[cfg(feature = "use_serde")] -fn serialize_error(error: &Option, serializer: S) -> Result -where - S: Serializer, -{ - match error { - Some(e) => serializer.serialize_str(&format!("{:?}", e)), - None => serializer.serialize_str(""), - } -} - -#[cfg(feature = "use_serde")] -fn serialize_data(data: &ResponseData, serializer: S) -> Result -where - S: Serializer, -{ - match data { - ResponseData::Bytes(bytes) => serializer.serialize_str(&format!("{} bytes", bytes.len())), - ResponseData::None => serializer.serialize_str(""), - } -} - pub fn response_ok(data: T) -> Result, E> where E: Into, diff --git a/rust-lib/flowy-sys/tests/api/module.rs b/rust-lib/flowy-sys/tests/api/module.rs index b5d7ecf066..015ac65b38 100644 --- a/rust-lib/flowy-sys/tests/api/module.rs +++ b/rust-lib/flowy-sys/tests/api/module.rs @@ -9,7 +9,7 @@ async fn test_init() { let event = "1"; init_dispatch(|| vec![Module::new().event(event, hello)]); - let request = DispatchRequest::new(event); + let request = DispatchRequest::new(event, Payload::None); let resp = async_send(request).await; dbg!(&resp); }