Skip to main content

helios_serde/
error.rs

1/// Error types for FHIR serialization and deserialization.
2#[derive(Debug)]
3pub enum SerdeError {
4    /// JSON serialization or deserialization error
5    Json(serde_json::Error),
6
7    /// XML serialization or deserialization error
8    #[cfg(feature = "xml")]
9    Xml(quick_xml::Error),
10
11    /// IO error during serialization/deserialization
12    Io(std::io::Error),
13
14    /// Custom error message
15    Custom(String),
16}
17
18impl std::fmt::Display for SerdeError {
19    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
20        match self {
21            SerdeError::Json(e) => write!(f, "JSON error: {}", e),
22            #[cfg(feature = "xml")]
23            SerdeError::Xml(e) => write!(f, "XML error: {}", e),
24            SerdeError::Io(e) => write!(f, "IO error: {}", e),
25            SerdeError::Custom(msg) => write!(f, "{}", msg),
26        }
27    }
28}
29
30impl std::error::Error for SerdeError {
31    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
32        match self {
33            SerdeError::Json(e) => Some(e),
34            #[cfg(feature = "xml")]
35            SerdeError::Xml(e) => Some(e),
36            SerdeError::Io(e) => Some(e),
37            SerdeError::Custom(_) => None,
38        }
39    }
40}
41
42impl From<serde_json::Error> for SerdeError {
43    fn from(err: serde_json::Error) -> Self {
44        SerdeError::Json(err)
45    }
46}
47
48#[cfg(feature = "xml")]
49impl From<quick_xml::Error> for SerdeError {
50    fn from(err: quick_xml::Error) -> Self {
51        SerdeError::Xml(err)
52    }
53}
54
55impl From<std::io::Error> for SerdeError {
56    fn from(err: std::io::Error) -> Self {
57        SerdeError::Io(err)
58    }
59}
60
61impl From<String> for SerdeError {
62    fn from(msg: String) -> Self {
63        SerdeError::Custom(msg)
64    }
65}
66
67impl From<&str> for SerdeError {
68    fn from(msg: &str) -> Self {
69        SerdeError::Custom(msg.to_string())
70    }
71}
72
73// Implement serde::ser::Error for serialization
74impl serde::ser::Error for SerdeError {
75    fn custom<T: std::fmt::Display>(msg: T) -> Self {
76        SerdeError::Custom(msg.to_string())
77    }
78}
79
80// Implement serde::de::Error for deserialization (will be needed in Phase 4)
81impl serde::de::Error for SerdeError {
82    fn custom<T: std::fmt::Display>(msg: T) -> Self {
83        SerdeError::Custom(msg.to_string())
84    }
85}
86
87/// Result type alias for FHIR serialization operations
88pub type Result<T> = std::result::Result<T, SerdeError>;