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
use crate::{CommandSummary, FlagSummary, ResourceSummary};
#[derive(Debug, Clone, PartialEq)]
pub struct Intent {
args: Vec<String>,
command: CommandSummary,
supcommands: Vec<CommandSummary>,
subcommands: Vec<CommandSummary>,
flags: Vec<FlagSummary>,
resources: Vec<ResourceSummary>,
}
impl Intent {
pub fn new(
args: Vec<String>,
command: CommandSummary,
supcommands: Vec<CommandSummary>,
subcommands: Vec<CommandSummary>,
flags: Vec<FlagSummary>,
resources: Vec<ResourceSummary>,
) -> Self {
Self {
args,
command,
supcommands,
subcommands,
flags,
resources
}
}
pub fn args(&self) -> &Vec<String> {
&self.args
}
pub fn command(&self) -> &CommandSummary {
&self.command
}
pub fn supcommands(&self) -> &Vec<CommandSummary> {
&self.supcommands
}
pub fn subcommands(&self) -> &Vec<CommandSummary>{
&self.subcommands
}
pub fn flags(&self) -> &Vec<FlagSummary> {
&self.flags
}
pub fn flag<S: Into<String>>(&self, name: S) -> Option<&FlagSummary> {
let name = name.into();
self.flags.iter().find(|f| *f.name() == name)
}
pub fn resources(&self) -> &Vec<ResourceSummary> {
&self.resources
}
pub fn resource<S: Into<String>>(&self, name: S) -> Option<&ResourceSummary> {
let name = name.into();
self.resources.iter().find(|f| *f.name() == name)
}
pub fn has_args(&self) -> bool {
!self.args.is_empty()
}
pub fn has_arg<S: Into<String>>(&self, name: S) -> bool {
let name = name.into();
self.args.iter().any(|a| *a == name)
}
pub fn has_supcommands(&self) -> bool {
!self.supcommands.is_empty()
}
pub fn has_subcommands(&self) -> bool {
!self.subcommands.is_empty()
}
pub fn has_flags(&self) -> bool {
!self.flags.is_empty()
}
pub fn has_flag<S: Into<String>>(&self, name: S) -> bool {
self.flag(name.into()).is_some()
}
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,
}
}
pub fn has_resources(&self) -> bool {
!self.resources.is_empty()
}
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 resources: Vec<ResourceSummary> = vec![];
Intent::new(args, command, supcommands, subcommands, flags, 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 resources: Vec<ResourceSummary> = vec![];
Intent::new(args, command, supcommands, subcommands, flags, 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 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);
}
}