Skip to content

Commit 2531947

Browse files
authored
add map_span to Rich error (#950)
1 parent b81c34c commit 2531947

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/error.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,17 @@ impl<'a, T, S> Simple<'a, T, S> {
239239
found: self.found.map(|found| f(found.into_inner()).into()),
240240
}
241241
}
242+
243+
/// Transform this error's span using the given function.
244+
///
245+
/// This is useful when you need to convert spans from one representation to another,
246+
/// such as when enriching or interning spans.
247+
pub fn map_span<S2, F: FnOnce(S) -> S2>(self, f: F) -> Simple<'a, T, S2> {
248+
Simple {
249+
span: f(self.span),
250+
found: self.found,
251+
}
252+
}
242253
}
243254

244255
impl<'a, I: Input<'a>> Error<'a, I> for Simple<'a, I::Token, I::Span> {}
@@ -709,6 +720,18 @@ impl<'a, T, S> Rich<'a, T, S> {
709720
.collect(),
710721
}
711722
}
723+
724+
/// Transform this error's spans using the given function.
725+
///
726+
/// This is useful when you need to convert spans, such as enriching or
727+
/// interning spans.
728+
pub fn map_span<S2, F: FnMut(S) -> S2>(self, mut f: F) -> Rich<'a, T, S2> {
729+
Rich {
730+
span: f(self.span),
731+
reason: self.reason,
732+
context: self.context.into_iter().map(|(p, s)| (p, f(s))).collect(),
733+
}
734+
}
712735
}
713736

714737
impl<'a, I: Input<'a>> Error<'a, I> for Rich<'a, I::Token, I::Span>

0 commit comments

Comments
 (0)