Skip to content

Commit

Permalink
make Options::apply fallible
Browse files Browse the repository at this point in the history
  • Loading branch information
BenWiederhake committed Mar 29, 2024
1 parent c380e9d commit 48ba2d8
Show file tree
Hide file tree
Showing 23 changed files with 116 additions and 63 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Arg> 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(())
}
}

Expand Down
17 changes: 11 additions & 6 deletions docs/guide/port.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,11 @@ enum Arg {
struct Settings { a: bool }

impl Options<Arg> 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(())
}
}

Expand Down Expand Up @@ -137,10 +138,11 @@ impl Default for Settings {
}

impl Options<Arg> 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(())
}
}

Expand Down Expand Up @@ -175,10 +177,11 @@ enum Arg {
struct Settings { a: u8 }

impl Options<Arg> 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(())
}
}

Expand Down Expand Up @@ -215,10 +218,11 @@ enum Arg {
struct Settings { a: OsString }

impl Options<Arg> 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(())
}
}

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

impl Options<Arg> 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(())
}
}

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

</div>
</div>
20 changes: 13 additions & 7 deletions docs/guide/quick.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ struct Settings {
}

impl Options<Arg> 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(())
}
}

Expand Down Expand Up @@ -100,11 +101,12 @@ struct Settings {
}

impl Options<Arg> 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(())
}
}

Expand Down Expand Up @@ -160,10 +162,11 @@ enum Arg {
# }
#
# impl Options<Arg> 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(())
# }
# }
#
Expand Down Expand Up @@ -197,10 +200,11 @@ enum Arg {
# }
#
# impl Options<Arg> 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(())
# }
# }
#
Expand Down Expand Up @@ -234,10 +238,11 @@ enum Arg {
# }
#
# impl Options<Arg> 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(())
# }
# }
#
Expand Down Expand Up @@ -269,10 +274,11 @@ enum Arg {
# }
#
# impl Options<Arg> 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(())
# }
# }
#
Expand All @@ -287,4 +293,4 @@ enum Arg {
[Up](super)
[Next](next)

</div>
</div>
2 changes: 1 addition & 1 deletion examples/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ enum Arg {
struct Settings;

impl Options<Arg> 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!")
}
}
Expand Down
3 changes: 2 additions & 1 deletion examples/deprecated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ struct Settings {
}

impl Options<Arg> 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(())
}
}

Expand Down
3 changes: 2 additions & 1 deletion examples/hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ struct Settings {
}

impl Options<Arg> 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(())
}
}

Expand Down
3 changes: 2 additions & 1 deletion examples/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ struct Settings {
}

impl Options<Arg> 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(())
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ impl<T: Arguments> ArgumentIter<T> {
/// call [`Options::apply`] on the result until the arguments are exhausted.
pub trait Options<Arg: Arguments>: 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)]
Expand All @@ -191,7 +191,7 @@ pub trait Options<Arg: Arguments>: Sized {
{
let mut iter = ArgumentIter::<Arg>::from_args(args);
while let Some(arg) = iter.next_arg()? {
self.apply(arg);
self.apply(arg)?;
}
Ok((self, iter.positional_arguments))
}
Expand Down
3 changes: 2 additions & 1 deletion tests/coreutils/b2sum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ struct Settings {
}

impl Options<Arg> 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,
Expand All @@ -57,6 +57,7 @@ impl Options<Arg> for Settings {
Arg::Strict => self.strict = true,
Arg::Warn => self.check_output = CheckOutput::Warn,
}
Ok(())
}
}

Expand Down
3 changes: 2 additions & 1 deletion tests/coreutils/base32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ impl Default for Settings {
}

impl Options<Arg> 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(())
}
}

Expand Down
3 changes: 2 additions & 1 deletion tests/coreutils/basename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ struct Settings {
}

impl Options<Arg> 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) => {
Expand All @@ -35,6 +35,7 @@ impl Options<Arg> for Settings {
}
Arg::Zero => self.zero = true,
}
Ok(())
}
}

Expand Down
3 changes: 2 additions & 1 deletion tests/coreutils/cat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ struct Settings {
}

impl Options<Arg> 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;
Expand All @@ -70,6 +70,7 @@ impl Options<Arg> for Settings {
Arg::NumberNonblank => self.number = NumberingMode::NonEmpty,
Arg::SqueezeBlank => self.squeeze_blank = true,
}
Ok(())
}
}

Expand Down
3 changes: 2 additions & 1 deletion tests/coreutils/dd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl Default for Settings {
}

impl Options<Arg> 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),
Expand All @@ -111,6 +111,7 @@ impl Options<Arg> for Settings {
Arg::Iflag(_) => todo!(),
Arg::Oflag(_) => todo!(),
}
Ok(())
}
}

Expand Down
3 changes: 2 additions & 1 deletion tests/coreutils/echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ struct Settings {
}

impl Options<Arg> 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(())
}
}

Expand Down
3 changes: 2 additions & 1 deletion tests/coreutils/head.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ struct Settings {
}

impl Options<Arg> 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;
Expand All @@ -202,6 +202,7 @@ impl Options<Arg> for Settings {
Arg::Verbose => self.verbose = true,
Arg::Zero => self.zero = true,
}
Ok(())
}
}

Expand Down
3 changes: 2 additions & 1 deletion tests/coreutils/ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ impl Default for Settings {
}

impl Options<Arg> 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,
Expand Down Expand Up @@ -417,6 +417,7 @@ impl Options<Arg> for Settings {
}
Arg::GroupDirectoriesFirst => self.group_directories_first = true,
}
Ok(())
}
}

Expand Down
3 changes: 2 additions & 1 deletion tests/coreutils/mktemp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ struct Settings {
}

impl Options<Arg> 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,
Expand All @@ -49,6 +49,7 @@ impl Options<Arg> for Settings {
Arg::TreatAsTemplate => self.treat_as_template = true,
Arg::TmpDir(dir) => self.tmp_dir = Some(dir),
}
Ok(())
}
}

Expand Down
Loading

0 comments on commit 48ba2d8

Please sign in to comment.