Skip to content

Commit 17d9eaa

Browse files
committed
fix(lex)!: Rename ErrorContext to LexError
The name was too general when used outside of its original context and we are using `Context` for other things now.
1 parent 621fe9e commit 17d9eaa

7 files changed

Lines changed: 65 additions & 72 deletions

File tree

crates/lexarg-error/examples/hello-error.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use lexarg_error::ErrorContext;
1+
use lexarg_error::LexError;
22

33
struct Args {
44
thing: String,
@@ -23,21 +23,21 @@ fn parse_args() -> Result<Args, String> {
2323
match arg {
2424
Short("n") | Long("number") => {
2525
let value = parser.next_flag_value().ok_or_else(|| {
26-
ErrorContext::msg("missing required value")
26+
LexError::msg("missing required value")
2727
.within(arg)
2828
.to_string()
2929
})?;
3030
number = value
3131
.to_str()
3232
.ok_or_else(|| {
33-
ErrorContext::msg("invalid number")
33+
LexError::msg("invalid number")
3434
.unexpected(Value(value))
3535
.within(arg)
3636
.to_string()
3737
})?
3838
.parse()
3939
.map_err(|e| {
40-
ErrorContext::msg(e)
40+
LexError::msg(e)
4141
.unexpected(Value(value))
4242
.within(arg)
4343
.to_string()
@@ -47,18 +47,17 @@ fn parse_args() -> Result<Args, String> {
4747
shout = true;
4848
}
4949
Value(val) if thing.is_none() => {
50-
thing = Some(val.to_str().ok_or_else(|| {
51-
ErrorContext::msg("invalid string")
52-
.unexpected(arg)
53-
.to_string()
54-
})?);
50+
thing =
51+
Some(val.to_str().ok_or_else(|| {
52+
LexError::msg("invalid string").unexpected(arg).to_string()
53+
})?);
5554
}
5655
Short("h") | Long("help") => {
5756
println!("Usage: hello [-n|--number=NUM] [--shout] THING");
5857
std::process::exit(0);
5958
}
6059
_ => {
61-
return Err(ErrorContext::msg("unexpected argument")
60+
return Err(LexError::msg("unexpected argument")
6261
.unexpected(arg)
6362
.to_string());
6463
}
@@ -68,7 +67,7 @@ fn parse_args() -> Result<Args, String> {
6867
Ok(Args {
6968
thing: thing
7069
.ok_or_else(|| {
71-
ErrorContext::msg("missing argument THING")
70+
LexError::msg("missing argument THING")
7271
.within(Value(bin_name))
7372
.to_string()
7473
})?

crates/lexarg-error/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717

1818
/// Collect context for creating an error
1919
#[derive(Debug)]
20-
pub struct ErrorContext<'a> {
20+
pub struct LexError<'a> {
2121
msg: String,
2222
within: Option<lexarg_parser::Arg<'a>>,
2323
unexpected: Option<lexarg_parser::Arg<'a>>,
2424
}
2525

26-
impl<'a> ErrorContext<'a> {
26+
impl<'a> LexError<'a> {
2727
/// Create a new error object from a printable error message.
2828
#[cold]
2929
pub fn msg<M>(message: M) -> Self
@@ -52,7 +52,7 @@ impl<'a> ErrorContext<'a> {
5252
}
5353
}
5454

55-
impl<E> From<E> for ErrorContext<'_>
55+
impl<E> From<E> for LexError<'_>
5656
where
5757
E: std::error::Error + Send + Sync + 'static,
5858
{
@@ -62,7 +62,7 @@ where
6262
}
6363
}
6464

65-
impl std::fmt::Display for ErrorContext<'_> {
65+
impl std::fmt::Display for LexError<'_> {
6666
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
6767
self.msg.fmt(formatter)?;
6868
if let Some(unexpected) = &self.unexpected {

crates/lexarg/examples/hello.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use lexarg::ErrorContext;
1+
use lexarg::LexError;
22
use lexarg::Result;
33

44
struct Args {
@@ -40,15 +40,13 @@ fn parse_args() -> Result<Args> {
4040
std::process::exit(0);
4141
}
4242
Unexpected(_) => {
43-
return Err(ErrorContext::msg("unexpected value")
43+
return Err(LexError::msg("unexpected value")
4444
.unexpected(arg)
4545
.within(prev_arg)
4646
.into());
4747
}
4848
_ => {
49-
return Err(ErrorContext::msg("unexpected argument")
50-
.unexpected(arg)
51-
.into());
49+
return Err(LexError::msg("unexpected argument").unexpected(arg).into());
5250
}
5351
}
5452
prev_arg = arg;

crates/lexarg/src/lib.rs

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919
/// Simplify parsing of arguments
2020
pub mod prelude {
2121
pub use crate::Arg::*;
22-
pub use crate::OptionContextExt as _;
23-
pub use crate::ResultContextExt as _;
22+
pub use crate::OptionLexErrorExt as _;
23+
pub use crate::ResultLexErrorExt as _;
2424
pub use crate::ValueExt as _;
2525
}
2626

27-
pub use lexarg_error::ErrorContext;
27+
pub use lexarg_error::LexError;
2828
pub use lexarg_parser::Arg;
2929
pub use lexarg_parser::Parser;
3030
pub use lexarg_parser::RawArgs;
@@ -50,9 +50,9 @@ impl Error {
5050
}
5151
}
5252

53-
impl From<ErrorContext<'_>> for Error {
53+
impl From<LexError<'_>> for Error {
5454
#[cold]
55-
fn from(error: ErrorContext<'_>) -> Self {
55+
fn from(error: LexError<'_>) -> Self {
5656
Self::msg(error.to_string())
5757
}
5858
}
@@ -72,32 +72,32 @@ impl std::fmt::Display for Error {
7272
/// Extensions for parsing [`Arg::Value`]
7373
pub trait ValueExt<'a> {
7474
/// Convert [`Arg::Value`]
75-
fn path(self) -> Result<&'a std::path::Path, ErrorContext<'a>>;
75+
fn path(self) -> Result<&'a std::path::Path, LexError<'a>>;
7676
/// Convert [`Arg::Value`] with a description of the intended format
77-
fn string(self, description: &str) -> Result<&'a str, ErrorContext<'a>>;
77+
fn string(self, description: &str) -> Result<&'a str, LexError<'a>>;
7878
/// Ensure [`Arg::Value`] is from a closed set of values
79-
fn one_of(self, possible: &[&str]) -> Result<&'a str, ErrorContext<'a>>;
79+
fn one_of(self, possible: &[&str]) -> Result<&'a str, LexError<'a>>;
8080
/// Parse [`Arg::Value`]
81-
fn parse<T: std::str::FromStr>(self) -> Result<T, ErrorContext<'a>>
81+
fn parse<T: std::str::FromStr>(self) -> Result<T, LexError<'a>>
8282
where
8383
T::Err: std::fmt::Display;
8484
/// Custom conversion for [`Arg::Value`]
85-
fn try_map<F, T, E>(self, op: F) -> Result<T, ErrorContext<'a>>
85+
fn try_map<F, T, E>(self, op: F) -> Result<T, LexError<'a>>
8686
where
8787
F: FnOnce(&'a std::ffi::OsStr) -> Result<T, E>,
8888
E: std::fmt::Display;
8989
}
9090

9191
impl<'a> ValueExt<'a> for &'a std::ffi::OsStr {
92-
fn path(self) -> Result<&'a std::path::Path, ErrorContext<'a>> {
92+
fn path(self) -> Result<&'a std::path::Path, LexError<'a>> {
9393
Ok(std::path::Path::new(self))
9494
}
95-
fn string(self, description: &str) -> Result<&'a str, ErrorContext<'a>> {
95+
fn string(self, description: &str) -> Result<&'a str, LexError<'a>> {
9696
self.to_str().ok_or_else(|| {
97-
ErrorContext::msg(format_args!("invalid {description}")).unexpected(Arg::Value(self))
97+
LexError::msg(format_args!("invalid {description}")).unexpected(Arg::Value(self))
9898
})
9999
}
100-
fn one_of(self, possible: &[&str]) -> Result<&'a str, ErrorContext<'a>> {
100+
fn one_of(self, possible: &[&str]) -> Result<&'a str, LexError<'a>> {
101101
self.to_str()
102102
.filter(|v| possible.contains(v))
103103
.ok_or_else(|| {
@@ -108,43 +108,43 @@ impl<'a> ValueExt<'a> for &'a std::ffi::OsStr {
108108
use std::fmt::Write as _;
109109
let _ = write!(&mut error, ", `{possible}`");
110110
}
111-
ErrorContext::msg(error)
111+
LexError::msg(error)
112112
})
113113
}
114-
fn parse<T: std::str::FromStr>(self) -> Result<T, ErrorContext<'a>>
114+
fn parse<T: std::str::FromStr>(self) -> Result<T, LexError<'a>>
115115
where
116116
T::Err: std::fmt::Display,
117117
{
118118
self.string(std::any::type_name::<T>())?
119119
.parse::<T>()
120-
.map_err(|err| ErrorContext::msg(err).unexpected(Arg::Value(self)))
120+
.map_err(|err| LexError::msg(err).unexpected(Arg::Value(self)))
121121
}
122-
fn try_map<F, T, E>(self, op: F) -> Result<T, ErrorContext<'a>>
122+
fn try_map<F, T, E>(self, op: F) -> Result<T, LexError<'a>>
123123
where
124124
F: FnOnce(&'a std::ffi::OsStr) -> Result<T, E>,
125125
E: std::fmt::Display,
126126
{
127-
op(self).map_err(|err| ErrorContext::msg(err).unexpected(Arg::Value(self)))
127+
op(self).map_err(|err| LexError::msg(err).unexpected(Arg::Value(self)))
128128
}
129129
}
130130

131-
impl<'a> ValueExt<'a> for Result<&'a std::ffi::OsStr, ErrorContext<'a>> {
132-
fn path(self) -> Result<&'a std::path::Path, ErrorContext<'a>> {
131+
impl<'a> ValueExt<'a> for Result<&'a std::ffi::OsStr, LexError<'a>> {
132+
fn path(self) -> Result<&'a std::path::Path, LexError<'a>> {
133133
self.and_then(|os| os.path())
134134
}
135-
fn string(self, description: &str) -> Result<&'a str, ErrorContext<'a>> {
135+
fn string(self, description: &str) -> Result<&'a str, LexError<'a>> {
136136
self.and_then(|os| os.string(description))
137137
}
138-
fn one_of(self, possible: &[&str]) -> Result<&'a str, ErrorContext<'a>> {
138+
fn one_of(self, possible: &[&str]) -> Result<&'a str, LexError<'a>> {
139139
self.and_then(|os| os.one_of(possible))
140140
}
141-
fn parse<T: std::str::FromStr>(self) -> Result<T, ErrorContext<'a>>
141+
fn parse<T: std::str::FromStr>(self) -> Result<T, LexError<'a>>
142142
where
143143
T::Err: std::fmt::Display,
144144
{
145145
self.and_then(|os| os.parse())
146146
}
147-
fn try_map<F, T, E>(self, op: F) -> Result<T, ErrorContext<'a>>
147+
fn try_map<F, T, E>(self, op: F) -> Result<T, LexError<'a>>
148148
where
149149
F: FnOnce(&'a std::ffi::OsStr) -> Result<T, E>,
150150
E: std::fmt::Display,
@@ -153,33 +153,33 @@ impl<'a> ValueExt<'a> for Result<&'a std::ffi::OsStr, ErrorContext<'a>> {
153153
}
154154
}
155155

156-
/// Extensions for extending [`ErrorContext`]
157-
pub trait ResultContextExt<'a> {
156+
/// Extensions for extending [`LexError`]
157+
pub trait ResultLexErrorExt<'a> {
158158
/// [`Arg`] the error occurred within
159159
fn within(self, within: Arg<'a>) -> Self;
160160
}
161161

162-
impl<'a, T> ResultContextExt<'a> for Result<T, ErrorContext<'a>> {
162+
impl<'a, T> ResultLexErrorExt<'a> for Result<T, LexError<'a>> {
163163
fn within(self, within: Arg<'a>) -> Self {
164164
self.map_err(|err| err.within(within))
165165
}
166166
}
167167

168-
/// Extensions for creating an [`ErrorContext`]
169-
pub trait OptionContextExt<T> {
168+
/// Extensions for creating an [`LexError`]
169+
pub trait OptionLexErrorExt<T> {
170170
/// [`Arg`] that was expected
171171
///
172172
/// For [`Arg::Value`], the contents are assumed to be a placeholder
173-
fn ok_or_missing(self, expected: Arg<'static>) -> Result<T, ErrorContext<'static>>;
173+
fn ok_or_missing(self, expected: Arg<'static>) -> Result<T, LexError<'static>>;
174174
}
175175

176-
impl<T> OptionContextExt<T> for Option<T> {
177-
fn ok_or_missing(self, expected: Arg<'static>) -> Result<T, ErrorContext<'static>> {
176+
impl<T> OptionLexErrorExt<T> for Option<T> {
177+
fn ok_or_missing(self, expected: Arg<'static>) -> Result<T, LexError<'static>> {
178178
self.ok_or_else(|| match expected {
179-
Arg::Short(short) => ErrorContext::msg(format_args!("missing required `-{short}`")),
180-
Arg::Long(long) => ErrorContext::msg(format_args!("missing required `--{long}`")),
181-
Arg::Escape(escape) => ErrorContext::msg(format_args!("missing required `{escape}`")),
182-
Arg::Value(value) | Arg::Unexpected(value) => ErrorContext::msg(format_args!(
179+
Arg::Short(short) => LexError::msg(format_args!("missing required `-{short}`")),
180+
Arg::Long(long) => LexError::msg(format_args!("missing required `--{long}`")),
181+
Arg::Escape(escape) => LexError::msg(format_args!("missing required `{escape}`")),
182+
Arg::Value(value) | Arg::Unexpected(value) => LexError::msg(format_args!(
183183
"missing required `{}`",
184184
value.to_string_lossy()
185185
)),

crates/libtest-lexarg/examples/libtest-cli.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ fn main() -> lexarg::Result<()> {
3131
continue;
3232
}
3333
Unexpected(_) => {
34-
return Err(lexarg::ErrorContext::msg("unexpected value")
34+
return Err(lexarg::LexError::msg("unexpected value")
3535
.unexpected(arg)
3636
.within(prev_arg)
3737
.into());
@@ -43,7 +43,7 @@ fn main() -> lexarg::Result<()> {
4343
let arg = test_opts.parse_next(&mut parser, arg)?;
4444

4545
if let Some(arg) = arg {
46-
return Err(lexarg::ErrorContext::msg("unexpected argument")
46+
return Err(lexarg::LexError::msg("unexpected argument")
4747
.unexpected(arg)
4848
.into());
4949
}

crates/libtest-lexarg/src/lib.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#![warn(missing_debug_implementations, elided_lifetimes_in_paths)]
1515

1616
use lexarg::Arg;
17-
use lexarg_error::ErrorContext;
17+
use lexarg_error::LexError;
1818

1919
/// Parsed command-line options
2020
///
@@ -174,7 +174,7 @@ impl TestOptsBuilder {
174174
&mut self,
175175
parser: &mut lexarg::Parser<'a>,
176176
arg: Arg<'a>,
177-
) -> Result<Option<Arg<'a>>, ErrorContext<'a>> {
177+
) -> Result<Option<Arg<'a>>, LexError<'a>> {
178178
use lexarg::prelude::*;
179179

180180
match arg {
@@ -257,7 +257,7 @@ impl TestOptsBuilder {
257257
.string("FEATURE")
258258
.within(arg)?;
259259
if !is_nightly() {
260-
return Err(ErrorContext::msg("expected nightly compiler").unexpected(arg));
260+
return Err(LexError::msg("expected nightly compiler").unexpected(arg));
261261
}
262262
// Don't validate `feature` as other parsers might provide values
263263
self.opts.allowed_unstable.push(feature.to_owned());
@@ -274,17 +274,15 @@ impl TestOptsBuilder {
274274
}
275275

276276
/// Finish parsing, resolving to [`TestOpts`]
277-
pub fn finish(mut self) -> Result<TestOpts, ErrorContext<'static>> {
277+
pub fn finish(mut self) -> Result<TestOpts, LexError<'static>> {
278278
let allow_unstable_options = self
279279
.opts
280280
.allowed_unstable
281281
.iter()
282282
.any(|f| f == UNSTABLE_OPTIONS);
283283

284284
if self.format.is_some() && !allow_unstable_options {
285-
return Err(ErrorContext::msg(
286-
"`--format` requires `-Zunstable-options`",
287-
));
285+
return Err(LexError::msg("`--format` requires `-Zunstable-options`"));
288286
}
289287
if let Some(format) = self.format {
290288
self.opts.format = format;
@@ -296,7 +294,7 @@ impl TestOptsBuilder {
296294

297295
self.opts.run_ignored = match (self.include_ignored, self.ignored) {
298296
(true, true) => {
299-
return Err(ErrorContext::msg(
297+
return Err(LexError::msg(
300298
"`--include-ignored` and `--ignored` are mutually exclusive",
301299
))
302300
}

crates/libtest2-harness/src/harness.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,7 @@ impl Harness {
8181

8282
const ERROR_EXIT_CODE: i32 = 101;
8383

84-
fn parse<'p>(
85-
parser: &mut cli::Parser<'p>,
86-
) -> Result<libtest_lexarg::TestOpts, cli::ErrorContext<'p>> {
84+
fn parse<'p>(parser: &mut cli::Parser<'p>) -> Result<libtest_lexarg::TestOpts, cli::LexError<'p>> {
8785
let mut test_opts = libtest_lexarg::TestOptsBuilder::new();
8886

8987
let bin = parser
@@ -112,7 +110,7 @@ fn parse<'p>(
112110
continue;
113111
}
114112
cli::Arg::Unexpected(_) => {
115-
return Err(cli::ErrorContext::msg("unexpected value")
113+
return Err(cli::LexError::msg("unexpected value")
116114
.unexpected(arg)
117115
.within(prev_arg));
118116
}
@@ -123,7 +121,7 @@ fn parse<'p>(
123121
let arg = test_opts.parse_next(parser, arg)?;
124122

125123
if let Some(arg) = arg {
126-
return Err(cli::ErrorContext::msg("unexpected argument").unexpected(arg));
124+
return Err(cli::LexError::msg("unexpected argument").unexpected(arg));
127125
}
128126
}
129127

0 commit comments

Comments
 (0)