#![allow(non_local_definitions)]
use abi_stable::{
StableAbi, declare_root_module_statics,
library::RootModule,
package_version_strings, sabi_trait,
sabi_types::VersionStrings,
std_types::{RBox, RResult, RSliceMut, RStr, RString, RVec},
};
pub const ABI_VERSION: u32 = 1;
pub const API_VERSION: &str = env!("CARGO_PKG_VERSION");
pub const MAX_REGION_PIXELS: u64 = 8192 * 8192;
pub const MAX_RASTER_PIXELS: u64 = 4096 * 4096;
pub const MAX_SLIDE_READ_PIXELS: u64 = 4096 * 4096;
pub const SLIDE_SOURCE_FIELD: usize = 3;
#[repr(u32)]
#[derive(StableAbi, Debug, Clone, Copy, PartialEq, Eq)]
pub enum FfiErrorKind {
Failed = 0,
Panicked = 1,
Unsupported = 2,
InvalidArguments = 3,
Revoked = 4,
}
#[repr(C)]
#[derive(StableAbi, Debug, Clone, PartialEq, Eq)]
pub struct FfiError {
pub kind: u32,
pub message: RString,
}
impl FfiError {
pub fn new(kind: FfiErrorKind, message: impl Into<String>) -> Self {
Self {
kind: kind as u32,
message: RString::from(message.into()),
}
}
pub fn failed(message: impl Into<String>) -> Self {
Self::new(FfiErrorKind::Failed, message)
}
pub fn kind(&self) -> FfiErrorKind {
match self.kind {
1 => FfiErrorKind::Panicked,
2 => FfiErrorKind::Unsupported,
3 => FfiErrorKind::InvalidArguments,
4 => FfiErrorKind::Revoked,
_ => FfiErrorKind::Failed,
}
}
}
impl std::fmt::Display for FfiError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.message.as_str())
}
}
impl std::error::Error for FfiError {}
pub type FfiResult<T> = RResult<T, FfiError>;
#[repr(C)]
#[derive(StableAbi, Debug, Clone, Copy, PartialEq)]
pub struct RegionRequest {
pub file_id: i64,
pub level: u32,
pub x: i64,
pub y: i64,
pub width: u32,
pub height: u32,
pub background: u32,
}
#[repr(C)]
#[derive(StableAbi, Debug, Clone, PartialEq, Eq)]
pub struct Pixels {
pub width: u32,
pub height: u32,
pub data: RVec<u8>,
}
impl Pixels {
pub fn is_well_formed(&self) -> bool {
(self.width as usize)
.checked_mul(self.height as usize)
.and_then(|n| n.checked_mul(4))
== Some(self.data.len())
}
}
#[repr(C)]
#[derive(StableAbi, Debug, Clone, PartialEq)]
pub struct RasterUpload {
pub layer_id: RString,
pub object_id: RString,
pub x: f64,
pub y: f64,
pub width: f64,
pub height: f64,
pub pixels: Pixels,
pub smooth: bool,
}
#[repr(C)]
#[derive(StableAbi, Debug, Clone, Copy, PartialEq, Default)]
pub struct FilterView {
pub pane: u32,
pub file_id: i64,
pub center_x: f64,
pub center_y: f64,
pub zoom: f64,
pub width: f64,
pub height: f64,
pub scale_factor: f64,
pub visible_x: f64,
pub visible_y: f64,
pub visible_width: f64,
pub visible_height: f64,
pub frame: u64,
}
#[repr(C)]
#[derive(StableAbi)]
pub struct FilterFrame<'a> {
pub pixels: RSliceMut<'a, u8>,
pub width: u32,
pub height: u32,
pub stride: u32,
pub view: FilterView,
}
#[sabi_trait]
pub trait PluginFfi: Send + Sync {
fn handle_event(&self, event_json: RStr<'_>) -> FfiResult<RString>;
fn apply_filter(&self, filter_id: RStr<'_>, frame: FilterFrame<'_>) -> FfiResult<()>;
#[sabi(last_prefix_field)]
fn shutdown(&self);
}
pub type PluginBox = PluginFfi_TO<'static, RBox<()>>;
#[sabi_trait]
pub trait HostFfi: Send + Sync {
fn call(&self, method: RStr<'_>, args_json: RStr<'_>) -> FfiResult<RString>;
fn read_region(&self, request: RegionRequest) -> FfiResult<Pixels>;
fn read_associated_image(&self, file_id: i64, name: RStr<'_>) -> FfiResult<Pixels>;
#[sabi(last_prefix_field)]
fn put_raster(&self, raster: RasterUpload) -> FfiResult<()>;
}
pub type HostBox = HostFfi_TO<'static, RBox<()>>;
#[repr(C)]
#[derive(StableAbi, Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct SlideRegion {
pub level: u32,
pub x: u64,
pub y: u64,
pub width: u32,
pub height: u32,
}
#[sabi_trait]
pub trait SlideSourceFfi: Send + Sync {
#[sabi(last_prefix_field)]
fn open(&self, request_json: RStr<'_>) -> FfiResult<SlideBox>;
}
pub type SlideSourceBox = SlideSourceFfi_TO<'static, RBox<()>>;
#[sabi_trait]
pub trait SlideFfi: Send + Sync {
fn info(&self) -> FfiResult<RString>;
fn read_region(&self, region: SlideRegion, out: RSliceMut<'_, u8>) -> FfiResult<()>;
fn read_associated_image(&self, name: RStr<'_>) -> FfiResult<Pixels>;
#[sabi(last_prefix_field)]
fn call(&self, method: RStr<'_>, args_json: RStr<'_>) -> FfiResult<RString>;
}
pub type SlideBox = SlideFfi_TO<'static, RBox<()>>;
#[repr(C)]
#[derive(StableAbi)]
#[sabi(kind(Prefix(prefix_ref = PluginModuleRef)))]
#[sabi(missing_field(panic))]
pub struct PluginModule {
pub abi_version: u32,
pub api_version: RStr<'static>,
#[sabi(last_prefix_field)]
pub create: extern "C" fn(host: HostBox, init_json: RStr<'_>) -> FfiResult<PluginBox>,
#[sabi(missing_field(option))]
pub slide_source: extern "C" fn(instance: &PluginBox) -> FfiResult<SlideSourceBox>,
}
impl RootModule for PluginModuleRef {
declare_root_module_statics! {PluginModuleRef}
const BASE_NAME: &'static str = "eov_plugin";
const NAME: &'static str = "eov plugin";
const VERSION_STRINGS: VersionStrings = package_version_strings!();
}
pub mod v1_0 {
use super::{FfiResult, HostBox, PluginBox};
use abi_stable::{
StableAbi, declare_root_module_statics, library::RootModule, package_version_strings,
sabi_types::VersionStrings, std_types::RStr,
};
#[repr(C)]
#[derive(StableAbi)]
#[sabi(kind(Prefix(prefix_ref = PluginModuleRef)))]
#[sabi(missing_field(panic))]
pub struct PluginModule {
pub abi_version: u32,
pub api_version: RStr<'static>,
#[sabi(last_prefix_field)]
pub create: extern "C" fn(host: HostBox, init_json: RStr<'_>) -> FfiResult<PluginBox>,
}
impl RootModule for PluginModuleRef {
declare_root_module_statics! {PluginModuleRef}
const BASE_NAME: &'static str = "eov_plugin";
const NAME: &'static str = "eov plugin";
const VERSION_STRINGS: VersionStrings = package_version_strings!();
}
}
pub fn plugin_library_filename(id: &str) -> String {
if cfg!(target_os = "macos") {
format!("lib{id}.dylib")
} else if cfg!(target_os = "windows") {
format!("{id}.dll")
} else {
format!("lib{id}.so")
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn export_macro_uses_abi_stables_loader_symbol() {
assert_eq!(
abi_stable::library::ROOT_MODULE_LOADER_NAME,
crate::sdk::ROOT_MODULE_LOADER_SYMBOL
);
}
#[test]
fn unknown_error_kinds_degrade_to_failed() {
let error = FfiError {
kind: 99,
message: "x".into(),
};
assert_eq!(error.kind(), FfiErrorKind::Failed);
assert_eq!(
FfiError::new(FfiErrorKind::Unsupported, "y").kind(),
FfiErrorKind::Unsupported
);
}
extern "C" fn refuse(_host: HostBox, _init_json: RStr<'_>) -> FfiResult<PluginBox> {
abi_stable::std_types::RErr(FfiError::failed("test module"))
}
extern "C" fn no_source(_instance: &PluginBox) -> FfiResult<SlideSourceBox> {
abi_stable::std_types::RErr(FfiError::failed("test module"))
}
fn module_1_0() -> v1_0::PluginModuleRef {
use abi_stable::prefix_type::PrefixTypeTrait;
v1_0::PluginModule {
abi_version: ABI_VERSION,
api_version: RStr::from_str("1.0.0"),
create: refuse,
}
.leak_into_prefix()
}
fn module_1_1() -> PluginModuleRef {
use abi_stable::prefix_type::PrefixTypeTrait;
PluginModule {
abi_version: ABI_VERSION,
api_version: RStr::from_str(API_VERSION),
create: refuse,
slide_source: no_source,
}
.leak_into_prefix()
}
fn has_slide_source<M: abi_stable::prefix_type::PrefixRefTrait>(module: M) -> bool {
module
.to_prefix_ref()
.field_accessibility()
.at(SLIDE_SOURCE_FIELD)
.is_accessible()
}
#[test]
fn hosts_check_plugins_against_the_frozen_1_0_root_module() {
use abi_stable::library::LibHeader;
let old = LibHeader::from_module(module_1_0());
let error = old
.init_root_module::<PluginModuleRef>()
.err()
.expect("a 1.0 module fails the 1.1 layout")
.to_string();
assert!(error.contains("too many fields"), "{error}");
let loaded = old.init_root_module::<v1_0::PluginModuleRef>().unwrap();
assert_eq!(loaded.api_version().as_str(), "1.0.0");
assert!(!has_slide_source(loaded));
let new = LibHeader::from_module(module_1_1());
let base = new.init_root_module::<v1_0::PluginModuleRef>().unwrap();
assert!(has_slide_source(base));
let full = new.init_root_module::<PluginModuleRef>().unwrap();
assert!(full.slide_source().is_some());
}
#[test]
fn pixels_well_formedness() {
let ok = Pixels {
width: 2,
height: 3,
data: vec![0; 24].into(),
};
assert!(ok.is_well_formed());
let bad = Pixels {
width: 2,
height: 3,
data: vec![0; 23].into(),
};
assert!(!bad.is_well_formed());
}
}