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
use crate::duplicates::DuplicateError;

use crate::SyntaxError;
use std::{
    error::{Error, Report},
    fmt::{Debug, Display, Formatter},
};

pub mod display;

pub type ValkyrieResult<T = ()> = Result<T, ValkyrieError>;

pub trait ValkyrieErrorType: Error {
    fn boxed(self) -> ValkyrieError;
    fn error_code(&self) -> usize;
    fn as_report(&self) -> Report;
}

impl Error for ValkyrieError {}

impl Debug for ValkyrieError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            ValkyrieError::Duplicate(v) => Debug::fmt(v, f),
            ValkyrieError::Custom(v) => Debug::fmt(v, f),
            ValkyrieError::Syntax(v) => Debug::fmt(v, f),
        }
    }
}

impl Display for ValkyrieError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            ValkyrieError::Duplicate(v) => Display::fmt(v, f),
            ValkyrieError::Custom(v) => Display::fmt(v, f),
            ValkyrieError::Syntax(v) => Display::fmt(v, f),
        }
    }
}

pub enum ValkyrieError {
    Syntax(Box<SyntaxError>),
    Duplicate(Box<DuplicateError>),
    Custom(Box<String>),
}

impl ValkyrieError {
    pub fn custom<S: ToString>(message: S) -> Self {
        Self::Custom(Box::new(message.to_string()))
    }
}