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
use jsona::Error as JsonaError;
use jsona::Position;
use thiserror::Error as TError;

#[derive(TError, Debug)]
pub enum Error {
    #[error("{0}")]
    Jsona(JsonaError),
    #[error("annotation in scope {} at line {} col {} {}", .scope, .position.line, .position.col, .message)]
    InvalidAnnotation {
        message: String,
        scope: String,
        position: Position,
    },
    #[error("value {} in scope at line {} col {} {}", .scope, .position.line, .position.col, .message)]
    InvalidAst {
        message: String,
        scope: String,
        position: Position,
    },
}

pub type Result<T> = std::result::Result<T, Error>;

impl Error {
    pub fn invalid_annotation<T: ToString>(
        message: T,
        name: &str,
        scope: &[&str],
        position: Position,
    ) -> Self {
        let scope = if scope.len() > 0 {
            format!("{}@{}", scope.join("."), name)
        } else {
            format!("@{}", name)
        };
        Error::InvalidAnnotation {
            message: message.to_string(),
            scope,
            position,
        }
    }
    pub fn invalid_ast<T: ToString>(message: T, scope: &[&str], position: Position) -> Self {
        let scope = scope.join(".");
        Error::InvalidAst {
            message: message.to_string(),
            scope,
            position,
        }
    }
}

impl From<JsonaError> for Error {
    fn from(e: JsonaError) -> Self {
        Error::Jsona(e)
    }
}