Skip to main content

safe_chains/registry/
mod.rs

1mod build;
2mod dispatch;
3mod docs;
4mod policy;
5pub(crate) mod types;
6
7use std::collections::HashMap;
8use std::sync::LazyLock;
9
10use crate::parse::Token;
11use crate::verdict::Verdict;
12
13pub use build::{build_registry, load_toml};
14pub use dispatch::dispatch_spec;
15pub use types::{CommandSpec, OwnedPolicy};
16
17type HandlerFn = fn(&[Token]) -> Verdict;
18
19static CMD_HANDLERS: LazyLock<HashMap<&'static str, HandlerFn>> =
20    LazyLock::new(crate::handlers::custom_cmd_handlers);
21
22static SUB_HANDLERS: LazyLock<HashMap<&'static str, HandlerFn>> =
23    LazyLock::new(crate::handlers::custom_sub_handlers);
24
25static TOML_REGISTRY: LazyLock<HashMap<String, CommandSpec>> = LazyLock::new(||
26    include!(concat!(env!("OUT_DIR"), "/toml_includes.rs"))
27);
28
29pub fn toml_dispatch(tokens: &[Token]) -> Option<Verdict> {
30    let cmd = tokens[0].command_name();
31    TOML_REGISTRY.get(cmd).map(|spec| dispatch_spec(tokens, spec))
32}
33
34pub fn toml_command_names() -> Vec<&'static str> {
35    TOML_REGISTRY
36        .keys()
37        .map(|k| k.as_str())
38        .collect()
39}
40
41pub fn toml_command_docs() -> Vec<crate::docs::CommandDoc> {
42    TOML_REGISTRY
43        .iter()
44        .filter(|(key, spec)| *key == &spec.name)
45        .map(|(_, spec)| spec.to_command_doc())
46        .collect()
47}
48
49#[cfg(test)]
50mod tests;