pikadick_slash_framework/
argument.rs

1use crate::{
2    BuilderError,
3    DataType,
4};
5
6/// An argument.
7///
8/// Specifically, this is a parameter, not a value.
9#[derive(Debug)]
10pub struct ArgumentParam {
11    name: Box<str>,
12    kind: DataType,
13    description: Box<str>,
14    required: bool,
15}
16
17impl ArgumentParam {
18    /// Get the name of the argument
19    pub fn name(&self) -> &str {
20        &self.name
21    }
22
23    /// Get the argument kind
24    pub fn kind(&self) -> DataType {
25        self.kind
26    }
27
28    /// Get the description of the argument
29    pub fn description(&self) -> &str {
30        &self.description
31    }
32
33    /// Check if the argument is required
34    pub fn required(&self) -> bool {
35        self.required
36    }
37}
38
39/// An argument param builder
40#[derive(Debug)]
41pub struct ArgumentParamBuilder<'a, 'b> {
42    name: Option<&'a str>,
43    kind: Option<DataType>,
44    description: Option<&'b str>,
45    required: bool,
46}
47
48impl<'a, 'b> ArgumentParamBuilder<'a, 'b> {
49    /// Make a new [`ArgumentParamBuilder`].
50    pub fn new() -> Self {
51        Self {
52            name: None,
53            kind: None,
54            description: None,
55            required: false,
56        }
57    }
58
59    /// Set the name
60    pub fn name(&mut self, name: &'a str) -> &mut Self {
61        self.name = Some(name);
62        self
63    }
64
65    /// Set the kind
66    pub fn kind(&mut self, kind: DataType) -> &mut Self {
67        self.kind = Some(kind);
68        self
69    }
70
71    /// Set the description
72    pub fn description(&mut self, description: &'b str) -> &mut Self {
73        self.description = Some(description);
74        self
75    }
76
77    /// Set if the argument is required
78    pub fn required(&mut self, required: bool) -> &mut Self {
79        self.required = required;
80        self
81    }
82
83    /// Build the argument param
84    pub fn build(&mut self) -> Result<ArgumentParam, BuilderError> {
85        #[allow(clippy::or_fun_call)]
86        let name = self.name.ok_or(BuilderError::MissingField("name"))?;
87        #[allow(clippy::or_fun_call)]
88        let kind = self.kind.ok_or(BuilderError::MissingField("kind"))?;
89        #[allow(clippy::or_fun_call)]
90        let description = self
91            .description
92            .ok_or(BuilderError::MissingField("description"))?;
93
94        Ok(ArgumentParam {
95            name: name.into(),
96            kind,
97            description: description.into(),
98            required: self.required,
99        })
100    }
101}
102
103impl<'a, 'b> Default for ArgumentParamBuilder<'a, 'b> {
104    fn default() -> Self {
105        Self::new()
106    }
107}