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
//! This module defines the [Recursion] options. To set it up from [Cli], a [Config] and its
//! [Default] value, use the [configure_from](Recursion::configure_from) method.
use crate::app::Cli;
use crate::config_file::Config;
/// The options relating to recursion.
#[derive(Clone, Debug, Copy, PartialEq, Eq)]
pub struct Recursion {
/// Whether the recursion into directories is enabled.
pub enabled: bool,
/// The depth for how far to recurse into directories.
pub depth: usize,
}
impl Recursion {
/// Get the Recursion from either [Cli], a [Config] or the [Default] value.
///
/// The "enabled" value is determined by [enabled_from](Recursion::enabled_from) and the depth
/// value is determined by [depth_from](Recursion::depth_from).
///
/// # Errors
///
/// If [depth_from](Recursion::depth_from) returns an [Error], this returns it.
pub fn configure_from(cli: &Cli, config: &Config) -> Self {
let enabled = Self::enabled_from(cli, config);
let depth = Self::depth_from(cli, config);
Self { enabled, depth }
}
/// Get the "enabled" boolean from [Cli], a [Config] or the [Default] value. The first
/// value that is not [None] is used. The order of precedence for the value used is:
/// - [enabled_from_cli](Recursion::enabled_from_cli)
/// - [Config.recursion.enabled]
/// - [Default::default]
fn enabled_from(cli: &Cli, config: &Config) -> bool {
if let Some(value) = Self::enabled_from_cli(cli) {
return value;
}
if let Some(recursion) = &config.recursion {
if let Some(enabled) = recursion.enabled {
return enabled;
}
}
Default::default()
}
/// Get a potential "enabled" boolean from [Cli].
///
/// If the "recursive" argument is passed, this returns `true` in a [Some]. Otherwise this
/// returns [None].
fn enabled_from_cli(cli: &Cli) -> Option<bool> {
if cli.recursive { Some(true) } else { None }
}
/// Get the "depth" integer from [Cli], a [Config] or the [Default] value. The first
/// value that is not [None] is used. The order of precedence for the value used is:
/// - Cli::depth
/// - [Config.recursion.depth]
/// - [Default::default]
///
/// # Note
///
/// If both configuration file and Args is error, this will return a Max-Uint value.
fn depth_from(cli: &Cli, config: &Config) -> usize {
if let Some(value) = cli.depth {
return value;
}
use crate::config_file::Recursion;
if let Some(Recursion {
depth: Some(value), ..
}) = &config.recursion
{
return *value;
}
usize::MAX
}
}
/// The default values for `Recursion` are the boolean default and [prim@usize::max_value()].
impl Default for Recursion {
fn default() -> Self {
Self {
depth: usize::MAX,
enabled: false,
}
}
}
#[cfg(test)]
mod test {
use clap::Parser;
use clap::error::ErrorKind;
use super::Recursion;
use crate::app::Cli;
use crate::config_file::{self, Config};
#[test]
fn test_enabled_from_cli_empty() {
let argv = ["lsd"];
let cli = Cli::try_parse_from(argv).unwrap();
assert_eq!(None, Recursion::enabled_from_cli(&cli));
}
#[test]
fn test_enabled_from_cli_true() {
let argv = ["lsd", "--recursive"];
let cli = Cli::try_parse_from(argv).unwrap();
assert_eq!(Some(true), Recursion::enabled_from_cli(&cli));
}
#[test]
fn test_enabled_from_empty_matches_and_config() {
let argv = ["lsd"];
assert!(!Recursion::enabled_from(
&Cli::try_parse_from(argv).unwrap(),
&Config::with_none()
));
}
#[test]
fn test_enabled_from_matches_empty_and_config_true() {
let argv = ["lsd"];
let mut c = Config::with_none();
c.recursion = Some(config_file::Recursion {
enabled: Some(true),
depth: None,
});
assert!(Recursion::enabled_from(
&Cli::try_parse_from(argv).unwrap(),
&c
));
}
#[test]
fn test_enabled_from_matches_empty_and_config_false() {
let argv = ["lsd"];
let mut c = Config::with_none();
c.recursion = Some(config_file::Recursion {
enabled: Some(false),
depth: None,
});
assert!(!Recursion::enabled_from(
&Cli::try_parse_from(argv).unwrap(),
&c
));
}
// The following depth_from_cli tests are implemented using match expressions instead
// of the assert_eq macro, because clap::Error does not implement PartialEq.
#[test]
fn test_depth_from_cli_empty() {
let argv = ["lsd"];
let cli = Cli::try_parse_from(argv).unwrap();
assert!(cli.depth.is_none());
}
#[test]
fn test_depth_from_cli_integer() {
let argv = ["lsd", "--depth", "42"];
let cli = Cli::try_parse_from(argv).unwrap();
assert!(matches!(cli.depth, Some(42)));
}
#[test]
fn test_depth_from_cli_depth_multi() {
let argv = ["lsd", "--depth", "4", "--depth", "2"];
let cli = Cli::try_parse_from(argv).unwrap();
assert!(matches!(cli.depth, Some(2)));
}
#[test]
fn test_depth_from_cli_neg_int() {
let argv = ["lsd", "--depth", "\\-42"];
let cli = Cli::try_parse_from(argv);
assert!(matches!(cli, Err(e) if e.kind() == ErrorKind::ValueValidation));
}
#[test]
fn test_depth_from_cli_non_int() {
let argv = ["lsd", "--depth", "foo"];
let cli = Cli::try_parse_from(argv);
assert!(matches!(cli, Err(e) if e.kind() == ErrorKind::ValueValidation));
}
#[test]
fn test_depth_from_config_none_max() {
let argv = ["lsd"];
let cli = Cli::try_parse_from(argv).unwrap();
assert_eq!(
usize::MAX,
Recursion::depth_from(&cli, &Config::with_none())
);
}
#[test]
fn test_depth_from_config_pos_integer() {
let argv = ["lsd"];
let mut c = Config::with_none();
c.recursion = Some(config_file::Recursion {
enabled: None,
depth: Some(42),
});
assert_eq!(
42,
Recursion::depth_from(&Cli::try_parse_from(argv).unwrap(), &c)
);
}
}