1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
//! Report errors into the context.
use errors::Error;
use {Diagnostics, Span};

pub trait WithSpan<T> {
    /// Report the span to the diagnostics and convert the result into a unit error if failed.
    fn with_span<S: Into<Span>>(self, diag: &mut Diagnostics, span: S) -> Result<T, ()>;
}

impl<T> WithSpan<T> for Result<T, Error> {
    fn with_span<S: Into<Span>>(self, diag: &mut Diagnostics, span: S) -> Result<T, ()> {
        match self {
            Ok(value) => Ok(value),
            Err(e) => {
                diag.err(span, e.display());
                Err(())
            }
        }
    }
}