rags_rs/
errors.rs

1use std::error::Error as ErrorImpl;
2
3use crate::printer::arg_string;
4
5pub enum Error {
6    InvalidState(&'static str),
7    InvalidInput(char, &'static str, &'static str),
8    MissingArgValue(char, &'static str),
9    ConstructionError(char, &'static str, String), // TODO: would be nice to keep the typed-error
10    PositionalConstructionError(&'static str, String), // TODO: would be nice to keep the original
11    SubConstructionError(&'static str, String), // TODO: would be nice to keep the typed-error
12    ValuedArgInRun(char, String), // offending short, run it was contained in
13
14    NestedGroup(&'static str, &'static str), // existing, attempted
15    PrinterMissingGroup(&'static str),
16
17    MissingArgument(String),
18    MissingPositional(String),
19    MultipleVariadic(&'static str),
20    UnorderedPositionals(&'static str),
21}
22
23impl std::error::Error for Error {
24    fn description(&self) -> &str {
25        match self {
26            Error::InvalidState(_) => {
27                "invalid parser state"
28            }
29            Error::InvalidInput(_, _, _) => {
30                "invalid input"
31            }
32            Error::MissingArgValue(_, _) => {
33                "missing argument value"
34            }
35            Error::ConstructionError(_, _, _) => {
36                "failed to construct target from string"
37            }
38            Error::PositionalConstructionError(_, _) => {
39                "failed to construct positional target from string"
40            }
41            Error::SubConstructionError(_, _) => {
42                "failed to construct subcommand from string"
43            }
44            Error::ValuedArgInRun(_, _) => {
45                "short-code runs only support valued-args as the last character in the run"
46            }
47
48            Error::NestedGroup(_, _) => {
49                "groups cannot be nested"
50            }
51            Error::PrinterMissingGroup(_) => {
52                "cannot add option to unknown group"
53            }
54
55
56            Error::MissingArgument(_) => {
57                "required argument was not given"
58            }
59            Error::MissingPositional(_) => {
60                "required positional was not given"
61            }
62            Error::MultipleVariadic(_) => {
63                "second declared variadic positional has no effect"
64            }
65            Error::UnorderedPositionals(_) => {
66                "declaring a positional after a variadic positional has no effect"
67            }
68        }
69    }
70}
71
72impl std::fmt::Display for Error {
73    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
74        match self {
75            Error::InvalidState(desc) => {
76                write!(f, "{}: {}", self.description(), desc)
77            }
78            Error::InvalidInput(short, long, desc) => {
79                write!(f, "{}: {} {}", self.description(), arg_string(*short, long, false), desc)
80            }
81            Error::MissingArgValue(short, long) => {
82                write!(f, "{} for {}", self.description(), arg_string(*short, long, false))
83            }
84            Error::ConstructionError(short, long, err) => {
85                write!(f, "{} for {}: {}", self.description(),
86                    arg_string(*short, long, false), err)
87            }
88            Error::PositionalConstructionError(name, err) => {
89                write!(f, "{} for {}: {}", self.description(), name, err)
90            }
91            Error::SubConstructionError(name, err) => {
92                write!(f, "{} for {}: {}", self.description(), name, err)
93            }
94            Error::ValuedArgInRun(short, run) => {
95                write!(f, "{}: {} is within {}", self.description(), short, run)
96            }
97
98
99            Error::NestedGroup(orig, attempt) => {
100                write!(f, "{} ({} within {})", self.description(), attempt, orig)
101            }
102            Error::PrinterMissingGroup(name) => {
103                write!(f, "{}: {}", self.description(), name)
104            }
105
106            Error::MissingArgument(a) => {
107                write!(f, "{}: {}", self.description(), a)
108            }
109            Error::MissingPositional(a) => {
110                write!(f, "{}: {}", self.description(), a)
111            }
112            Error::MultipleVariadic(p) => {
113                write!(f, "{}: {}", self.description(), p)
114            }
115            Error::UnorderedPositionals(p) => {
116                write!(f, "{}: {}", self.description(), p)
117            }
118        }
119    }
120}
121
122impl std::fmt::Debug for Error {
123    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
124        write!(f, "{}", self)
125    }
126}