reproto_core/with_span.rs
1//! Report errors into the context.
2use errors::Error;
3use {Diagnostics, Span};
4
5pub trait WithSpan<T> {
6 /// Report the span to the diagnostics and convert the result into a unit error if failed.
7 fn with_span<S: Into<Span>>(self, diag: &mut Diagnostics, span: S) -> Result<T, ()>;
8}
9
10impl<T> WithSpan<T> for Result<T, Error> {
11 fn with_span<S: Into<Span>>(self, diag: &mut Diagnostics, span: S) -> Result<T, ()> {
12 match self {
13 Ok(value) => Ok(value),
14 Err(e) => {
15 diag.err(span, e.display());
16 Err(())
17 }
18 }
19 }
20}