#![warn(missing_docs)]
pub mod abi;
mod export;
mod host;
pub use abi::{
ClassDesc, FfiReturn, FfiStatus, FfiStr, FunctionDesc, GENERIC_PLUGIN_ABI_VERSION,
GenericValue, HostApi, MethodDesc, ModuleDesc, PluginFn, PluginMethodFn, PluginTraverseFn,
PluginValueFn, PluginVisitFn, ValueDesc,
};
pub use host::{
__invoke_plugin_fn, __invoke_plugin_method_fn, __invoke_plugin_value_fn, ArgValue, Host,
Rooted, RustPluginFn, RustPluginMethodFn, RustPluginValueFn,
};
#[repr(u32)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ValueKind {
Nil = 0,
Bool = 1,
Int = 2,
BigInt = 3,
Float = 4,
Rational = 5,
String = 6,
List = 7,
Tuple = 8,
Dict = 9,
Set = 10,
Range = 11,
StopIteration = 12,
Instance = 13,
Class = 14,
Function = 15,
Module = 16,
Exception = 17,
Generator = 18,
Iterator = 19,
Other = 20,
}
impl ValueKind {
#[must_use]
pub const fn from_u32(code: u32) -> Self {
match code {
0 => Self::Nil,
1 => Self::Bool,
2 => Self::Int,
3 => Self::BigInt,
4 => Self::Float,
5 => Self::Rational,
6 => Self::String,
7 => Self::List,
8 => Self::Tuple,
9 => Self::Dict,
10 => Self::Set,
11 => Self::Range,
12 => Self::StopIteration,
13 => Self::Instance,
14 => Self::Class,
15 => Self::Function,
16 => Self::Module,
17 => Self::Exception,
18 => Self::Generator,
19 => Self::Iterator,
_ => Self::Other,
}
}
}
#[derive(Debug, Clone, Copy)]
pub enum PluginError {
Exception(GenericValue),
Fatal,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn abi_type_layout() {
assert_eq!(size_of::<GenericValue>(), 32);
}
#[test]
fn crate_version_encodes_the_abi_version() {
let mut parts = env!("CARGO_PKG_VERSION").split('.');
let major: u32 = parts.next().unwrap().parse().unwrap();
let minor: u32 = parts.next().unwrap().parse().unwrap();
assert_eq!(major, 0, "the version scheme is 0.<abi>.<patch>");
assert_eq!(
minor, GENERIC_PLUGIN_ABI_VERSION,
"bump GENERIC_PLUGIN_ABI_VERSION and the crate minor version together"
);
}
#[test]
fn kind_round_trips() {
for kind in [
ValueKind::Nil,
ValueKind::Bool,
ValueKind::Int,
ValueKind::BigInt,
ValueKind::Float,
ValueKind::Rational,
ValueKind::String,
ValueKind::List,
ValueKind::Tuple,
ValueKind::Dict,
ValueKind::Set,
ValueKind::Range,
ValueKind::StopIteration,
ValueKind::Instance,
ValueKind::Class,
ValueKind::Function,
ValueKind::Module,
ValueKind::Exception,
ValueKind::Generator,
ValueKind::Iterator,
ValueKind::Other,
] {
assert_eq!(ValueKind::from_u32(kind as u32), kind);
}
assert_eq!(ValueKind::from_u32(999), ValueKind::Other);
}
}