Skip to content

Commit dca1347

Browse files
BenWiederhakecakebaker
authored andcommitted
make Options::apply fallible
1 parent e50cf46 commit dca1347

24 files changed

+118
-64
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,12 @@ struct Settings {
6767
// To implement `Options`, we only need to provide the `apply` method.
6868
// The `parse` method will be automatically generated.
6969
impl Options<Arg> for Settings {
70-
fn apply(&mut self, arg: Arg) {
70+
fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> {
7171
match arg {
7272
Arg::Caps => self.caps = true,
7373
Arg::ExclamationMarks(n) => self.exclamation_marks += n,
7474
}
75+
Ok(())
7576
}
7677
}
7778

docs/guide/port.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,11 @@ enum Arg {
9494
struct Settings { a: bool }
9595

9696
impl Options<Arg> for Settings {
97-
fn apply(&mut self, arg: Arg) {
97+
fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> {
9898
match arg {
9999
Arg::A => self.a = true,
100100
}
101+
Ok(())
101102
}
102103
}
103104

@@ -137,10 +138,11 @@ impl Default for Settings {
137138
}
138139

139140
impl Options<Arg> for Settings {
140-
fn apply(&mut self, arg: Arg) {
141+
fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> {
141142
match arg {
142143
Arg::A => self.a = false,
143144
}
145+
Ok(())
144146
}
145147
}
146148

@@ -175,10 +177,11 @@ enum Arg {
175177
struct Settings { a: u8 }
176178

177179
impl Options<Arg> for Settings {
178-
fn apply(&mut self, arg: Arg) {
180+
fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> {
179181
match arg {
180182
Arg::A => self.a += 1,
181183
}
184+
Ok(())
182185
}
183186
}
184187

@@ -215,10 +218,11 @@ enum Arg {
215218
struct Settings { a: OsString }
216219

217220
impl Options<Arg> for Settings {
218-
fn apply(&mut self, arg: Arg) {
221+
fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> {
219222
match arg {
220223
Arg::A(s) => self.a = s,
221224
}
225+
Ok(())
222226
}
223227
}
224228

@@ -255,10 +259,11 @@ enum Arg {
255259
struct Settings { a: Vec<OsString> }
256260

257261
impl Options<Arg> for Settings {
258-
fn apply(&mut self, arg: Arg) {
262+
fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> {
259263
match arg {
260264
Arg::A(s) => self.a.push(s),
261265
}
266+
Ok(())
262267
}
263268
}
264269

@@ -271,4 +276,4 @@ let a = Settings::default().parse(std::env::args_os()).unwrap().0.a;
271276
[Up](super)
272277
[Next](next)
273278

274-
</div>
279+
</div>

docs/guide/quick.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,11 @@ struct Settings {
5959
}
6060

6161
impl Options<Arg> for Settings {
62-
fn apply(&mut self, arg: Arg) {
62+
fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> {
6363
match arg {
6464
Arg::Force => self.force = true,
6565
}
66+
Ok(())
6667
}
6768
}
6869

@@ -100,11 +101,12 @@ struct Settings {
100101
}
101102

102103
impl Options<Arg> for Settings {
103-
fn apply(&mut self, arg: Arg) {
104+
fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> {
104105
match arg {
105106
Arg::Force => self.force = true,
106107
Arg::NoForce => self.force = false,
107108
}
109+
Ok(())
108110
}
109111
}
110112

@@ -160,10 +162,11 @@ enum Arg {
160162
# }
161163
#
162164
# impl Options<Arg> for Settings {
163-
# fn apply(&mut self, arg: Arg) {
165+
# fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> {
164166
# match arg {
165167
# Arg::Name(name) => self.name = name,
166168
# }
169+
# Ok(())
167170
# }
168171
# }
169172
#
@@ -197,10 +200,11 @@ enum Arg {
197200
# }
198201
#
199202
# impl Options<Arg> for Settings {
200-
# fn apply(&mut self, arg: Arg) {
203+
# fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> {
201204
# match arg {
202205
# Arg::Name(name) => self.name = name,
203206
# }
207+
# Ok(())
204208
# }
205209
# }
206210
#
@@ -234,10 +238,11 @@ enum Arg {
234238
# }
235239
#
236240
# impl Options<Arg> for Settings {
237-
# fn apply(&mut self, arg: Arg) {
241+
# fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> {
238242
# match arg {
239243
# Arg::Force(b) => self.force = b,
240244
# }
245+
# Ok(())
241246
# }
242247
# }
243248
#
@@ -269,10 +274,11 @@ enum Arg {
269274
# }
270275
#
271276
# impl Options<Arg> for Settings {
272-
# fn apply(&mut self, arg: Arg) {
277+
# fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> {
273278
# match arg {
274279
# Arg::Sort(s) => self.sort = s,
275280
# }
281+
# Ok(())
276282
# }
277283
# }
278284
#
@@ -287,4 +293,4 @@ enum Arg {
287293
[Up](super)
288294
[Next](next)
289295

290-
</div>
296+
</div>

examples/completion.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ enum Arg {
3333
struct Settings;
3434

3535
impl Options<Arg> for Settings {
36-
fn apply(&mut self, _arg: Arg) {
36+
fn apply(&mut self, _arg: Arg) -> Result<(), uutils_args::Error> {
3737
panic!("Compile with the 'parse-is-complete' feature!")
3838
}
3939
}

examples/deprecated.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@ struct Settings {
3434
}
3535

3636
impl Options<Arg> for Settings {
37-
fn apply(&mut self, arg: Arg) {
37+
fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> {
3838
match arg {
3939
Arg::Min(n) => self.n1 = n,
4040
Arg::Plus(n) => self.n2 = n,
4141
}
42+
Ok(())
4243
}
4344
}
4445

examples/hello_world.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@ struct Settings {
2222
}
2323

2424
impl Options<Arg> for Settings {
25-
fn apply(&mut self, arg: Arg) {
25+
fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> {
2626
match arg {
2727
Arg::Name(n) => self.name = n,
2828
Arg::Count(c) => self.count = c,
2929
Arg::Hidden => {}
3030
}
31+
Ok(())
3132
}
3233
}
3334

examples/value.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ struct Settings {
2525
}
2626

2727
impl Options<Arg> for Settings {
28-
fn apply(&mut self, arg: Arg) {
28+
fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> {
2929
match arg {
3030
Arg::Color(c) => self.color = c,
3131
}
32+
Ok(())
3233
}
3334
}
3435

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ impl<T: Arguments> ArgumentIter<T> {
166166
/// call [`Options::apply`] on the result until the arguments are exhausted.
167167
pub trait Options<Arg: Arguments>: Sized {
168168
/// Apply a single argument to the options.
169-
fn apply(&mut self, arg: Arg);
169+
fn apply(&mut self, arg: Arg) -> Result<(), Error>;
170170

171171
/// Parse an iterator of arguments into the options
172172
#[allow(unused_mut)]
@@ -191,7 +191,7 @@ pub trait Options<Arg: Arguments>: Sized {
191191
{
192192
let mut iter = ArgumentIter::<Arg>::from_args(args);
193193
while let Some(arg) = iter.next_arg()? {
194-
self.apply(arg);
194+
self.apply(arg)?;
195195
}
196196
Ok((self, iter.positional_arguments))
197197
}

tests/coreutils/b2sum.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ struct Settings {
4646
}
4747

4848
impl Options<Arg> for Settings {
49-
fn apply(&mut self, arg: Arg) {
49+
fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> {
5050
match arg {
5151
Arg::Binary => self.binary = true,
5252
Arg::Check => self.check = true,
@@ -57,6 +57,7 @@ impl Options<Arg> for Settings {
5757
Arg::Strict => self.strict = true,
5858
Arg::Warn => self.check_output = CheckOutput::Warn,
5959
}
60+
Ok(())
6061
}
6162
}
6263

tests/coreutils/base32.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,14 @@ impl Default for Settings {
3434
}
3535

3636
impl Options<Arg> for Settings {
37-
fn apply(&mut self, arg: Arg) {
37+
fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> {
3838
match arg {
3939
Arg::Decode => self.decode = true,
4040
Arg::IgnoreGarbage => self.ignore_garbage = true,
4141
Arg::Wrap(0) => self.wrap = None,
4242
Arg::Wrap(x) => self.wrap = Some(x),
4343
}
44+
Ok(())
4445
}
4546
}
4647

0 commit comments

Comments
 (0)