use std::rc::Rc;
use super::BaseCommand;
use crate::Result;
pub struct BasicCommand<'a, S> {
name: &'a str,
help: &'a str,
exec: Rc<dyn Fn(&mut S, &[String]) -> Result<String>>,
}
impl<'a, S> BasicCommand<'a, S> {
pub fn new<F>(name: &'a str, exec: F) -> BasicCommand<'a, S>
where
F: Fn(&mut S, &[String]) -> Result<String> + 'static,
{
BasicCommand {
name,
help: "",
exec: Rc::new(exec),
}
}
pub fn new_with_help<F>(name: &'a str, help: &'a str, exec: F) -> BasicCommand<'a, S>
where
F: Fn(&mut S, &[String]) -> Result<String> + 'static,
{
BasicCommand {
name,
help,
exec: Rc::new(exec),
}
}
}
impl<'a, S> BaseCommand for BasicCommand<'a, S> {
type State = S;
fn name(&self) -> &str {
self.name
}
fn validate_args(&self, _: &[String]) -> Result<()> {
Ok(())
}
fn execute(&self, state: &mut S, args: &[String]) -> Result<String> {
(self.exec)(state, args)
}
fn help(&self) -> String {
self.help.to_string()
}
}