1use crate::error::*;
2use crate::Callback;
3use crate::Parameter;
4use std::fmt;
5
6pub struct Command<Context, E> {
8 pub(crate) name: String,
9 pub(crate) parameters: Vec<Parameter>,
10 pub(crate) callback: Callback<Context, E>,
11 pub(crate) help_summary: Option<String>,
12}
13
14impl<Context, E> fmt::Debug for Command<Context, E> {
15 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
16 f.debug_struct("Command")
17 .field("name", &self.name)
18 .field("parameters", &self.parameters)
19 .field("help_summary", &self.help_summary)
20 .finish()
21 }
22}
23
24impl<Context, E> std::cmp::PartialEq for Command<Context, E> {
25 fn eq(&self, other: &Command<Context, E>) -> bool {
26 self.name == other.name
27 && self.parameters == other.parameters
28 && self.help_summary == other.help_summary
29 }
30}
31
32impl<Context, E> Command<Context, E> {
33 pub fn new(name: &str, callback: Callback<Context, E>) -> Self {
35 Self {
36 name: name.to_string(),
37 parameters: vec![],
38 callback,
39 help_summary: None,
40 }
41 }
42
43 pub fn with_parameter(mut self, parameter: Parameter) -> Result<Command<Context, E>> {
46 if parameter.required && self.parameters.iter().any(|param| !param.required) {
47 return Err(Error::IllegalRequiredError(parameter.name));
48 }
49
50 self.parameters.push(parameter);
51
52 Ok(self)
53 }
54
55 pub fn with_help(mut self, help: &str) -> Command<Context, E> {
57 self.help_summary = Some(help.to_string());
58
59 self
60 }
61}