1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use crate::Span;
use std::error;
use std::fmt;

/// Trait to coerce a result of a non-spanned error into a spanned error.
pub trait WithSpan<T> {
    /// Convert the given result into a result which produces a spanned error.
    fn with_span(self, span: Span) -> Result<T, SpannedError>;
}

/// Blanket implementation that is helpful.
impl<T, E> WithSpan<T> for Result<T, E>
where
    anyhow::Error: From<E>,
{
    fn with_span(self, span: Span) -> Result<T, SpannedError> {
        match self {
            Ok(value) => Ok(value),
            Err(error) => Err(SpannedError::new(span, error)),
        }
    }
}

/// An error with an associated span.
#[derive(Debug)]
pub struct SpannedError {
    span: Span,
    inner: anyhow::Error,
}

impl SpannedError {
    /// Construct a new error with the associated span.
    pub fn new<E>(span: Span, error: E) -> Self
    where
        anyhow::Error: From<E>,
    {
        Self {
            span,
            inner: anyhow::Error::from(error),
        }
    }

    /// Construct a new error out of the given message.
    pub fn msg<M>(span: Span, message: M) -> Self
    where
        M: fmt::Display + fmt::Debug + Send + Sync + 'static,
    {
        Self {
            span,
            inner: anyhow::Error::msg(message),
        }
    }

    /// Convert into inner.
    pub fn into_inner(self) -> anyhow::Error {
        self.inner
    }

    /// Get the span of the error.
    pub fn span(&self) -> Span {
        self.span
    }
}

impl fmt::Display for SpannedError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(&self.inner, f)
    }
}

impl error::Error for SpannedError {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        self.inner.source()
    }
}