diff --git a/README.md b/README.md index d27b71e..15801e5 100644 --- a/README.md +++ b/README.md @@ -67,11 +67,12 @@ struct Settings { // To implement `Options`, we only need to provide the `apply` method. // The `parse` method will be automatically generated. impl Options for Settings { - fn apply(&mut self, arg: Arg) { + fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> { match arg { Arg::Caps => self.caps = true, Arg::ExclamationMarks(n) => self.exclamation_marks += n, } + Ok(()) } } diff --git a/docs/guide/port.md b/docs/guide/port.md index f7d6070..990f59b 100644 --- a/docs/guide/port.md +++ b/docs/guide/port.md @@ -94,10 +94,11 @@ enum Arg { struct Settings { a: bool } impl Options for Settings { - fn apply(&mut self, arg: Arg) { + fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> { match arg { Arg::A => self.a = true, } + Ok(()) } } @@ -137,10 +138,11 @@ impl Default for Settings { } impl Options for Settings { - fn apply(&mut self, arg: Arg) { + fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> { match arg { Arg::A => self.a = false, } + Ok(()) } } @@ -175,10 +177,11 @@ enum Arg { struct Settings { a: u8 } impl Options for Settings { - fn apply(&mut self, arg: Arg) { + fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> { match arg { Arg::A => self.a += 1, } + Ok(()) } } @@ -215,10 +218,11 @@ enum Arg { struct Settings { a: OsString } impl Options for Settings { - fn apply(&mut self, arg: Arg) { + fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> { match arg { Arg::A(s) => self.a = s, } + Ok(()) } } @@ -255,10 +259,11 @@ enum Arg { struct Settings { a: Vec } impl Options for Settings { - fn apply(&mut self, arg: Arg) { + fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> { match arg { Arg::A(s) => self.a.push(s), } + Ok(()) } } @@ -271,4 +276,4 @@ let a = Settings::default().parse(std::env::args_os()).unwrap().0.a; [Up](super) [Next](next) - \ No newline at end of file + diff --git a/docs/guide/quick.md b/docs/guide/quick.md index 31f515b..5de6afb 100644 --- a/docs/guide/quick.md +++ b/docs/guide/quick.md @@ -59,10 +59,11 @@ struct Settings { } impl Options for Settings { - fn apply(&mut self, arg: Arg) { + fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> { match arg { Arg::Force => self.force = true, } + Ok(()) } } @@ -100,11 +101,12 @@ struct Settings { } impl Options for Settings { - fn apply(&mut self, arg: Arg) { + fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> { match arg { Arg::Force => self.force = true, Arg::NoForce => self.force = false, } + Ok(()) } } @@ -160,10 +162,11 @@ enum Arg { # } # # impl Options for Settings { -# fn apply(&mut self, arg: Arg) { +# fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> { # match arg { # Arg::Name(name) => self.name = name, # } +# Ok(()) # } # } # @@ -197,10 +200,11 @@ enum Arg { # } # # impl Options for Settings { -# fn apply(&mut self, arg: Arg) { +# fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> { # match arg { # Arg::Name(name) => self.name = name, # } +# Ok(()) # } # } # @@ -234,10 +238,11 @@ enum Arg { # } # # impl Options for Settings { -# fn apply(&mut self, arg: Arg) { +# fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> { # match arg { # Arg::Force(b) => self.force = b, # } +# Ok(()) # } # } # @@ -269,10 +274,11 @@ enum Arg { # } # # impl Options for Settings { -# fn apply(&mut self, arg: Arg) { +# fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> { # match arg { # Arg::Sort(s) => self.sort = s, # } +# Ok(()) # } # } # @@ -287,4 +293,4 @@ enum Arg { [Up](super) [Next](next) - \ No newline at end of file + diff --git a/examples/completion.rs b/examples/completion.rs index 112cc60..39a49ac 100644 --- a/examples/completion.rs +++ b/examples/completion.rs @@ -32,7 +32,7 @@ enum Arg { struct Settings; impl Options for Settings { - fn apply(&mut self, _arg: Arg) { + fn apply(&mut self, _arg: Arg) -> Result<(), uutils_args::Error> { panic!("Compile with the 'parse-is-complete' feature!") } } diff --git a/examples/deprecated.rs b/examples/deprecated.rs index 2a6d5bd..71db541 100644 --- a/examples/deprecated.rs +++ b/examples/deprecated.rs @@ -34,11 +34,12 @@ struct Settings { } impl Options for Settings { - fn apply(&mut self, arg: Arg) { + fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> { match arg { Arg::Min(n) => self.n1 = n, Arg::Plus(n) => self.n2 = n, } + Ok(()) } } diff --git a/examples/hello_world.rs b/examples/hello_world.rs index 7bd4eb0..8df6742 100644 --- a/examples/hello_world.rs +++ b/examples/hello_world.rs @@ -22,12 +22,13 @@ struct Settings { } impl Options for Settings { - fn apply(&mut self, arg: Arg) { + fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> { match arg { Arg::Name(n) => self.name = n, Arg::Count(c) => self.count = c, Arg::Hidden => {} } + Ok(()) } } diff --git a/examples/value.rs b/examples/value.rs index 5359192..67926a1 100644 --- a/examples/value.rs +++ b/examples/value.rs @@ -25,10 +25,11 @@ struct Settings { } impl Options for Settings { - fn apply(&mut self, arg: Arg) { + fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> { match arg { Arg::Color(c) => self.color = c, } + Ok(()) } } diff --git a/src/lib.rs b/src/lib.rs index 5164734..e7b7058 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -166,7 +166,7 @@ impl ArgumentIter { /// call [`Options::apply`] on the result until the arguments are exhausted. pub trait Options: Sized { /// Apply a single argument to the options. - fn apply(&mut self, arg: Arg); + fn apply(&mut self, arg: Arg) -> Result<(), Error>; /// Parse an iterator of arguments into the options #[allow(unused_mut)] @@ -191,7 +191,7 @@ pub trait Options: Sized { { let mut iter = ArgumentIter::::from_args(args); while let Some(arg) = iter.next_arg()? { - self.apply(arg); + self.apply(arg)?; } Ok((self, iter.positional_arguments)) } diff --git a/tests/coreutils/b2sum.rs b/tests/coreutils/b2sum.rs index 3f24c71..a2081e3 100644 --- a/tests/coreutils/b2sum.rs +++ b/tests/coreutils/b2sum.rs @@ -46,7 +46,7 @@ struct Settings { } impl Options for Settings { - fn apply(&mut self, arg: Arg) { + fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> { match arg { Arg::Binary => self.binary = true, Arg::Check => self.check = true, @@ -57,6 +57,7 @@ impl Options for Settings { Arg::Strict => self.strict = true, Arg::Warn => self.check_output = CheckOutput::Warn, } + Ok(()) } } diff --git a/tests/coreutils/base32.rs b/tests/coreutils/base32.rs index 24ce251..e44ad9a 100644 --- a/tests/coreutils/base32.rs +++ b/tests/coreutils/base32.rs @@ -34,13 +34,14 @@ impl Default for Settings { } impl Options for Settings { - fn apply(&mut self, arg: Arg) { + fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> { match arg { Arg::Decode => self.decode = true, Arg::IgnoreGarbage => self.ignore_garbage = true, Arg::Wrap(0) => self.wrap = None, Arg::Wrap(x) => self.wrap = Some(x), } + Ok(()) } } diff --git a/tests/coreutils/basename.rs b/tests/coreutils/basename.rs index 9324fe3..473610e 100644 --- a/tests/coreutils/basename.rs +++ b/tests/coreutils/basename.rs @@ -26,7 +26,7 @@ struct Settings { } impl Options for Settings { - fn apply(&mut self, arg: Arg) { + fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> { match arg { Arg::Multiple => self.multiple = true, Arg::Suffix(s) => { @@ -35,6 +35,7 @@ impl Options for Settings { } Arg::Zero => self.zero = true, } + Ok(()) } } diff --git a/tests/coreutils/cat.rs b/tests/coreutils/cat.rs index 03df331..8b8da96 100644 --- a/tests/coreutils/cat.rs +++ b/tests/coreutils/cat.rs @@ -48,7 +48,7 @@ struct Settings { } impl Options for Settings { - fn apply(&mut self, arg: Arg) { + fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> { match arg { Arg::ShowAll => { self.show_tabs = true; @@ -70,6 +70,7 @@ impl Options for Settings { Arg::NumberNonblank => self.number = NumberingMode::NonEmpty, Arg::SqueezeBlank => self.squeeze_blank = true, } + Ok(()) } } diff --git a/tests/coreutils/dd.rs b/tests/coreutils/dd.rs index fe5ef69..7401380 100644 --- a/tests/coreutils/dd.rs +++ b/tests/coreutils/dd.rs @@ -92,7 +92,7 @@ impl Default for Settings { } impl Options for Settings { - fn apply(&mut self, arg: Arg) { + fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> { match arg { Arg::Infile(f) => self.infile = Some(f), Arg::Outfile(f) => self.outfile = Some(f), @@ -111,6 +111,7 @@ impl Options for Settings { Arg::Iflag(_) => todo!(), Arg::Oflag(_) => todo!(), } + Ok(()) } } diff --git a/tests/coreutils/echo.rs b/tests/coreutils/echo.rs index 2d561f2..89c3fd7 100644 --- a/tests/coreutils/echo.rs +++ b/tests/coreutils/echo.rs @@ -24,12 +24,13 @@ struct Settings { } impl Options for Settings { - fn apply(&mut self, arg: Arg) { + fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> { match arg { Arg::NoNewline => self.trailing_newline = false, Arg::EnableEscape => self.escape = true, Arg::DisableEscape => self.escape = false, } + Ok(()) } } diff --git a/tests/coreutils/head.rs b/tests/coreutils/head.rs index cdf8326..90d6375 100644 --- a/tests/coreutils/head.rs +++ b/tests/coreutils/head.rs @@ -188,7 +188,7 @@ struct Settings { } impl Options for Settings { - fn apply(&mut self, arg: Arg) { + fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> { match arg { Arg::Bytes(n) => { self.mode = Mode::Bytes; @@ -202,6 +202,7 @@ impl Options for Settings { Arg::Verbose => self.verbose = true, Arg::Zero => self.zero = true, } + Ok(()) } } diff --git a/tests/coreutils/ls.rs b/tests/coreutils/ls.rs index 9673926..4eca547 100644 --- a/tests/coreutils/ls.rs +++ b/tests/coreutils/ls.rs @@ -360,7 +360,7 @@ impl Default for Settings { } impl Options for Settings { - fn apply(&mut self, arg: Arg) { + fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> { match arg { Arg::All => self.which_files = Files::All, Arg::AlmostAll => self.which_files = Files::AlmostAll, @@ -417,6 +417,7 @@ impl Options for Settings { } Arg::GroupDirectoriesFirst => self.group_directories_first = true, } + Ok(()) } } diff --git a/tests/coreutils/mktemp.rs b/tests/coreutils/mktemp.rs index 2410f7d..fb5608f 100644 --- a/tests/coreutils/mktemp.rs +++ b/tests/coreutils/mktemp.rs @@ -40,7 +40,7 @@ struct Settings { } impl Options for Settings { - fn apply(&mut self, arg: Arg) { + fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> { match arg { Arg::Directory => self.directory = true, Arg::DryRun => self.dry_run = true, @@ -49,6 +49,7 @@ impl Options for Settings { Arg::TreatAsTemplate => self.treat_as_template = true, Arg::TmpDir(dir) => self.tmp_dir = Some(dir), } + Ok(()) } } diff --git a/tests/coreutils/shuf.rs b/tests/coreutils/shuf.rs index 185e0b2..04ff88b 100644 --- a/tests/coreutils/shuf.rs +++ b/tests/coreutils/shuf.rs @@ -23,11 +23,12 @@ struct Settings { } impl Options for Settings { - fn apply(&mut self, arg: Arg) { + fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> { match arg { Arg::Echo => self.echo = true, Arg::Zero => self.zero = true, } + Ok(()) } } diff --git a/tests/coreutils/tail.rs b/tests/coreutils/tail.rs index 2c9573e..c302547 100644 --- a/tests/coreutils/tail.rs +++ b/tests/coreutils/tail.rs @@ -244,7 +244,7 @@ struct Settings { } impl Options for Settings { - fn apply(&mut self, arg: Arg) { + fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> { match arg { Arg::Bytes(n) => { self.mode = Mode::Bytes; @@ -268,6 +268,7 @@ impl Options for Settings { Arg::Zero => self.zero = true, Arg::PresumeInputPipe => self.presume_input_pipe = true, } + Ok(()) } } diff --git a/tests/coreutils/uniq.rs b/tests/coreutils/uniq.rs index 28a3264..dbcce1d 100644 --- a/tests/coreutils/uniq.rs +++ b/tests/coreutils/uniq.rs @@ -65,7 +65,7 @@ struct Settings { } impl Options for Settings { - fn apply(&mut self, arg: Arg) { + fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> { match arg { Arg::SkipFields(n) => { self.skip_fields = Some(n); @@ -100,6 +100,7 @@ impl Options for Settings { Arg::ZeroTerminated => { self.zero_terminated = true; } - } + }; + Ok(()) } } diff --git a/tests/flags.rs b/tests/flags.rs index c9877d1..b292c1b 100644 --- a/tests/flags.rs +++ b/tests/flags.rs @@ -14,10 +14,11 @@ fn one_flag() { } impl Options for Settings { - fn apply(&mut self, arg: Arg) { + fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> { match arg { Arg::Foo => self.foo = true, } + Ok(()) } } @@ -42,11 +43,12 @@ fn two_flags() { } impl Options for Settings { - fn apply(&mut self, arg: Arg) { + fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> { match arg { Arg::A => self.a = true, Arg::B => self.b = true, } + Ok(()) } } @@ -82,8 +84,9 @@ fn long_and_short_flag() { } impl Options for Settings { - fn apply(&mut self, Arg::Foo: Arg) { + fn apply(&mut self, Arg::Foo: Arg) -> Result<(), uutils_args::Error> { self.foo = true; + Ok(()) } } @@ -106,8 +109,9 @@ fn short_alias() { } impl Options for Settings { - fn apply(&mut self, Arg::Foo: Arg) { + fn apply(&mut self, Arg::Foo: Arg) -> Result<(), uutils_args::Error> { self.foo = true; + Ok(()) } } @@ -128,8 +132,9 @@ fn long_alias() { } impl Options for Settings { - fn apply(&mut self, Arg::Foo: Arg) { + fn apply(&mut self, Arg::Foo: Arg) -> Result<(), uutils_args::Error> { self.foo = true; + Ok(()) } } @@ -153,11 +158,12 @@ fn short_and_long_alias() { } impl Options for Settings { - fn apply(&mut self, arg: Arg) { + fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> { match arg { Arg::Foo => self.foo = true, Arg::Bar => self.bar = true, } + Ok(()) } } @@ -209,7 +215,7 @@ fn xyz_map_to_abc() { } impl Options for Settings { - fn apply(&mut self, arg: Arg) { + fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> { match arg { Arg::X => { self.a = true; @@ -225,6 +231,7 @@ fn xyz_map_to_abc() { self.c = true; } } + Ok(()) } } @@ -282,11 +289,12 @@ fn non_rust_ident() { } impl Options for Settings { - fn apply(&mut self, arg: Arg) { + fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> { match arg { Arg::FooBar => self.a = true, Arg::Super => self.b = true, } + Ok(()) } } @@ -312,8 +320,9 @@ fn number_flag() { } impl Options for Settings { - fn apply(&mut self, Arg::One: Arg) { + fn apply(&mut self, Arg::One: Arg) -> Result<(), uutils_args::Error> { self.one = true; + Ok(()) } } @@ -336,11 +345,12 @@ fn false_bool() { } impl Options for Settings { - fn apply(&mut self, arg: Arg) { + fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> { self.foo = match arg { Arg::A => true, Arg::B => false, - } + }; + Ok(()) } } @@ -378,8 +388,9 @@ fn verbosity() { } impl Options for Settings { - fn apply(&mut self, Arg::Verbosity: Arg) { + fn apply(&mut self, Arg::Verbosity: Arg) -> Result<(), uutils_args::Error> { self.verbosity += 1; + Ok(()) } } @@ -429,12 +440,13 @@ fn infer_long_args() { } impl Options for Settings { - fn apply(&mut self, arg: Arg) { + fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> { match arg { Arg::All => self.all = true, Arg::AlmostAll => self.almost_all = true, Arg::Author => self.author = true, } + Ok(()) } } @@ -482,12 +494,13 @@ fn enum_flag() { } impl Options for Settings { - fn apply(&mut self, arg: Arg) { + fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> { self.foo = match arg { Arg::Foo => SomeEnum::Foo, Arg::Bar => SomeEnum::Bar, Arg::Baz => SomeEnum::Baz, }; + Ok(()) } } diff --git a/tests/options.rs b/tests/options.rs index 1f1e972..66053f8 100644 --- a/tests/options.rs +++ b/tests/options.rs @@ -16,8 +16,9 @@ fn string_option() { } impl Options for Settings { - fn apply(&mut self, Arg::Message(s): Arg) { - self.message = s + fn apply(&mut self, Arg::Message(s): Arg) -> Result<(), uutils_args::Error> { + self.message = s; + Ok(()) } } @@ -56,8 +57,9 @@ fn enum_option() { } impl Options for Settings { - fn apply(&mut self, Arg::Format(f): Arg) { + fn apply(&mut self, Arg::Format(f): Arg) -> Result<(), uutils_args::Error> { self.format = f; + Ok(()) } } @@ -103,8 +105,9 @@ fn enum_option_with_fields() { } impl Options for Settings { - fn apply(&mut self, Arg::Indent(i): Arg) { + fn apply(&mut self, Arg::Indent(i): Arg) -> Result<(), uutils_args::Error> { self.indent = i; + Ok(()) } } @@ -160,8 +163,9 @@ fn enum_with_complex_from_value() { } impl Options for Settings { - fn apply(&mut self, Arg::Indent(i): Arg) { + fn apply(&mut self, Arg::Indent(i): Arg) -> Result<(), uutils_args::Error> { self.indent = i; + Ok(()) } } @@ -208,8 +212,9 @@ fn color() { } impl Options for Settings { - fn apply(&mut self, Arg::Color(c): Arg) { + fn apply(&mut self, Arg::Color(c): Arg) -> Result<(), uutils_args::Error> { self.color = c.unwrap_or(Color::Always); + Ok(()) } } @@ -283,7 +288,7 @@ fn actions() { } impl Options for Settings { - fn apply(&mut self, arg: Arg) { + fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> { match arg { Arg::Message(m) => { self.last_message = m.clone(); @@ -292,6 +297,7 @@ fn actions() { Arg::Send => self.send = true, Arg::Receive => self.send = false, } + Ok(()) } } @@ -317,11 +323,12 @@ fn width() { } impl Options for Settings { - fn apply(&mut self, Arg::Width(w): Arg) { + fn apply(&mut self, Arg::Width(w): Arg) -> Result<(), uutils_args::Error> { self.width = match w { 0 => None, x => Some(x), - } + }; + Ok(()) } } @@ -367,7 +374,7 @@ fn integers() { } impl Options for Settings { - fn apply(&mut self, arg: Arg) { + fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> { self.n = match arg { Arg::U8(x) => x as i128, Arg::U16(x) => x as i128, @@ -379,7 +386,8 @@ fn integers() { Arg::I32(x) => x as i128, Arg::I64(x) => x as i128, Arg::I128(x) => x, - } + }; + Ok(()) } } @@ -454,8 +462,9 @@ fn ls_classify() { } impl Options for Settings { - fn apply(&mut self, Arg::Classify(c): Arg) { + fn apply(&mut self, Arg::Classify(c): Arg) -> Result<(), uutils_args::Error> { self.classify = c; + Ok(()) } } @@ -507,8 +516,9 @@ fn mktemp_tmpdir() { } impl Options for Settings { - fn apply(&mut self, Arg::TmpDir(dir): Arg) { + fn apply(&mut self, Arg::TmpDir(dir): Arg) -> Result<(), uutils_args::Error> { self.tmpdir = Some(dir); + Ok(()) } } @@ -581,11 +591,12 @@ fn deprecated() { } impl Options for Settings { - fn apply(&mut self, arg: Arg) { + fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> { match arg { Arg::Min(n) => self.n1 = n, Arg::Plus(n) => self.n2 = n, } + Ok(()) } } @@ -620,10 +631,11 @@ fn empty_value() { struct Settings {} impl Options for Settings { - fn apply(&mut self, arg: Arg) { + fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> { match arg { Arg::Val(_) => {} } + Ok(()) } } } diff --git a/tests/options_first.rs b/tests/options_first.rs index ee9bc73..c7fa0e7 100644 --- a/tests/options_first.rs +++ b/tests/options_first.rs @@ -18,10 +18,11 @@ fn timeout_like() { } impl Options for Settings { - fn apply(&mut self, arg: Arg) { + fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> { match arg { Arg::Verbose => self.verbose = true, } + Ok(()) } }