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
use miette::{Diagnostic, SourceSpan};
use starbase_styles::{Style, Stylize};
use thiserror::Error;

/// Error for a single parse failure. Can be used with [`TryFrom`], [`FromStr`], or serde.
#[derive(Clone, Debug, Diagnostic, Error)]
#[error("{message}")]
pub struct ParseError {
    /// Failure message.
    pub message: String,
}

impl ParseError {
    /// Create a new parse error with the provided message.
    pub fn new<T: AsRef<str>>(message: T) -> Self {
        ParseError {
            message: message.as_ref().to_owned(),
        }
    }
}

/// Error related to serde parsing.
#[derive(Error, Debug, Diagnostic)]
#[error("{}{} {message}", .path.style(Style::Id), ":".style(Style::MutedLight))]
#[diagnostic(severity(Error))]
pub struct ParserError {
    #[source_code]
    pub content: String, // NamedSource,

    pub message: String,

    pub path: String,

    #[label("Fix this")]
    pub span: Option<SourceSpan>,
}