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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
use std::fmt::Display;

use thiserror::Error;

pub type RealmeResult<T> = Result<T, RealmeError>;

/// Represents all possible errors that can occur in the Realme library.
#[derive(Error, Debug)]
pub enum RealmeError {
    #[error(transparent)]
    InvalidCast(CastError),
    #[error(transparent)]
    ParseError(ParseError),
    #[error("Expression error: {0}")]
    ExprError(String),

    #[error("Build error: {0}")]
    BuildError(String),
    #[error("Read file error: {0}")]
    ReadFileError(String),

    #[error(transparent)]
    DeserializeError(DeserializeError),
    #[error(transparent)]
    SerializeError(SerializeError),

    #[error("Unknown error: {0}")]
    Unknown(String),
}

impl RealmeError {
    /// Creates a new `InvalidCast` error.
    pub fn new_cast_error(origin: String, cause: String) -> Self {
        Self::InvalidCast(CastError::new(origin, cause))
    }

    /// Creates a new `ParseError`.
    pub fn new_parse_error(from: String, to: String, cause: String) -> Self {
        Self::ParseError(ParseError::new(from, to, cause))
    }

    /// Creates a new `BuildError`.
    pub const fn new_build_error(cause: String) -> Self {
        Self::BuildError(cause)
    }
}

/// Error type for casting operations within Realme.
#[derive(Debug, Error)]
pub struct CastError {
    origin: String,
    cause: String,
}

impl CastError {
    pub fn new(origin: String, cause: String) -> Self {
        tracing::error!("Cast error: {}, error: {}", origin, cause);
        Self { origin, cause }
    }
}

impl Display for CastError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Cast from {}, error: {}", self.origin, self.cause)
    }
}

/// Error type for parsing operations within Realme.
#[derive(Debug, Error)]
pub struct ParseError {
    from: String,
    to: String,
    cause: String,
}

impl Display for ParseError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "Parse {} to {}, error: {}",
            self.from, self.to, self.cause
        )
    }
}

impl ParseError {
    pub fn new(from: String, to: String, cause: String) -> Self {
        tracing::error!("Parse error: {} to {}, error: {}", from, to, cause);
        Self { from, to, cause }
    }
}

/// Error type for deserialization operations within Realme.
#[derive(Debug, Error)]
pub struct DeserializeError(String);

impl serde::de::Error for DeserializeError {
    fn custom<T>(msg: T) -> Self
    where
        T: Display,
    {
        Self(msg.to_string())
    }
}

impl Display for DeserializeError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl From<DeserializeError> for RealmeError {
    fn from(value: DeserializeError) -> Self {
        tracing::error!("Deserialize error: {}", value);
        Self::DeserializeError(value)
    }
}

/// Error type for serialization operations within Realme.
#[derive(Debug, Error)]
pub struct SerializeError(String);

impl serde::ser::Error for SerializeError {
    fn custom<T>(msg: T) -> Self
    where
        T: Display,
    {
        Self(msg.to_string())
    }
}

impl Display for SerializeError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl From<SerializeError> for RealmeError {
    fn from(value: SerializeError) -> Self {
        tracing::error!("Serialize error: {}", value);
        Self::SerializeError(value)
    }
}

#[derive(Debug, Error)]
pub struct ExprError(String);

impl Display for ExprError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}