use std::collections::BTreeMap;
use schemars::schema_for;
use serde_json::Value;
pub fn schemas() -> BTreeMap<String, Value> {
let mut schemas = BTreeMap::new();
macro_rules! insert {
($key:expr, $ty:ty) => {
schemas.insert(
$key.to_string(),
serde_json::to_value(schema_for!($ty)).unwrap(),
);
};
}
insert!("neverest-check", crate::cli::check::CheckOutput);
insert!("neverest-configure", crate::cli::configure::ConfigureOutput);
insert!("neverest-init", crate::cli::init::InitOutput);
insert!("neverest-sync", crate::sync::report::SyncOutput);
insert!(
"neverest-conflict-list",
crate::conflict::report::ConflictListOutput
);
insert!(
"neverest-conflict-show",
crate::conflict::report::ConflictShowOutput
);
insert!(
"neverest-conflict-resolve",
crate::conflict::report::ConflictResolveOutput
);
schemas
}
#[cfg(test)]
mod tests {
use clap::{Command, CommandFactory};
use super::schemas;
use crate::cli::main::Cli;
#[test]
fn every_registered_key_names_a_command() {
let mut paths = Vec::new();
collect(&Cli::command(), String::new(), &mut paths);
for cmd in schemas().keys() {
assert!(paths.contains(cmd), "{cmd} names no command");
}
}
fn collect(command: &Command, prefix: String, paths: &mut Vec<String>) {
let path = match prefix.is_empty() {
true => command.get_name().to_string(),
false => format!("{prefix}-{name}", name = command.get_name()),
};
for sub in command.get_subcommands() {
collect(sub, path.clone(), paths);
}
paths.push(path);
}
}