Skip to main content

molrs_core/
error.rs

1//! Unified error types for the molrs library.
2
3use std::fmt;
4use std::io;
5
6use crate::block::BlockError;
7
8/// Main error type for the molrs library.
9#[derive(Debug)]
10pub enum MolRsError {
11    /// Error from Block operations
12    Block(BlockError),
13
14    /// IO error (file reading/writing)
15    Io(io::Error),
16
17    /// Parse error with context
18    Parse {
19        /// Line number where error occurred (if applicable)
20        line: Option<usize>,
21        /// Error message
22        message: String,
23    },
24
25    /// Validation error
26    Validation {
27        /// Error message
28        message: String,
29    },
30
31    /// Zarr I/O error
32    Zarr {
33        /// Error message
34        message: String,
35    },
36
37    /// Entity not found (atom, bond, angle, dihedral)
38    NotFound {
39        /// Kind of entity ("atom", "bond", "angle", "dihedral")
40        entity: &'static str,
41        /// Human-readable message
42        message: String,
43    },
44
45    /// Generic error with message
46    Other(String),
47}
48
49impl fmt::Display for MolRsError {
50    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51        match self {
52            MolRsError::Block(e) => write!(f, "Block error: {}", e),
53            MolRsError::Io(e) => write!(f, "IO error: {}", e),
54            MolRsError::Parse {
55                line: Some(line),
56                message,
57            } => {
58                write!(f, "Parse error at line {}: {}", line, message)
59            }
60            MolRsError::Parse {
61                line: None,
62                message,
63            } => {
64                write!(f, "Parse error: {}", message)
65            }
66            MolRsError::Validation { message } => {
67                write!(f, "Validation error: {}", message)
68            }
69            MolRsError::NotFound { entity, message } => {
70                write!(f, "{} not found: {}", entity, message)
71            }
72            MolRsError::Zarr { message } => {
73                write!(f, "Zarr error: {}", message)
74            }
75            MolRsError::Other(msg) => write!(f, "{}", msg),
76        }
77    }
78}
79
80impl std::error::Error for MolRsError {
81    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
82        match self {
83            MolRsError::Block(e) => Some(e),
84            MolRsError::Io(e) => Some(e),
85            _ => None,
86        }
87    }
88}
89
90// Automatic conversions
91impl From<BlockError> for MolRsError {
92    fn from(err: BlockError) -> Self {
93        MolRsError::Block(err)
94    }
95}
96
97impl From<io::Error> for MolRsError {
98    fn from(err: io::Error) -> Self {
99        MolRsError::Io(err)
100    }
101}
102
103impl From<String> for MolRsError {
104    fn from(msg: String) -> Self {
105        MolRsError::Other(msg)
106    }
107}
108
109impl From<&str> for MolRsError {
110    fn from(msg: &str) -> Self {
111        MolRsError::Other(msg.to_string())
112    }
113}
114
115#[cfg(feature = "zarr")]
116impl From<zarrs::group::GroupCreateError> for MolRsError {
117    fn from(e: zarrs::group::GroupCreateError) -> Self {
118        MolRsError::zarr(e.to_string())
119    }
120}
121
122#[cfg(feature = "zarr")]
123impl From<zarrs::storage::StorageError> for MolRsError {
124    fn from(e: zarrs::storage::StorageError) -> Self {
125        MolRsError::zarr(e.to_string())
126    }
127}
128
129#[cfg(feature = "zarr")]
130impl From<zarrs::array::ArrayCreateError> for MolRsError {
131    fn from(e: zarrs::array::ArrayCreateError) -> Self {
132        MolRsError::zarr(e.to_string())
133    }
134}
135
136#[cfg(feature = "zarr")]
137impl From<zarrs::array::ArrayError> for MolRsError {
138    fn from(e: zarrs::array::ArrayError) -> Self {
139        MolRsError::zarr(e.to_string())
140    }
141}
142
143#[cfg(feature = "zarr")]
144impl From<zarrs::node::NodeCreateError> for MolRsError {
145    fn from(e: zarrs::node::NodeCreateError) -> Self {
146        MolRsError::zarr(e.to_string())
147    }
148}
149
150// Helper constructors
151impl MolRsError {
152    /// Create a parse error with line number
153    pub fn parse_error(line: usize, message: impl Into<String>) -> Self {
154        MolRsError::Parse {
155            line: Some(line),
156            message: message.into(),
157        }
158    }
159
160    /// Create a parse error without line number
161    pub fn parse(message: impl Into<String>) -> Self {
162        MolRsError::Parse {
163            line: None,
164            message: message.into(),
165        }
166    }
167
168    /// Create a validation error
169    pub fn validation(message: impl Into<String>) -> Self {
170        MolRsError::Validation {
171            message: message.into(),
172        }
173    }
174
175    /// Create a not-found error
176    pub fn not_found(entity: &'static str, message: impl Into<String>) -> Self {
177        MolRsError::NotFound {
178            entity,
179            message: message.into(),
180        }
181    }
182
183    /// Create a Zarr I/O error
184    pub fn zarr(message: impl Into<String>) -> Self {
185        MolRsError::Zarr {
186            message: message.into(),
187        }
188    }
189}
190
191#[cfg(test)]
192mod tests {
193    use super::*;
194    use crate::block::BlockError;
195
196    #[test]
197    fn test_error_display() {
198        let err = MolRsError::parse_error(42, "unexpected token");
199        assert_eq!(
200            format!("{}", err),
201            "Parse error at line 42: unexpected token"
202        );
203
204        let err = MolRsError::parse("invalid format");
205        assert_eq!(format!("{}", err), "Parse error: invalid format");
206
207        let err = MolRsError::validation("inconsistent dimensions");
208        assert_eq!(
209            format!("{}", err),
210            "Validation error: inconsistent dimensions"
211        );
212    }
213
214    #[test]
215    fn test_from_block_error() {
216        let block_err = BlockError::RankZero {
217            key: "test".to_string(),
218        };
219        let err: MolRsError = block_err.into();
220        assert!(matches!(err, MolRsError::Block(_)));
221    }
222
223    #[test]
224    fn test_from_string() {
225        let err: MolRsError = "test error".into();
226        assert!(matches!(err, MolRsError::Other(_)));
227        assert_eq!(format!("{}", err), "test error");
228    }
229}