pub mod config;
pub mod custom;
pub mod handler;
pub mod plugin;

use anyhow::{Context, Result};
use folk_api::{Plugin, PluginFactory, ServerPluginWrapper};
use serde_json::Value;

pub use config::MetricsConfig;
pub use plugin::MetricsPlugin;

struct MetricsPluginFactory;

impl PluginFactory for MetricsPluginFactory {
    fn create(&self, config: Value) -> Result<Box<dyn Plugin>> {
        let config: MetricsConfig =
            serde_json::from_value(config).context("invalid [metrics] configuration")?;
        Ok(Box::new(ServerPluginWrapper::new(MetricsPlugin::new(
            config,
        ))))
    }
}

pub fn folk_plugin_factory() -> Box<dyn PluginFactory> {
    Box::new(MetricsPluginFactory)
}
