radar-g/radarg_plugin_interface/src/lib.rs
2024-03-05 20:39:32 +08:00

169 lines
4.1 KiB
Rust

use abi_stable::{
declare_root_module_statics,
library::RootModule,
package_version_strings, sabi_trait,
sabi_types::{RMut, VersionStrings},
std_types::{RBox, RCowStr, ROk, ROption, RResult, RSome, RStr, RString, RVec},
StableAbi,
};
mod error;
pub use self::error::Error;
/// The identifier for a plugin.
#[repr(C)]
#[derive(Debug, Clone, PartialEq, Eq, StableAbi, Hash)]
#[sabi(impl_InterfaceType(Sync, Send, Debug, Debug, Hash))]
pub struct PluginId {
pub named: RCowStr<'static>,
/// The number of the instance of this Plugin.
pub instance: u64,
}
#[repr(C)]
#[derive(StableAbi, Clone, Debug, Copy)]
#[sabi(impl_InterfaceType(Sync, Send, Debug, Debug))]
pub enum CoordType {
Polar,
Cartesian,
Other,
}
pub type PluginType = Plugin_TO<'static, RBox<()>>;
#[repr(C)]
#[derive(StableAbi, Clone, Debug)]
#[sabi(impl_InterfaceType(Sync, Send, Debug))]
pub enum VecResult {
I64(RVec<i64>),
F64(RVec<f64>),
I32(RVec<i32>),
I16(RVec<i16>),
F32(RVec<f32>),
U64(RVec<u64>),
U32(RVec<u32>),
Bool(RVec<bool>),
I8(RVec<i8>),
U8(RVec<u8>),
}
#[repr(C)]
#[derive(StableAbi, Clone, Copy, Debug)]
#[sabi(impl_InterfaceType(Sync, Send, Debug))]
pub enum DataShape {
Scalar,
Vector,
Matrix,
}
#[repr(C)]
#[derive(Debug, StableAbi, Clone)]
pub struct MetaData {
pub datetime: ROption<i64>,
pub site_info: ROption<RString>,
pub lat_range: ROption<[f64; 2]>,
pub lon_range: ROption<[f64; 2]>,
pub data_format: ROption<RString>,
pub other_info: ROption<RString>,
}
#[repr(C)]
#[derive(StableAbi, Clone, Debug)]
#[sabi(impl_InterfaceType(Sync, Send, Debug))]
pub enum PluginResultType {
// Single
R,
V,
SW,
CC,
ZDR,
PHIDP,
KDP,
HCA,
DBZ,
QPE,
QPF,
VIL,
OHP,
THP,
ET,
EB,
// Unknown
Unknown,
}
#[repr(C)]
#[derive(StableAbi, Clone, Debug)]
#[sabi(impl_InterfaceType(Sync, Send, Debug))]
pub struct PluginResult {
pub datetime: RString,
pub meta: MetaData,
pub blocks: RVec<Block>,
}
#[repr(C)]
#[derive(StableAbi, Clone, Debug)]
#[sabi(impl_InterfaceType(Sync, Send, Debug))]
pub struct Block {
pub data: VecResult,
pub shape: DataShape,
pub size: RVec<usize>,
pub coord_type: CoordType,
pub datetime: i64,
pub dimensions: RVec<RString>,
pub dimension_values: RVec<RVec<f64>>,
pub fill_value: f64,
pub data_type: PluginResultType,
}
#[repr(C)]
#[derive(Debug, StableAbi)]
#[sabi(impl_InterfaceType(Sync, Send, Debug))]
pub struct PluginInfo {
pub name: RStr<'static>,
pub version: RStr<'static>,
pub author: RStr<'static>,
pub description: ROption<RStr<'static>>,
pub url: ROption<RStr<'static>>,
}
#[sabi_trait]
#[sabi(impl_InterfaceType(Sync, Send, Debug))]
pub trait Plugin: Send + Sync {
fn load(&self, path: RStr<'_>) -> RResult<PluginResult, Error>;
fn plugin_id(&self) -> &PluginId;
fn plugin_info(&self) -> PluginInfo;
}
/// The root module of a`plugin` dynamic library.
///
/// To load this module,
/// call <PluginMod as RootModule>::load_from_directory(some_directory_path)
#[repr(C)]
#[derive(StableAbi)]
#[sabi(impl_InterfaceType(Sync, Send, Debug))]
#[sabi(kind(Prefix(prefix_ref = PluginMod_Ref)))]
#[sabi(missing_field(panic))]
pub struct PluginMod {
/// Constructs the plugin.
///
///
/// The `#[sabi(last_prefix_field)]` attribute here means that this is the last field in this struct
/// that was defined in the first compatible version of the library
/// (0.1.0, 0.2.0, 0.3.0, 1.0.0, 2.0.0 ,etc),
/// requiring new fields to always be added below preexisting ones.
///
/// The `#[sabi(last_prefix_field)]` attribute would stay on this field until the library
/// bumps its "major" version,
/// at which point it would be moved to the last field at the time.
///
#[sabi(last_prefix_field)]
pub new: extern "C" fn(PluginId) -> RResult<PluginType, Error>,
}
impl RootModule for PluginMod_Ref {
declare_root_module_statics! {PluginMod_Ref}
const BASE_NAME: &'static str = "plugin";
const NAME: &'static str = "plugin";
const VERSION_STRINGS: VersionStrings = package_version_strings!();
}