#[derive(clap::Args, Clone, Debug)]
pub struct GenerateConfigSchema {
#[clap(short, long, default_value = ".")]
pub output_directory: std::path::PathBuf,
#[clap(num_args = 1.., default_value = "all")]
pub filter_ids: Vec<String>,
}
impl GenerateConfigSchema {
pub fn generate_config_schema(&self) -> crate::Result<()> {
let set = crate::filters::FilterSet::default();
type SchemaIterator<'r> = Box<dyn Iterator<Item = (&'static str, schemars::Schema)> + 'r>;
let schemas = if self.filter_ids.len() == 1 && self.filter_ids[0].to_lowercase() == "all" {
{
Box::new(
set.iter()
.map(|factory| (factory.name(), factory.config_schema())),
) as SchemaIterator<'_>
}
} else {
{
Box::new(self.filter_ids.iter().filter_map(|id| {
let item = set.get(id);
if item.is_none() {
tracing::error!("{id} not found in filter set.");
}
item.map(|item| (item.name(), item.config_schema()))
})) as SchemaIterator<'_>
}
};
for (id, schema) in schemas {
let mut path = self.output_directory.join(id);
path.set_extension("yaml");
tracing::info!("Writing {id} schema to {}", path.display());
std::fs::write(path, serde_yaml::to_string(&schema)?)?;
}
Ok(())
}
}