http_server_plugin/
lib.rs

1pub mod config;
2
3use std::path::PathBuf;
4use std::sync::Arc;
5
6use async_trait::async_trait;
7use http::request::Parts;
8use http_body_util::Full;
9use hyper::body::Bytes;
10use hyper::Response;
11use tokio::runtime::Handle;
12
13pub static CORE_VERSION: &str = env!("CARGO_PKG_VERSION");
14pub static RUSTC_VERSION: &str = env!("RUSTC_VERSION");
15
16#[async_trait]
17pub trait Function: Send + Sync {
18    async fn call(
19        &self,
20        parts: Parts,
21        body: Bytes,
22    ) -> Result<Response<Full<Bytes>>, InvocationError>;
23}
24
25#[derive(Debug)]
26pub enum InvocationError {
27    InvalidArgumentCount { expected: usize, found: usize },
28    Other { msg: String },
29}
30
31#[allow(improper_ctypes_definitions)]
32pub struct PluginDeclaration {
33    pub rustc_version: &'static str,
34    pub core_version: &'static str,
35    pub register:
36        unsafe extern "C" fn(config_path: PathBuf, rt: Arc<Handle>, &mut dyn PluginRegistrar),
37}
38
39pub trait PluginRegistrar {
40    fn register_function(&mut self, name: &str, function: Arc<dyn Function>);
41}
42
43#[macro_export]
44macro_rules! export_plugin {
45    ($register:expr) => {
46        #[doc(hidden)]
47        #[no_mangle]
48        pub static PLUGIN_DECLARATION: $crate::PluginDeclaration = $crate::PluginDeclaration {
49            rustc_version: $crate::RUSTC_VERSION,
50            core_version: $crate::CORE_VERSION,
51            register: $register,
52        };
53    };
54}