use crate::Result;
pub mod echo;
pub mod exit;
pub mod help;
pub mod helptree;
pub mod history;
pub use echo::*;
pub use exit::*;
pub use help::*;
pub use helptree::*;
pub use history::*;
pub mod example {
pub use super::echo::EchoCommand;
}
pub(crate) mod builtin {
pub use super::exit::ExitCommand;
pub use super::help::HelpCommand;
pub use super::helptree::HelpTreeCommand;
pub use super::history::HistoryCommand;
}
pub mod parent;
pub use parent::ParentCommand;
pub mod basic;
pub use basic::BasicCommand;
pub enum Command<'a, S> {
Leaf(Box<dyn BaseCommand<State = S> + 'a>),
Parent(ParentCommand<'a, S>),
}
impl<'a, S> Command<'a, S> {
pub fn new_leaf<C>(child_cmd: C) -> Self
where
C: BaseCommand<State = S> + 'a,
{
Self::Leaf(Box::new(child_cmd))
}
pub fn new_parent(name: &'a str, sub_cmds: Vec<Command<'a, S>>) -> Self {
Self::Parent(ParentCommand::new(name, sub_cmds))
}
}
impl<'a, S> BaseCommand for Command<'a, S> {
type State = S;
fn name(&self) -> &str {
match self {
Self::Leaf(cmd) => cmd.name(),
Self::Parent(parent_cmd) => parent_cmd.name(),
}
}
fn validate_args(&self, args: &[String]) -> Result<()> {
match self {
Self::Leaf(cmd) => cmd.validate_args(args),
Self::Parent(parent_cmd) => parent_cmd.validate_args(args),
}
}
fn execute(&self, state: &mut Self::State, args: &[String]) -> Result<String> {
match self {
Self::Leaf(cmd) => cmd.execute(state, args),
Self::Parent(parent_cmd) => parent_cmd.execute(state, args),
}
}
fn help(&self) -> String {
match self {
Self::Leaf(cmd) => cmd.help(),
Self::Parent(parent_cmd) => parent_cmd.help(),
}
}
}
#[derive(Debug, PartialEq)]
pub enum Completion {
PartialArgCompletion(Vec<String>),
Possibilities(Vec<String>),
Nothing,
}
pub trait BaseCommand {
type State;
fn name(&self) -> &str;
fn validate_args(&self, args: &[String]) -> Result<()>;
fn execute(&self, state: &mut Self::State, args: &[String]) -> Result<String>;
fn autocomplete(&self, _args: Vec<&str>, _trailing_space: bool) -> Completion {
Completion::Nothing
}
fn help(&self) -> String {
"".to_string()
}
}