Skip to main content

entrenar/monitor/inference/serialization/
error.rs

1//! Serialization error types.
2
3/// Serialization error
4#[derive(Debug)]
5pub enum SerializationError {
6    /// Invalid format
7    InvalidFormat(String),
8    /// Version mismatch
9    VersionMismatch { expected: u8, actual: u8 },
10    /// JSON error
11    Json(serde_json::Error),
12    /// IO error
13    Io(std::io::Error),
14}
15
16impl std::fmt::Display for SerializationError {
17    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
18        match self {
19            SerializationError::InvalidFormat(msg) => write!(f, "Invalid format: {msg}"),
20            SerializationError::VersionMismatch { expected, actual } => {
21                write!(f, "Version mismatch: expected {expected}, got {actual}")
22            }
23            SerializationError::Json(e) => write!(f, "JSON error: {e}"),
24            SerializationError::Io(e) => write!(f, "IO error: {e}"),
25        }
26    }
27}
28
29impl std::error::Error for SerializationError {
30    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
31        match self {
32            SerializationError::Json(e) => Some(e),
33            SerializationError::Io(e) => Some(e),
34            _ => None,
35        }
36    }
37}
38
39impl From<serde_json::Error> for SerializationError {
40    fn from(e: serde_json::Error) -> Self {
41        SerializationError::Json(e)
42    }
43}
44
45impl From<std::io::Error> for SerializationError {
46    fn from(e: std::io::Error) -> Self {
47        SerializationError::Io(e)
48    }
49}