From f918d5f6e16b100359052920422526a182073252 Mon Sep 17 00:00:00 2001 From: Avril Date: Sat, 13 Aug 2022 00:31:51 +0100 Subject: [PATCH] Prepared or replacing return type from directly normal operation `Options` to `enum Mode`, which will facilitate returning a `--help` or other special mode case. We can use `impl From for Mode` and work with this to make the migration of return types easier. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fortune for collect's current commit: Half blessing − 半吉 --- src/args.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/args.rs b/src/args.rs index 4c6ec2f..7acdddd 100644 --- a/src/args.rs +++ b/src/args.rs @@ -404,10 +404,13 @@ where I: IntoIterator, /*($parser:path => $then:expr) => { $then(try_parse_for!(try $parser => std::convert::identity)?) }*/ - } - + } + //TODO: Add `impl TryParse` struct for `--help` and add it at the *top* of the visitation stack (it will most likely appear there.) + // This may require a re-work of the `Options` struct, or an enum wrapper around it should be returned instead of options directly, for special modes (like `--help` is, etc.) Perhaps `pub enum Mode { Normal(Options), Help, }` or something should be returned, and `impl From` for it, with the caller of this closure (below) try_parse_for!(parsers::ExecMode => |result| output.exec.push(result)); - //TODO: try_parse_for!(parsers::SomeOtherOption => |result| output.some_other_option.set(result.something)), etc, for any newly added arguments. + + //Note: try_parse_for!(parsers::SomeOtherOption => |result| output.some_other_option.set(result.something)), etc, for any newly added arguments. + if_trace!(debug!("reached end of parser visitation for argument #{idx} {arg:?}! Failing now with `UnknownOption`")); return Err(ArgParseError::UnknownOption(arg)); } @@ -415,7 +418,7 @@ where I: IntoIterator, }; parser() .with_index(idx) - .map(move |_| output) + .map(move |_| output.into()) //XXX: This is `output.into()`, because when successful result return type is changed from directly `Options` to `enum Mode` (which will `impl From`), it will allow any `impl Into` to be returned. (Boxed dynamic dispatch with a trait `impl FromMode (for Mode) { fn from(val: Box) -> Self { IntoMode::into(val) } }, auto impl trait IntoMode { fn into(self: Box) -> Mode }` may be required if different types are returned from the closure, this is okay, as argument parsed struct can get rather large.) } #[derive(Debug)]