Skip to content

Commit e632428

Browse files
author
MayankRaj435
committed
refactor(parser): implement ErrorContext for Error, remove redundant methods
- Implement ErrorContext for Error with set_context and context - Remove duplicate set_context/context from impl Error - Remove #[allow(unused)] and TODO from error module - Remove #[allow(dead_code)] from ErrorContext::context in trait - ParseResult::context now uses ErrorContext::context for Error No behavior change; existing tests pass. Made-with: Cursor
1 parent 15957ff commit e632428

File tree

1 file changed

+23
-25
lines changed

1 file changed

+23
-25
lines changed

core/parser/src/error/mod.rs

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,38 @@ pub(crate) trait ErrorContext {
1616
fn set_context(self, context: &'static str) -> Self;
1717

1818
/// Gets the context of the error, if any.
19-
#[allow(dead_code)]
2019
fn context(&self) -> Option<&'static str>;
2120
}
2221

22+
impl ErrorContext for Error {
23+
fn set_context(self, new_context: &'static str) -> Self {
24+
match self {
25+
Self::Expected {
26+
expected,
27+
found,
28+
span,
29+
..
30+
} => Self::expected(expected, found, span, new_context),
31+
e => e,
32+
}
33+
}
34+
35+
fn context(&self) -> Option<&'static str> {
36+
if let Self::Expected { context, .. } = self {
37+
Some(*context)
38+
} else {
39+
None
40+
}
41+
}
42+
}
43+
2344
impl<T> ErrorContext for ParseResult<T> {
2445
fn set_context(self, context: &'static str) -> Self {
2546
self.map_err(|e| e.set_context(context))
2647
}
2748

2849
fn context(&self) -> Option<&'static str> {
29-
self.as_ref().err().and_then(Error::context)
50+
self.as_ref().err().and_then(ErrorContext::context)
3051
}
3152
}
3253

@@ -93,29 +114,6 @@ pub enum Error {
93114
}
94115

95116
impl Error {
96-
/// Changes the context of the error, if any.
97-
fn set_context(self, new_context: &'static str) -> Self {
98-
match self {
99-
Self::Expected {
100-
expected,
101-
found,
102-
span,
103-
..
104-
} => Self::expected(expected, found, span, new_context),
105-
e => e,
106-
}
107-
}
108-
109-
/// Gets the context of the error, if any.
110-
#[allow(unused)] // TODO: context method is unused, candidate for removal?
111-
const fn context(&self) -> Option<&'static str> {
112-
if let Self::Expected { context, .. } = self {
113-
Some(context)
114-
} else {
115-
None
116-
}
117-
}
118-
119117
/// Creates an `Expected` parsing error.
120118
pub(crate) fn expected<E, F>(expected: E, found: F, span: Span, context: &'static str) -> Self
121119
where

0 commit comments

Comments
 (0)