Enum argwerk::ErrorKind

source ·
pub enum ErrorKind {
    UnsupportedArgument {
        argument: Box<str>,
    },
    UnsupportedSwitch {
        switch: Box<str>,
    },
    MissingSwitchArgument {
        switch: Box<str>,
        argument: &'static str,
    },
    MissingPositional {
        name: &'static str,
    },
    MissingRequired {
        name: &'static str,
        reason: Option<&'static str>,
    },
    InputError {
        error: InputError,
    },
    Error {
        name: Box<str>,
        error: Box<dyn Error + Send + Sync + 'static>,
    },
}
Expand description

The kind of an error.

Variants§

§

UnsupportedArgument

Encountered an argument that was not supported.

An unsupported argument is triggered when none of the branches in the parser matches the current agument.

§Examples

argwerk::define! {
    struct Args { }
    // This errors because `bar` is not a supported switch, nor do we
    // match any positional arguments.
    ["--file", arg] => {}
}

let error = Args::parse(vec!["bar"]).unwrap_err();

assert!(matches!(error.kind(), argwerk::ErrorKind::UnsupportedArgument { .. }));

Fields

§argument: Box<str>

The name of the unsupported argument.

§

UnsupportedSwitch

Encountered a switch that was not supported.

An unsupported switch is caused by the same reason as an unsupported argument, but it’s prefixed with a hyphen -.

§Examples

argwerk::define! {
    #[usage = "command [-h]"]
    struct Args { }
    // This errors because `--path` is not a supported switch. But
    // `"--file"` is.
    ["--file", arg] => {}
}

let error = Args::parse(vec!["--path"]).unwrap_err();

assert!(matches!(error.kind(), argwerk::ErrorKind::UnsupportedSwitch { .. }));

Fields

§switch: Box<str>

The name of the unsupported switch.

§

MissingSwitchArgument

When a parameter to an argument is missing.

§Examples

argwerk::define! {
    struct Args { }
    // This errors because `--file` requires an argument `path`, but
    // that is not provided.
    ["--file", path] => {}
}

let error = Args::parse(vec!["--file"]).unwrap_err();

assert!(matches!(error.kind(), argwerk::ErrorKind::MissingSwitchArgument { .. }));

Fields

§switch: Box<str>

The switch where the argument was missing, like --file in --file <path>.

§argument: &'static str

The argument that was missing, like path in --file <path>.

§

MissingPositional

When a positional argument is missing.

§Examples

argwerk::define! {
    struct Args { }
    // This errors because `b` is a required argument, but we only have
    // one which matches `a`.
    [a, b] => {}
}

let error = Args::parse(vec!["foo"]).unwrap_err();

assert!(matches!(error.kind(), argwerk::ErrorKind::MissingPositional { .. }));

Fields

§name: &'static str

The name of the argument missing like path in <path>.

§

MissingRequired

When a positional argument is missing.

§Examples

argwerk::define! {
    struct Args {
        #[required = "--name must be used"]
        name: String,
    }
    ["--name", n] => {
        name = Some(n);
    }
    [rest] => {}
}

let error = Args::parse(vec!["foo"]).unwrap_err();

assert!(matches!(error.kind(), argwerk::ErrorKind::MissingRequired { name: "name", .. }));

Fields

§name: &'static str

The name of the required variable that is missing.

§reason: Option<&'static str>

The reason that the required argument was missing.

§

InputError

Failed to parse input as unicode string.

This is raised in case argwerk needs to treat an input as a string, but that is not possible.

This is required if the string needs to be used in a switch branch.

Fields

§error: InputError

The underlying error.

§

Error

When an error has been raised while processing an argument, typically when something is being parsed.

§Examples

argwerk::define! {
    #[usage = "command [-h]"]
    struct Args { }
    // This errors because we raise an error in the branch body.
    ["foo"] => {
        Err("something went wrong")
    }
}

let error = Args::parse(vec!["foo"]).unwrap_err();

assert!(matches!(error.kind(), argwerk::ErrorKind::Error { .. }));

Fields

§name: Box<str>

The name of the switch or positional that couldn’t be processed.

§error: Box<dyn Error + Send + Sync + 'static>

The error that caused the parsing error.

Trait Implementations§

source§

impl Debug for ErrorKind

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.