mode selection process outlined in 'arg::parsing::into_mode'

arg-parsing-better
Avril 4 years ago
parent 172462b48e
commit deddddcdce
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -32,10 +32,43 @@ pub enum Argument
Input(String),
}
/// Kinds of modes of operation for the program.
///
/// These map to `super::Mode`.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd, Copy)]
#[non_exhaustive]
enum ModeKind
{
Normal,
Help
}
impl Default for ModeKind
{
#[inline]
fn default() -> Self
{
Self::Normal
}
}
impl Argument
{
/// What mode does this argument change to, if any?
fn mode_change_kind(&self) -> Option<ModeKind>
{
Some(match self
{
Self::ModeChangeHelp => ModeKind::Help,
_ => return None,
})
}
/// Convert into a mode change if this is a mode change
// We consume the whole output here in case the mode change needs to traverse it for information. As of now, we have only one non-`Normal` mode: `Help`, which doesn't require any extra information.
#[deprecated(note = "mode selection happens in `into_mode` and early return through `Continue::Abort` will be removed or reworked to not require this function.")]
pub fn try_into_mode(self, _rest: Output) -> Result<Mode, (Self, Output)>
{
match self {
@ -233,6 +266,8 @@ pub enum Continue
///
/// Returning this when the contained value is `Some` immediately terminates parsing and precedes to mode-switch. However, if it is `None`, parsing of chained short args is allowed to continue, although `Abort(None)` will be returned at the end regardless of subsequent `Continue` results from that change (unless one is an `Abort(Some(_))`, which immediately returns itself.)
// Box `Argument` to reduce the size of `Continue`, as it is returned from functions often and when its value is set to `Some` it will always be the last `Argument` processed anyway and the only one to be boxed here at all.
//TODO: Deprecate the early return of an `Argument` here. Either change it to `Mode`, or have no early return. Mode change happens at the bottom in `into_mode` now.
Abort(Option<Box<Argument>>),
}
@ -416,9 +451,21 @@ mod modes {
/// Consume this parsed list of arguments into a `Mode` and return it
pub fn into_mode(args: Output) -> eyre::Result<Mode>
{
//TODO: Find out what mode `args` is in.
let mut mode_kind = ModeKind::default(); //Normal.
//TODO: pass `args` to the respective mode generation function in mode `modes`, and wrap that mode around its return value.
for arg in args.iter() {
//find any mode change Argument (with `Argument::mode_change_kind()`) in `args`, changing `mode_kind` in turn. There should be at most 1.
if let Some(mode) = arg.mode_change_kind()
{
mode_kind = mode;
break;
}
}
//pass `args` to the respective mode generation function in mode `modes`, and wrap that mode around its return value.
match mode_kind
{
ModeKind::Normal => modes::normal(args).map(Mode::Normal),
ModeKind::Help => Ok(Mode::Help),
}
todo!()
}

Loading…
Cancel
Save