1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
use crate::{CommandSummary, FlagSummary, ParamSummary, ResourceSummary};

/// Intent structure which represents user intent.
#[derive(Debug, Clone, PartialEq)]
pub struct Intent {
    args: Vec<String>,
    command: CommandSummary,
    supcommands: Vec<CommandSummary>,
    subcommands: Vec<CommandSummary>,
    flags: Vec<FlagSummary>,
    params: Vec<ParamSummary>,
    resources: Vec<ResourceSummary>,
}

/// Intent structure implementation.
impl Intent {

    /// Returns new instance.
    pub fn new(
        args: Vec<String>,
        command: CommandSummary,
        supcommands: Vec<CommandSummary>,
        subcommands: Vec<CommandSummary>,
        flags: Vec<FlagSummary>,
        params: Vec<ParamSummary>,
        resources: Vec<ResourceSummary>,
    ) -> Self {
        Self {
            args,
            command,
            supcommands,
            subcommands,
            flags,
            params,
            resources
        }
    }

    /// Returns raw command-line arguments.
    pub fn args(&self) -> &Vec<String> {
        &self.args
    }

    /// Returns summary objects of the executed command.
    pub fn command(&self) -> &CommandSummary {
        &self.command
    }

    /// Returns summary objects of parent commands in a tree.
    pub fn supcommands(&self) -> &Vec<CommandSummary> {
        &self.supcommands
    }

    /// Returns summary objects of child commands.
    pub fn subcommands(&self) -> &Vec<CommandSummary>{
        &self.subcommands
    }

    /// Returns summary objects of all flags.
    pub fn flags(&self) -> &Vec<FlagSummary> {
        &self.flags
    }

    /// Returns summary objects of a specific flag.
    pub fn flag<S: Into<String>>(&self, name: S) -> Option<&FlagSummary> {
        let name = name.into();
        self.flags.iter().find(|f| *f.name() == name)
    }

    /// Returns summary objects of all params.
    pub fn params(&self) -> &Vec<ParamSummary> {
        &self.params
    }

    /// Returns summary objects of a specific flag.
    pub fn param<S: Into<String>>(&self, name: S) -> Option<&ParamSummary> {
        let name = name.into();
        self.params.iter().find(|f| *f.name() == name)
    }
    
    /// Returns summary objects of all resources.
    pub fn resources(&self) -> &Vec<ResourceSummary> {
        &self.resources
    }

    /// Returns summary objects of a specific resource.
    pub fn resource<S: Into<String>>(&self, name: S) -> Option<&ResourceSummary> {
        let name = name.into();
        self.resources.iter().find(|f| *f.name() == name)
    }

    /// Returns true if command-line arguments are present.
    pub fn has_args(&self) -> bool {
        !self.args.is_empty()
    }

    /// Returns true if command-line argument is present.
    pub fn has_arg<S: Into<String>>(&self, name: S) -> bool {
        let name = name.into();
        self.args.iter().any(|a| *a == name)
    }

    /// Returns true if the executed command has parent commands.
    pub fn has_supcommands(&self) -> bool {
        !self.supcommands.is_empty()
    }

    /// Returns true if the executed command has child commands.
    pub fn has_subcommands(&self) -> bool {
        !self.subcommands.is_empty()
    }

    /// Returns true if the executed command has flags.
    pub fn has_flags(&self) -> bool {
        !self.flags.is_empty()
    }

    /// Returns true if flag is present.
    pub fn has_flag<S: Into<String>>(&self, name: S) -> bool {
        self.flag(name.into()).is_some()
    }

    /// Returns true if flag is present.
    pub fn has_provided_flag<S: Into<String>>(&self, name: S) -> bool {
        let name = name.into();
        match self.flag(name) {
            Some(f) => f.provided(),
            None => false,
        }
    }

    /// Returns true if the executed command has params.
    pub fn has_params(&self) -> bool {
        !self.params.is_empty()
    }

    /// Returns true if param is present.
    pub fn has_param<S: Into<String>>(&self, name: S) -> bool {
        self.param(name.into()).is_some()
    }

    /// Returns true if param is present.
    pub fn has_provided_param<S: Into<String>>(&self, name: S) -> bool {
        let name = name.into();
        match self.param(name) {
            Some(f) => f.provided(),
            None => false,
        }
    }

    /// Returns true if the executed command has resources.
    pub fn has_resources(&self) -> bool {
        !self.resources.is_empty()
    }

    /// Returns true if resource is present.
    pub fn has_resource<S: Into<String>>(&self, name: S) -> bool {
        self.resource(name.into()).is_some()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn intent_with_args(args: Vec<String>) -> Intent {
        let command: CommandSummary = CommandSummary::with_name("", None, None, None, None);
        let supcommands: Vec<CommandSummary> = vec![];
        let subcommands: Vec<CommandSummary> = vec![];
        let flags: Vec<FlagSummary> = vec![];
        let params: Vec<ParamSummary> = vec![];
        let resources: Vec<ResourceSummary> = vec![];
        Intent::new(args, command, supcommands, subcommands, flags, params, resources)
    }

    fn intent_with_flags(flags: Vec<FlagSummary>) -> Intent {
        let args: Vec<String> = vec![];
        let command: CommandSummary = CommandSummary::with_name("", None, None, None, None);
        let supcommands: Vec<CommandSummary> = vec![];
        let subcommands: Vec<CommandSummary> = vec![];
        let flags: Vec<FlagSummary> = flags;
        let params: Vec<ParamSummary> = vec![];
        let resources: Vec<ResourceSummary> = vec![];
        Intent::new(args, command, supcommands, subcommands, flags, params, resources)
    }

    fn intent_with_params(params: Vec<ParamSummary>) -> Intent {
        let args: Vec<String> = vec![];
        let command: CommandSummary = CommandSummary::with_name("", None, None, None, None);
        let supcommands: Vec<CommandSummary> = vec![];
        let subcommands: Vec<CommandSummary> = vec![];
        let flags: Vec<FlagSummary> = vec![];
        let params: Vec<ParamSummary> = params;
        let resources: Vec<ResourceSummary> = vec![];
        Intent::new(args, command, supcommands, subcommands, flags, params, resources)
    }

    #[test]
    fn provides_flag_by_name() {
        let intent = intent_with_flags(vec![
            FlagSummary::with_name("a", None, None, None, None, false, false),
            FlagSummary::with_name("b", None, None, None, None, false, false),
        ]);
        assert_eq!(intent.flag("b").unwrap().name(), "b");
    }

    #[test]
    fn provides_param_by_name() {
        let intent = intent_with_params(vec![
            ParamSummary::with_name("a", None, None, None, false),
            ParamSummary::with_name("b", None, None, None, false),
        ]);
        assert_eq!(intent.param("b").unwrap().name(), "b");
    }

    #[test]
    fn checks_argument_existance() {
        let intent = intent_with_args(vec![
            "--b".to_string(), "-c".to_string(),
        ]);
        assert_eq!(intent.has_arg("-c"), true);
        assert_eq!(intent.has_arg("b"), false);
    }

    #[test]
    fn checks_flag_existance() {
        let intent = intent_with_flags(vec![
            FlagSummary::with_name("b", None, None, None, None, false, false),
        ]);
        assert_eq!(intent.has_flag("b"), true);
        assert_eq!(intent.has_flag("x"), false);
    }

    #[test]
    fn checks_param_existance() {
        let intent = intent_with_params(vec![
            ParamSummary::with_name("b", None, None, None, false),
        ]);
        assert_eq!(intent.has_param("b"), true);
        assert_eq!(intent.has_param("x"), false);
    }
}