2021-08-20 22:00:03 +08:00
|
|
|
use bytes::Bytes;
|
2021-07-05 23:17:12 +08:00
|
|
|
use flowy_derive::ProtoBuf;
|
2021-07-08 21:23:44 +08:00
|
|
|
use flowy_dispatch::prelude::ModuleRequest;
|
2021-07-05 23:17:12 +08:00
|
|
|
use std::convert::TryFrom;
|
|
|
|
|
|
|
|
#[derive(Default, ProtoBuf)]
|
|
|
|
pub struct FFIRequest {
|
|
|
|
#[pb(index = 1)]
|
2021-07-07 22:24:26 +08:00
|
|
|
pub(crate) event: String,
|
2021-07-05 23:17:12 +08:00
|
|
|
|
|
|
|
#[pb(index = 2)]
|
2021-07-07 22:24:26 +08:00
|
|
|
pub(crate) payload: Vec<u8>,
|
2021-07-05 23:17:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FFIRequest {
|
|
|
|
pub fn from_u8_pointer(pointer: *const u8, len: usize) -> Self {
|
2021-08-20 22:00:03 +08:00
|
|
|
let buffer = unsafe { std::slice::from_raw_parts(pointer, len) }.to_vec();
|
|
|
|
let bytes = Bytes::from(buffer);
|
|
|
|
|
2021-07-05 23:17:12 +08:00
|
|
|
let request: FFIRequest = FFIRequest::try_from(&bytes).unwrap();
|
|
|
|
request
|
|
|
|
}
|
|
|
|
}
|
2021-07-07 22:24:26 +08:00
|
|
|
|
2021-07-08 13:47:11 +08:00
|
|
|
impl std::convert::Into<ModuleRequest> for FFIRequest {
|
|
|
|
fn into(self) -> ModuleRequest { ModuleRequest::new(self.event).payload(self.payload) }
|
2021-07-07 22:24:26 +08:00
|
|
|
}
|