mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
feat: run rustfmt with custom defined fmt configuration (#1848)
* chore: update rustfmt * chore: apply rustfmt format
This commit is contained in:
@ -1,3 +1,3 @@
|
||||
fn main() {
|
||||
flowy_codegen::protobuf_file::gen(env!("CARGO_PKG_NAME"));
|
||||
flowy_codegen::protobuf_file::gen(env!("CARGO_PKG_NAME"));
|
||||
}
|
||||
|
@ -2,25 +2,25 @@ use byteorder::{BigEndian, ByteOrder};
|
||||
use std::mem::forget;
|
||||
|
||||
pub fn forget_rust(buf: Vec<u8>) -> *const u8 {
|
||||
let ptr = buf.as_ptr();
|
||||
forget(buf);
|
||||
ptr
|
||||
let ptr = buf.as_ptr();
|
||||
forget(buf);
|
||||
ptr
|
||||
}
|
||||
|
||||
#[allow(unused_attributes)]
|
||||
#[allow(dead_code)]
|
||||
pub fn reclaim_rust(ptr: *mut u8, length: u32) {
|
||||
unsafe {
|
||||
let len: usize = length as usize;
|
||||
Vec::from_raw_parts(ptr, len, len);
|
||||
}
|
||||
unsafe {
|
||||
let len: usize = length as usize;
|
||||
Vec::from_raw_parts(ptr, len, len);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn extend_front_four_bytes_into_bytes(bytes: &[u8]) -> Vec<u8> {
|
||||
let mut output = Vec::with_capacity(bytes.len() + 4);
|
||||
let mut marker_bytes = [0; 4];
|
||||
BigEndian::write_u32(&mut marker_bytes, bytes.len() as u32);
|
||||
output.extend_from_slice(&marker_bytes);
|
||||
output.extend_from_slice(bytes);
|
||||
output
|
||||
let mut output = Vec::with_capacity(bytes.len() + 4);
|
||||
let mut marker_bytes = [0; 4];
|
||||
BigEndian::write_u32(&mut marker_bytes, bytes.len() as u32);
|
||||
output.extend_from_slice(&marker_bytes);
|
||||
output.extend_from_slice(bytes);
|
||||
output
|
||||
}
|
||||
|
@ -7,8 +7,8 @@ mod util;
|
||||
|
||||
use crate::notification::DartNotificationSender;
|
||||
use crate::{
|
||||
c::{extend_front_four_bytes_into_bytes, forget_rust},
|
||||
model::{FFIRequest, FFIResponse},
|
||||
c::{extend_front_four_bytes_into_bytes, forget_rust},
|
||||
model::{FFIRequest, FFIResponse},
|
||||
};
|
||||
use flowy_core::get_client_server_configuration;
|
||||
use flowy_core::*;
|
||||
@ -20,69 +20,74 @@ use parking_lot::RwLock;
|
||||
use std::{ffi::CStr, os::raw::c_char};
|
||||
|
||||
lazy_static! {
|
||||
static ref APPFLOWY_CORE: RwLock<Option<AppFlowyCore>> = RwLock::new(None);
|
||||
static ref APPFLOWY_CORE: RwLock<Option<AppFlowyCore>> = RwLock::new(None);
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn init_sdk(path: *mut c_char) -> i64 {
|
||||
let c_str: &CStr = unsafe { CStr::from_ptr(path) };
|
||||
let path: &str = c_str.to_str().unwrap();
|
||||
let c_str: &CStr = unsafe { CStr::from_ptr(path) };
|
||||
let path: &str = c_str.to_str().unwrap();
|
||||
|
||||
let server_config = get_client_server_configuration().unwrap();
|
||||
let log_crates = vec!["flowy-ffi".to_string()];
|
||||
let config = AppFlowyCoreConfig::new(path, "appflowy".to_string(), server_config).log_filter("info", log_crates);
|
||||
*APPFLOWY_CORE.write() = Some(AppFlowyCore::new(config));
|
||||
let server_config = get_client_server_configuration().unwrap();
|
||||
let log_crates = vec!["flowy-ffi".to_string()];
|
||||
let config = AppFlowyCoreConfig::new(path, "appflowy".to_string(), server_config)
|
||||
.log_filter("info", log_crates);
|
||||
*APPFLOWY_CORE.write() = Some(AppFlowyCore::new(config));
|
||||
|
||||
0
|
||||
0
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn async_event(port: i64, input: *const u8, len: usize) {
|
||||
let request: AFPluginRequest = FFIRequest::from_u8_pointer(input, len).into();
|
||||
log::trace!(
|
||||
"[FFI]: {} Async Event: {:?} with {} port",
|
||||
&request.id,
|
||||
&request.event,
|
||||
port
|
||||
);
|
||||
let request: AFPluginRequest = FFIRequest::from_u8_pointer(input, len).into();
|
||||
log::trace!(
|
||||
"[FFI]: {} Async Event: {:?} with {} port",
|
||||
&request.id,
|
||||
&request.event,
|
||||
port
|
||||
);
|
||||
|
||||
let dispatcher = match APPFLOWY_CORE.read().as_ref() {
|
||||
None => {
|
||||
log::error!("sdk not init yet.");
|
||||
return;
|
||||
}
|
||||
Some(e) => e.event_dispatcher.clone(),
|
||||
};
|
||||
let _ = AFPluginDispatcher::async_send_with_callback(dispatcher, request, move |resp: AFPluginEventResponse| {
|
||||
log::trace!("[FFI]: Post data to dart through {} port", port);
|
||||
Box::pin(post_to_flutter(resp, port))
|
||||
});
|
||||
let dispatcher = match APPFLOWY_CORE.read().as_ref() {
|
||||
None => {
|
||||
log::error!("sdk not init yet.");
|
||||
return;
|
||||
},
|
||||
Some(e) => e.event_dispatcher.clone(),
|
||||
};
|
||||
let _ = AFPluginDispatcher::async_send_with_callback(
|
||||
dispatcher,
|
||||
request,
|
||||
move |resp: AFPluginEventResponse| {
|
||||
log::trace!("[FFI]: Post data to dart through {} port", port);
|
||||
Box::pin(post_to_flutter(resp, port))
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn sync_event(input: *const u8, len: usize) -> *const u8 {
|
||||
let request: AFPluginRequest = FFIRequest::from_u8_pointer(input, len).into();
|
||||
log::trace!("[FFI]: {} Sync Event: {:?}", &request.id, &request.event,);
|
||||
let request: AFPluginRequest = FFIRequest::from_u8_pointer(input, len).into();
|
||||
log::trace!("[FFI]: {} Sync Event: {:?}", &request.id, &request.event,);
|
||||
|
||||
let dispatcher = match APPFLOWY_CORE.read().as_ref() {
|
||||
None => {
|
||||
log::error!("sdk not init yet.");
|
||||
return forget_rust(Vec::default());
|
||||
}
|
||||
Some(e) => e.event_dispatcher.clone(),
|
||||
};
|
||||
let _response = AFPluginDispatcher::sync_send(dispatcher, request);
|
||||
let dispatcher = match APPFLOWY_CORE.read().as_ref() {
|
||||
None => {
|
||||
log::error!("sdk not init yet.");
|
||||
return forget_rust(Vec::default());
|
||||
},
|
||||
Some(e) => e.event_dispatcher.clone(),
|
||||
};
|
||||
let _response = AFPluginDispatcher::sync_send(dispatcher, request);
|
||||
|
||||
// FFIResponse { }
|
||||
let response_bytes = vec![];
|
||||
let result = extend_front_four_bytes_into_bytes(&response_bytes);
|
||||
forget_rust(result)
|
||||
// FFIResponse { }
|
||||
let response_bytes = vec![];
|
||||
let result = extend_front_four_bytes_into_bytes(&response_bytes);
|
||||
forget_rust(result)
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn set_stream_port(port: i64) -> i32 {
|
||||
register_notification_sender(DartNotificationSender::new(port));
|
||||
0
|
||||
register_notification_sender(DartNotificationSender::new(port));
|
||||
0
|
||||
}
|
||||
|
||||
#[inline(never)]
|
||||
@ -91,39 +96,39 @@ pub extern "C" fn link_me_please() {}
|
||||
|
||||
#[inline(always)]
|
||||
async fn post_to_flutter(response: AFPluginEventResponse, port: i64) {
|
||||
let isolate = allo_isolate::Isolate::new(port);
|
||||
match isolate
|
||||
.catch_unwind(async {
|
||||
let ffi_resp = FFIResponse::from(response);
|
||||
ffi_resp.into_bytes().unwrap().to_vec()
|
||||
})
|
||||
.await
|
||||
{
|
||||
Ok(_success) => {
|
||||
log::trace!("[FFI]: Post data to dart success");
|
||||
}
|
||||
Err(e) => {
|
||||
if let Some(msg) = e.downcast_ref::<&str>() {
|
||||
log::error!("[FFI]: {:?}", msg);
|
||||
} else {
|
||||
log::error!("[FFI]: allo_isolate post panic");
|
||||
}
|
||||
}
|
||||
}
|
||||
let isolate = allo_isolate::Isolate::new(port);
|
||||
match isolate
|
||||
.catch_unwind(async {
|
||||
let ffi_resp = FFIResponse::from(response);
|
||||
ffi_resp.into_bytes().unwrap().to_vec()
|
||||
})
|
||||
.await
|
||||
{
|
||||
Ok(_success) => {
|
||||
log::trace!("[FFI]: Post data to dart success");
|
||||
},
|
||||
Err(e) => {
|
||||
if let Some(msg) = e.downcast_ref::<&str>() {
|
||||
log::error!("[FFI]: {:?}", msg);
|
||||
} else {
|
||||
log::error!("[FFI]: allo_isolate post panic");
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn backend_log(level: i64, data: *const c_char) {
|
||||
let c_str = unsafe { CStr::from_ptr(data) };
|
||||
let log_str = c_str.to_str().unwrap();
|
||||
let c_str = unsafe { CStr::from_ptr(data) };
|
||||
let log_str = c_str.to_str().unwrap();
|
||||
|
||||
// Don't change the mapping relation between number and level
|
||||
match level {
|
||||
0 => tracing::info!("{}", log_str),
|
||||
1 => tracing::debug!("{}", log_str),
|
||||
2 => tracing::trace!("{}", log_str),
|
||||
3 => tracing::warn!("{}", log_str),
|
||||
4 => tracing::error!("{}", log_str),
|
||||
_ => (),
|
||||
}
|
||||
// Don't change the mapping relation between number and level
|
||||
match level {
|
||||
0 => tracing::info!("{}", log_str),
|
||||
1 => tracing::debug!("{}", log_str),
|
||||
2 => tracing::trace!("{}", log_str),
|
||||
3 => tracing::warn!("{}", log_str),
|
||||
4 => tracing::error!("{}", log_str),
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
|
@ -5,24 +5,24 @@ use std::convert::TryFrom;
|
||||
|
||||
#[derive(Default, ProtoBuf)]
|
||||
pub struct FFIRequest {
|
||||
#[pb(index = 1)]
|
||||
pub(crate) event: String,
|
||||
#[pb(index = 1)]
|
||||
pub(crate) event: String,
|
||||
|
||||
#[pb(index = 2)]
|
||||
pub(crate) payload: Vec<u8>,
|
||||
#[pb(index = 2)]
|
||||
pub(crate) payload: Vec<u8>,
|
||||
}
|
||||
|
||||
impl FFIRequest {
|
||||
pub fn from_u8_pointer(pointer: *const u8, len: usize) -> Self {
|
||||
let buffer = unsafe { std::slice::from_raw_parts(pointer, len) }.to_vec();
|
||||
let bytes = Bytes::from(buffer);
|
||||
let request: FFIRequest = FFIRequest::try_from(bytes).unwrap();
|
||||
request
|
||||
}
|
||||
pub fn from_u8_pointer(pointer: *const u8, len: usize) -> Self {
|
||||
let buffer = unsafe { std::slice::from_raw_parts(pointer, len) }.to_vec();
|
||||
let bytes = Bytes::from(buffer);
|
||||
let request: FFIRequest = FFIRequest::try_from(bytes).unwrap();
|
||||
request
|
||||
}
|
||||
}
|
||||
|
||||
impl std::convert::From<FFIRequest> for AFPluginRequest {
|
||||
fn from(ffi_request: FFIRequest) -> Self {
|
||||
AFPluginRequest::new(ffi_request.event).payload(ffi_request.payload)
|
||||
}
|
||||
fn from(ffi_request: FFIRequest) -> Self {
|
||||
AFPluginRequest::new(ffi_request.event).payload(ffi_request.payload)
|
||||
}
|
||||
}
|
||||
|
@ -3,43 +3,43 @@ use lib_dispatch::prelude::{AFPluginEventResponse, Payload, StatusCode};
|
||||
|
||||
#[derive(ProtoBuf_Enum, Clone, Copy)]
|
||||
pub enum FFIStatusCode {
|
||||
Ok = 0,
|
||||
Err = 1,
|
||||
Internal = 2,
|
||||
Ok = 0,
|
||||
Err = 1,
|
||||
Internal = 2,
|
||||
}
|
||||
|
||||
impl std::default::Default for FFIStatusCode {
|
||||
fn default() -> FFIStatusCode {
|
||||
FFIStatusCode::Ok
|
||||
}
|
||||
fn default() -> FFIStatusCode {
|
||||
FFIStatusCode::Ok
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(ProtoBuf, Default)]
|
||||
pub struct FFIResponse {
|
||||
#[pb(index = 1)]
|
||||
payload: Vec<u8>,
|
||||
#[pb(index = 1)]
|
||||
payload: Vec<u8>,
|
||||
|
||||
#[pb(index = 2)]
|
||||
code: FFIStatusCode,
|
||||
#[pb(index = 2)]
|
||||
code: FFIStatusCode,
|
||||
}
|
||||
|
||||
impl std::convert::From<AFPluginEventResponse> for FFIResponse {
|
||||
fn from(resp: AFPluginEventResponse) -> Self {
|
||||
let payload = match resp.payload {
|
||||
Payload::Bytes(bytes) => bytes.to_vec(),
|
||||
Payload::None => vec![],
|
||||
};
|
||||
fn from(resp: AFPluginEventResponse) -> Self {
|
||||
let payload = match resp.payload {
|
||||
Payload::Bytes(bytes) => bytes.to_vec(),
|
||||
Payload::None => vec![],
|
||||
};
|
||||
|
||||
let code = match resp.status_code {
|
||||
StatusCode::Ok => FFIStatusCode::Ok,
|
||||
StatusCode::Err => FFIStatusCode::Err,
|
||||
};
|
||||
let code = match resp.status_code {
|
||||
StatusCode::Ok => FFIStatusCode::Ok,
|
||||
StatusCode::Err => FFIStatusCode::Err,
|
||||
};
|
||||
|
||||
// let msg = match resp.error {
|
||||
// None => "".to_owned(),
|
||||
// Some(e) => format!("{:?}", e),
|
||||
// };
|
||||
// let msg = match resp.error {
|
||||
// None => "".to_owned(),
|
||||
// Some(e) => format!("{:?}", e),
|
||||
// };
|
||||
|
||||
FFIResponse { payload, code }
|
||||
}
|
||||
FFIResponse { payload, code }
|
||||
}
|
||||
}
|
||||
|
@ -5,21 +5,21 @@ use flowy_notification::NotificationSender;
|
||||
use std::convert::TryInto;
|
||||
|
||||
pub struct DartNotificationSender {
|
||||
isolate: Isolate,
|
||||
isolate: Isolate,
|
||||
}
|
||||
|
||||
impl DartNotificationSender {
|
||||
pub fn new(port: i64) -> Self {
|
||||
Self {
|
||||
isolate: Isolate::new(port),
|
||||
}
|
||||
pub fn new(port: i64) -> Self {
|
||||
Self {
|
||||
isolate: Isolate::new(port),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl NotificationSender for DartNotificationSender {
|
||||
fn send_subject(&self, subject: SubscribeObject) -> Result<(), String> {
|
||||
let bytes: Bytes = subject.try_into().unwrap();
|
||||
self.isolate.post(bytes.to_vec());
|
||||
Ok(())
|
||||
}
|
||||
fn send_subject(&self, subject: SubscribeObject) -> Result<(), String> {
|
||||
let bytes: Bytes = subject.try_into().unwrap();
|
||||
self.isolate.post(bytes.to_vec());
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user