LexError

Struct LexError 

Source
pub struct LexError<'a> { /* private fields */ }
Expand description

Collect context for creating an error

Implementations§

Source§

impl<'a> LexError<'a>

Source

pub fn msg<M>(message: M) -> Self
where M: Display,

Create a new error object from a printable error message.

Examples found in repository?
examples/hello-error.rs (line 26)
9fn parse_args() -> Result<Args, String> {
10    #![allow(clippy::enum_glob_use)]
11    use lexarg_parser::Arg::*;
12
13    let mut thing = None;
14    let mut number = 1;
15    let mut shout = false;
16    let raw = std::env::args_os().collect::<Vec<_>>();
17    let mut parser = lexarg_parser::Parser::new(&raw);
18    let bin_name = parser
19        .next_raw()
20        .expect("nothing parsed yet so no attached lingering")
21        .expect("always at least one");
22    while let Some(arg) = parser.next_arg() {
23        match arg {
24            Short("n") | Long("number") => {
25                let value = parser.next_flag_value().ok_or_else(|| {
26                    LexError::msg("missing required value")
27                        .within(arg)
28                        .to_string()
29                })?;
30                number = value
31                    .to_str()
32                    .ok_or_else(|| {
33                        LexError::msg("invalid number")
34                            .unexpected(Value(value))
35                            .within(arg)
36                            .to_string()
37                    })?
38                    .parse()
39                    .map_err(|e| {
40                        LexError::msg(e)
41                            .unexpected(Value(value))
42                            .within(arg)
43                            .to_string()
44                    })?;
45            }
46            Long("shout") => {
47                shout = true;
48            }
49            Value(val) if thing.is_none() => {
50                thing =
51                    Some(val.to_str().ok_or_else(|| {
52                        LexError::msg("invalid string").unexpected(arg).to_string()
53                    })?);
54            }
55            Short("h") | Long("help") => {
56                println!("Usage: hello [-n|--number=NUM] [--shout] THING");
57                std::process::exit(0);
58            }
59            _ => {
60                return Err(LexError::msg("unexpected argument")
61                    .unexpected(arg)
62                    .to_string());
63            }
64        }
65    }
66
67    Ok(Args {
68        thing: thing
69            .ok_or_else(|| {
70                LexError::msg("missing argument THING")
71                    .within(Value(bin_name))
72                    .to_string()
73            })?
74            .to_owned(),
75        number,
76        shout,
77    })
78}
Source

pub fn within(self, within: Arg<'a>) -> Self

Arg the error occurred within

Examples found in repository?
examples/hello-error.rs (line 27)
9fn parse_args() -> Result<Args, String> {
10    #![allow(clippy::enum_glob_use)]
11    use lexarg_parser::Arg::*;
12
13    let mut thing = None;
14    let mut number = 1;
15    let mut shout = false;
16    let raw = std::env::args_os().collect::<Vec<_>>();
17    let mut parser = lexarg_parser::Parser::new(&raw);
18    let bin_name = parser
19        .next_raw()
20        .expect("nothing parsed yet so no attached lingering")
21        .expect("always at least one");
22    while let Some(arg) = parser.next_arg() {
23        match arg {
24            Short("n") | Long("number") => {
25                let value = parser.next_flag_value().ok_or_else(|| {
26                    LexError::msg("missing required value")
27                        .within(arg)
28                        .to_string()
29                })?;
30                number = value
31                    .to_str()
32                    .ok_or_else(|| {
33                        LexError::msg("invalid number")
34                            .unexpected(Value(value))
35                            .within(arg)
36                            .to_string()
37                    })?
38                    .parse()
39                    .map_err(|e| {
40                        LexError::msg(e)
41                            .unexpected(Value(value))
42                            .within(arg)
43                            .to_string()
44                    })?;
45            }
46            Long("shout") => {
47                shout = true;
48            }
49            Value(val) if thing.is_none() => {
50                thing =
51                    Some(val.to_str().ok_or_else(|| {
52                        LexError::msg("invalid string").unexpected(arg).to_string()
53                    })?);
54            }
55            Short("h") | Long("help") => {
56                println!("Usage: hello [-n|--number=NUM] [--shout] THING");
57                std::process::exit(0);
58            }
59            _ => {
60                return Err(LexError::msg("unexpected argument")
61                    .unexpected(arg)
62                    .to_string());
63            }
64        }
65    }
66
67    Ok(Args {
68        thing: thing
69            .ok_or_else(|| {
70                LexError::msg("missing argument THING")
71                    .within(Value(bin_name))
72                    .to_string()
73            })?
74            .to_owned(),
75        number,
76        shout,
77    })
78}
Source

pub fn unexpected(self, unexpected: Arg<'a>) -> Self

The failing Arg

Examples found in repository?
examples/hello-error.rs (line 34)
9fn parse_args() -> Result<Args, String> {
10    #![allow(clippy::enum_glob_use)]
11    use lexarg_parser::Arg::*;
12
13    let mut thing = None;
14    let mut number = 1;
15    let mut shout = false;
16    let raw = std::env::args_os().collect::<Vec<_>>();
17    let mut parser = lexarg_parser::Parser::new(&raw);
18    let bin_name = parser
19        .next_raw()
20        .expect("nothing parsed yet so no attached lingering")
21        .expect("always at least one");
22    while let Some(arg) = parser.next_arg() {
23        match arg {
24            Short("n") | Long("number") => {
25                let value = parser.next_flag_value().ok_or_else(|| {
26                    LexError::msg("missing required value")
27                        .within(arg)
28                        .to_string()
29                })?;
30                number = value
31                    .to_str()
32                    .ok_or_else(|| {
33                        LexError::msg("invalid number")
34                            .unexpected(Value(value))
35                            .within(arg)
36                            .to_string()
37                    })?
38                    .parse()
39                    .map_err(|e| {
40                        LexError::msg(e)
41                            .unexpected(Value(value))
42                            .within(arg)
43                            .to_string()
44                    })?;
45            }
46            Long("shout") => {
47                shout = true;
48            }
49            Value(val) if thing.is_none() => {
50                thing =
51                    Some(val.to_str().ok_or_else(|| {
52                        LexError::msg("invalid string").unexpected(arg).to_string()
53                    })?);
54            }
55            Short("h") | Long("help") => {
56                println!("Usage: hello [-n|--number=NUM] [--shout] THING");
57                std::process::exit(0);
58            }
59            _ => {
60                return Err(LexError::msg("unexpected argument")
61                    .unexpected(arg)
62                    .to_string());
63            }
64        }
65    }
66
67    Ok(Args {
68        thing: thing
69            .ok_or_else(|| {
70                LexError::msg("missing argument THING")
71                    .within(Value(bin_name))
72                    .to_string()
73            })?
74            .to_owned(),
75        number,
76        shout,
77    })
78}

Trait Implementations§

Source§

impl<'a> Debug for LexError<'a>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Display for LexError<'_>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<E> From<E> for LexError<'_>
where E: Error + Send + Sync + 'static,

Source§

fn from(error: E) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<'a> Freeze for LexError<'a>

§

impl<'a> RefUnwindSafe for LexError<'a>

§

impl<'a> Send for LexError<'a>

§

impl<'a> Sync for LexError<'a>

§

impl<'a> Unpin for LexError<'a>

§

impl<'a> UnwindSafe for LexError<'a>

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<!> for T

Source§

fn from(t: !) -> T

Converts to this type from the input type.
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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

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>,

Source§

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.