use std::collections::HashMap;
#[derive(Debug, Clone)]
pub struct CommandDoc {
pub description: Option<String>,
pub examples: Vec<String>,
pub notes: Vec<String>,
}
impl CommandDoc {
pub fn new<D, E, N>(description: D, examples: E, notes: N) -> Self
where
D: Into<String>,
E: IntoIterator,
E::Item: Into<String>,
N: IntoIterator,
N::Item: Into<String>,
{
Self {
description: Some(description.into()).filter(|s| !s.is_empty()),
examples: examples.into_iter().map(Into::into).collect(),
notes: notes.into_iter().map(Into::into).collect(),
}
}
}
#[derive(Default)]
pub struct DocRegistry {
commands: HashMap<String, CommandDoc>,
guides: HashMap<String, String>,
}
impl DocRegistry {
pub fn new() -> Self {
Self::default()
}
pub fn register_command<K: Into<String>>(&mut self, key: K, doc: CommandDoc) {
self.commands.insert(key.into(), doc);
}
pub fn register_guide<K, C>(&mut self, key: K, content: C)
where
K: Into<String>,
C: Into<String>,
{
self.guides.insert(key.into(), content.into());
}
pub fn command(&self, key: &str) -> Option<&CommandDoc> {
self.commands.get(key)
}
pub fn guide(&self, key: &str) -> Option<&str> {
self.guides.get(key).map(|s| s.as_str())
}
}