plugin_framework, return C structure

Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
fufesou
2023-05-05 13:37:43 +08:00
parent 357028b222
commit 8f0d696304
5 changed files with 95 additions and 108 deletions

View File

@@ -6,7 +6,6 @@ use serde_json;
use std::{
collections::HashMap,
ffi::{c_char, c_void},
ptr::null,
sync::Arc,
};
@@ -63,11 +62,9 @@ macro_rules! cb_msg_field {
($field: ident) => {
let $field = match cstr_to_string($field) {
Err(e) => {
log::error!("Failed to convert {} to string, {}", stringify!($field), e);
return make_return_code_msg(
errno::ERR_CALLBACK_INVALID_ARGS,
&format!("Failed to convert {} to string, {}", stringify!($field), e),
);
let msg = format!("Failed to convert {} to string, {}", stringify!($field), e);
log::error!("{}", &msg);
return PluginReturn::new(errno::ERR_CALLBACK_INVALID_ARGS, &msg);
}
Ok(v) => v,
};
@@ -77,7 +74,7 @@ macro_rules! cb_msg_field {
macro_rules! early_return_value {
($e:expr, $code: ident, $($arg:tt)*) => {
match $e {
Err(e) => return make_return_code_msg(
Err(e) => return PluginReturn::new(
errno::$code,
&format!("Failed to {} '{}'", format_args!($($arg)*), e),
),
@@ -105,7 +102,7 @@ pub(super) extern "C" fn cb_msg(
id: *const c_char,
content: *const c_void,
len: usize,
) -> *const c_void {
) -> PluginReturn {
cb_msg_field!(peer);
cb_msg_field!(target);
cb_msg_field!(id);
@@ -122,9 +119,9 @@ pub(super) extern "C" fn cb_msg(
..Default::default()
};
session.send_plugin_request(request);
null()
PluginReturn::success()
} else {
make_return_code_msg(
PluginReturn::new(
errno::ERR_CALLBACK_PEER_NOT_FOUND,
&format!("Failed to find session for peer '{}'", peer),
)
@@ -136,7 +133,7 @@ pub(super) extern "C" fn cb_msg(
let content = std::string::String::from_utf8(content_slice[2..].to_vec())
.unwrap_or("".to_string());
push_event_to_ui(channel, &peer, &content);
null()
PluginReturn::success()
}
MSG_TO_CONFIG_TARGET => {
let s = early_return_value!(
@@ -162,7 +159,7 @@ pub(super) extern "C" fn cb_msg(
// No need to set the peer id for location config.
push_option_to_ui(ui.channel, &id, "", &msg, ui);
}
null()
PluginReturn::success()
}
config::CONFIG_TYPE_PEER => {
let _r = early_return_value!(
@@ -173,9 +170,9 @@ pub(super) extern "C" fn cb_msg(
if let Some(ui) = &msg.ui {
push_option_to_ui(ui.channel, &id, &peer, &msg, ui);
}
null()
PluginReturn::success()
}
_ => make_return_code_msg(
_ => PluginReturn::new(
errno::ERR_CALLBACK_TARGET_TYPE,
&format!("Unknown target type '{}'", &msg.r#type),
),
@@ -195,7 +192,7 @@ pub(super) extern "C" fn cb_msg(
);
super::callback_ext::ext_support_callback(&id, &peer, &msg)
}
_ => make_return_code_msg(
_ => PluginReturn::new(
errno::ERR_CALLBACK_TARGET,
&format!("Unknown target '{}'", target),
),