datex_core/serde/
error.rs

1use core::fmt;
2use serde::de::Error;
3use serde::ser::StdError;
4use serde::ser::{self};
5use std::fmt::Display;
6use std::io;
7
8use crate::compiler::error::CompilerError;
9use crate::runtime::execution::ExecutionError;
10
11#[derive(Debug)]
12pub enum SerializationError {
13    Custom(String),
14    CanNotSerialize(String),
15    CompilerError(CompilerError),
16}
17impl ser::Error for SerializationError {
18    fn custom<T: fmt::Display>(msg: T) -> Self {
19        SerializationError::Custom(msg.to_string())
20    }
21}
22impl Error for SerializationError {
23    fn custom<T: fmt::Display>(msg: T) -> Self {
24        SerializationError::Custom(msg.to_string())
25    }
26}
27
28impl From<io::Error> for SerializationError {
29    fn from(e: io::Error) -> Self {
30        SerializationError::Custom(e.to_string())
31    }
32}
33impl From<CompilerError> for SerializationError {
34    fn from(e: CompilerError) -> Self {
35        SerializationError::CompilerError(e)
36    }
37}
38impl StdError for SerializationError {}
39impl Display for SerializationError {
40    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41        match self {
42            SerializationError::Custom(msg) => {
43                write!(f, "Serialization error: {}", msg)
44            }
45            SerializationError::CanNotSerialize(msg) => {
46                write!(f, "Can not serialize value: {}", msg)
47            }
48            SerializationError::CompilerError(err) => {
49                write!(f, "Compiler error: {}", err)
50            }
51        }
52    }
53}
54
55#[derive(Debug)]
56pub enum DeserializationError {
57    Custom(String),
58    CanNotDeserialize(String),
59    ExecutionError(ExecutionError),
60    CanNotReadFile(String),
61    CompilerError(CompilerError),
62    NoStaticValueFound,
63}
64impl ser::Error for DeserializationError {
65    fn custom<T: fmt::Display>(msg: T) -> Self {
66        DeserializationError::Custom(msg.to_string())
67    }
68}
69impl Error for DeserializationError {
70    fn custom<T: fmt::Display>(msg: T) -> Self {
71        DeserializationError::Custom(msg.to_string())
72    }
73}
74
75impl From<io::Error> for DeserializationError {
76    fn from(e: io::Error) -> Self {
77        DeserializationError::Custom(e.to_string())
78    }
79}
80impl From<ExecutionError> for DeserializationError {
81    fn from(e: ExecutionError) -> Self {
82        DeserializationError::ExecutionError(e)
83    }
84}
85impl From<CompilerError> for DeserializationError {
86    fn from(e: CompilerError) -> Self {
87        DeserializationError::CompilerError(e)
88    }
89}
90impl StdError for DeserializationError {}
91impl Display for DeserializationError {
92    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
93        match self {
94            DeserializationError::Custom(msg) => {
95                write!(f, "Deserialization error: {}", msg)
96            }
97            DeserializationError::CanNotDeserialize(msg) => {
98                write!(f, "Can not deserialize value: {}", msg)
99            }
100            DeserializationError::ExecutionError(err) => {
101                write!(f, "Execution error: {}", err)
102            }
103            DeserializationError::CanNotReadFile(msg) => {
104                write!(f, "Can not read file: {}", msg)
105            }
106            DeserializationError::CompilerError(err) => {
107                write!(f, "Compiler error: {}", err)
108            }
109            DeserializationError::NoStaticValueFound => {
110                write!(f, "No static value found in script")
111            }
112        }
113    }
114}