Skip to main content

PluginFactory

Trait PluginFactory 

Source
pub trait PluginFactory: Send + Sync {
    // Required method
    fn create(
        &self,
        config: &PluginConfig,
    ) -> Result<PluginInstance, Box<PluginError>>;
}
Expand description

Factory for creating plugin instances from config.

The host registers factories by kind name before loading config. When the manager processes a config file, it looks up the factory for each plugin’s kind and calls create().

The factory returns both the plugin and its handler because it knows the concrete types — which handler traits the plugin implements and which hooks it handles.

§Examples

struct RateLimiterFactory;

impl PluginFactory for RateLimiterFactory {
    fn create(&self, config: &PluginConfig)
        -> Result<PluginInstance, Box<PluginError>>
    {
        let plugin = Arc::new(RateLimiter::from_config(config)?);
        let handler = Arc::new(TypedHandlerAdapter::<RequestHeadersReceived, _>::new(
            Arc::clone(&plugin),
        ));
        Ok(PluginInstance { plugin, handler })
    }
}

let mut factories = PluginFactoryRegistry::new();
factories.register("security/rate_limit", Box::new(RateLimiterFactory));

Required Methods§

Source

fn create( &self, config: &PluginConfig, ) -> Result<PluginInstance, Box<PluginError>>

Create a plugin instance and its handler from config.

The config is the plugin’s entry from the YAML file.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§