use anyhow::Result;
use serde::de::DeserializeOwned;
use serde::Serialize;
use crate::config::KnownFields;
use crate::processors::{Processor, ProcessorType};
#[allow(dead_code)]
pub struct ProcessorPlugin {
pub name: &'static str,
pub processor_type: ProcessorType,
pub create: fn(&toml::Value) -> Result<Box<dyn Processor>>,
pub known_fields: fn() -> &'static [&'static str],
pub output_fields: fn() -> &'static [&'static str],
pub must_fields: fn() -> &'static [&'static str],
pub field_descriptions: fn() -> &'static [(&'static str, &'static str)],
pub defconfig_json: fn(&str) -> Option<String>,
}
unsafe impl Sync for ProcessorPlugin {}
inventory::collect!(ProcessorPlugin);
pub(crate) fn all_plugins() -> impl Iterator<Item = &'static ProcessorPlugin> {
inventory::iter::<ProcessorPlugin>.into_iter()
}
pub fn apply_all_defaults(name: &str, value: &mut toml::Value) {
crate::config::apply_processor_defaults(name, value);
crate::config::apply_scan_defaults(name, value);
}
pub fn deserialize_and_create<C: Default + DeserializeOwned>(
config_toml: &toml::Value, ctor: fn(C) -> Box<dyn Processor>,
) -> Result<Box<dyn Processor>> {
let cfg: C = toml::from_str(&toml::to_string(config_toml)?)?;
Ok(ctor(cfg))
}
pub fn default_config_json<C: Default + DeserializeOwned + Serialize>(name: &str) -> Option<String> {
let mut val = toml::Value::Table(toml::map::Map::new());
apply_all_defaults(name, &mut val);
let cfg: C = toml::from_str(&toml::to_string(&val).ok()?).ok()?;
serde_json::to_string_pretty(&serde_json::to_value(cfg).ok()?).ok()
}
pub fn typed_known_fields<C: KnownFields>() -> &'static [&'static str] { C::known_fields() }
pub fn typed_output_fields<C: KnownFields>() -> &'static [&'static str] { C::output_fields() }
pub fn typed_must_fields<C: KnownFields>() -> &'static [&'static str] { C::must_fields() }
pub fn typed_field_descriptions<C: KnownFields>() -> &'static [(&'static str, &'static str)] { C::field_descriptions() }