//! Rust SDK for writing Marqdo native plugins against plugin ABI v1. //! //! The SDK is the plugin-developer side of `include/marqdo_plugin_abi_v1.h`. //! It hides the raw C layout behind safe wrappers and provides the //! [`mq_plugin_entry!`] macro that exports the required //! `mq_plugin_entry_v1` symbol: //! //! ``` //! use marqdo_plugin_sdk::{mq_plugin_entry, CallContext, PluginError, Value}; //! //! fn add(_ctx: &mut CallContext, args: &[u8]) -> Result, PluginError> { //! let request = marqdo_plugin_sdk::wire::decode_request(args)?; //! let values = request.typed_args()?; //! let a = marqdo_plugin_sdk::wire::int_arg(&values, "a")?; //! let b = marqdo_plugin_sdk::wire::int_arg(&values, "b")?; //! marqdo_plugin_sdk::wire::success(Some(Value::Int((a + b).into()))) //! } //! //! mq_plugin_entry! { //! name: "demo.solver", //! version: "0.1.0", //! exports: { //! "solver.add" => "($a: Int, $b: Int) -> Int" => add, //! } //! } //! ``` //! //! The SDK never depends on host-only crates: it only uses the shared ABI //! types and the JSON wire envelope from `marqdo-plugin-abi`. Building a //! plugin does not grant it any capability; authorization is a separate host //! decision performed before loading. #![deny(missing_docs)] pub use marqdo_plugin_abi::abi::result as result_code; pub use marqdo_plugin_abi::abi::{ MqBytes, MqCallContext, MqHostApi, MqOwnedBytes, MqPluginApi, MqPluginInvoke, ABI_MAJOR, ABI_MINOR, ABI_VERSION_STRING, ENTRY_SYMBOL, }; pub use marqdo_plugin_abi::{PluginError, Value, WireRequest, WireResponse, WIRE_VERSION}; pub mod bytes; pub mod call; pub mod entry; pub mod wire; pub use bytes::{BytesRef, HostBuffer}; pub use call::CallContext; pub use wire::{ bool_arg, decode_request, error_response, int_arg, response_value, success, success_raw, text_arg, value_arg, }; /// Plugin handler signature. /// /// A handler receives the per-call context (for capability requests and /// logging) and the raw request envelope bytes, and returns the raw response /// envelope bytes on success. /// /// Use [`wire::decode_request`] and [`wire::success`] to build the response; /// handlers should never panic: panics are caught by the SDK and converted /// into a structured error, but the plugin process stays healthy only if the /// panic does not corrupt plugin state. pub type HandlerFn = fn(&mut CallContext, &[u8]) -> Result, PluginError>; /// Declare a plugin and export `mq_plugin_entry_v1`. /// /// ```ignore /// marqdo_plugin_sdk::mq_plugin_entry! { /// name: "demo.solver", /// version: "0.1.0", /// exports: { /// "solver.add" => "($a: Int, $b: Int) -> Int" => add, /// "solver.echo" => "($text: Text) -> Text" => echo, /// } /// } /// ``` /// /// Each handler is a path (or non-capturing closure) with the [`HandlerFn`] /// signature. The macro: /// - validates the host ABI table (major version and `struct_size`), /// - reports the plugin ABI version and a metadata envelope, /// - registers every export and fails the load if registration fails. #[macro_export] macro_rules! mq_plugin_entry { ( name: $name:literal, version: $version:literal, exports: { $( $export_name:literal => $signature:literal => $handler:expr ),* $(,)? } ) => { /// Marqdo plugin ABI v1 entry point (generated by the SDK macro). #[unsafe(no_mangle)] pub unsafe extern "C" fn mq_plugin_entry_v1( host: *const $crate::MqHostApi, plugin: *mut $crate::MqPluginApi, ) -> i32 { const METADATA: &[u8] = concat!( "{\"name\":\"", $name, "\",\"version\":\"", $version, "\"}" ) .as_bytes(); let metadata: &'static [u8] = METADATA; // SAFETY: the host provides the ABI tables per the header contract. let init = unsafe { $crate::entry::initialize(host, plugin, metadata) }; if init != 0 { return init; } $( { extern "C" fn invoke( ctx: *mut $crate::MqCallContext, args: $crate::MqBytes, result: *mut $crate::MqOwnedBytes, error: *mut $crate::MqOwnedBytes, ) -> i32 { // Expand the user's handler expression in a safe // context (also required by `macro_metavars_in_unsafe`). let handler: $crate::HandlerFn = $handler; // SAFETY: the host owns the invocation buffers; the SDK // validates them and never retains them. unsafe { $crate::entry::dispatch(handler, ctx, args, result, error) } } let export_name: &str = $export_name; let signature: &str = $signature; // SAFETY: host table is valid for the plugin lifetime and // was validated by `initialize`. let code = unsafe { $crate::entry::register_export(host, export_name, signature, invoke) }; if code != 0 { return code; } } )* $crate::result_code::OK } }; } #[cfg(test)] mod tests;