use anyhow::Result;
use serde::de::DeserializeOwned;
use serde::Serialize;
use crate::config::KnownFields;
use crate::processors::{Processor, ProcessorType};
pub struct ProcessorPlugin {
pub name: &'static str,
pub processor_type: ProcessorType,
pub version: u32,
pub create: fn(&toml::Value) -> Result<Box<dyn Processor>>,
pub known_fields: fn() -> &'static [&'static str],
pub checksum_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>,
pub keywords: &'static [&'static str],
pub description: &'static str,
pub is_native: bool,
pub can_fix: bool,
pub supports_batch: bool,
pub max_jobs_cap: Option<usize>,
}
unsafe impl Sync for ProcessorPlugin {}
inventory::collect!(ProcessorPlugin);
pub fn all_plugins() -> impl Iterator<Item = &'static ProcessorPlugin> {
inventory::iter::<ProcessorPlugin>.into_iter()
}
pub fn find_plugin(name: &str) -> Option<&'static ProcessorPlugin> {
let type_name = name.split('.').next().unwrap_or(name);
all_plugins().find(|p| p.name == type_name)
}
pub fn description_of(name: &str) -> &'static str {
find_plugin(name).map(|p| p.description).unwrap_or("")
}
pub fn processor_type_of(name: &str) -> crate::processors::ProcessorType {
find_plugin(name)
.map(|p| p.processor_type)
.unwrap_or(crate::processors::ProcessorType::Checker)
}
pub fn is_native(name: &str) -> bool {
find_plugin(name).is_some_and(|p| p.is_native)
}
pub fn can_fix(name: &str) -> bool {
find_plugin(name).is_some_and(|p| p.can_fix)
}
pub fn processor_version(name: &str) -> Option<u32> {
all_plugins().find(|p| p.name == name).map(|p| p.version)
}
pub fn processor_name_parser() -> clap::builder::PossibleValuesParser {
let mut names: Vec<&'static str> = all_plugins().map(|p| p.name).collect();
names.sort_unstable();
clap::builder::PossibleValuesParser::new(names)
}
pub fn apply_all_defaults(
name: &str,
value: &mut toml::Value,
provenance: &mut crate::config::ProvenanceMap,
) {
crate::config::apply_processor_defaults(name, value, provenance);
crate::config::apply_scan_defaults(name, value, provenance);
}
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 deserialize_and_try_create<C: Default + DeserializeOwned>(
config_toml: &toml::Value, ctor: fn(C) -> Result<Box<dyn Processor>>,
) -> Result<Box<dyn Processor>> {
let cfg: C = toml::from_str(&toml::to_string(config_toml)?)?;
ctor(cfg)
}
pub fn default_config_json<C: Default + DeserializeOwned + Serialize + KnownFields>(name: &str) -> Option<String> {
let mut val = toml::Value::Table(toml::map::Map::new());
let mut prov = crate::config::ProvenanceMap::new();
apply_all_defaults(name, &mut val, &mut prov);
let cfg: C = toml::from_str(&toml::to_string(&val).ok()?).ok()?;
let json_val = serde_json::to_value(&cfg).ok()?;
#[cfg(debug_assertions)]
{
if let Some(obj) = json_val.as_object() {
let known: std::collections::HashSet<&str> = C::known_fields().iter().copied()
.chain(crate::config::StandardConfig::known_fields().iter().copied())
.chain(crate::config::SCAN_CONFIG_FIELDS.iter().copied())
.chain(crate::config::STANDARD_EXTRA_FIELDS.iter().copied())
.collect();
let checksum: std::collections::HashSet<&str> = C::checksum_fields().iter().copied().collect();
for key in obj.keys() {
debug_assert!(
known.contains(key.as_str()),
"Processor '{name}': default config field '{key}' is serialized but not declared in known_fields() or scan fields"
);
}
for k in &checksum {
debug_assert!(
known.contains(k),
"Processor '{name}': checksum_fields() contains '{k}' which is not in known_fields()"
);
}
}
}
serde_json::to_string_pretty(&json_val).ok()
}
pub fn typed_known_fields<C: KnownFields>() -> &'static [&'static str] { C::known_fields() }
pub fn typed_checksum_fields<C: KnownFields>() -> &'static [&'static str] { C::checksum_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() }