Skip to main content

modelio/
error.rs

1use std::error::Error;
2use std::fmt;
3
4/// Aliases the result shape used by the corresponding Model I/O wrappers.
5pub type Result<T> = std::result::Result<T, ModelIoError>;
6
7#[derive(Debug, Clone, PartialEq, Eq)]
8/// Wraps the corresponding Model I/O model io error counterpart.
9pub struct ModelIoError {
10    code: i32,
11    message: String,
12}
13
14impl ModelIoError {
15    /// Wraps the corresponding Model I/O initializer for the wrapped Model I/O model io error counterpart.
16    pub(crate) fn new(code: i32, message: impl Into<String>) -> Self {
17        Self {
18            code,
19            message: message.into(),
20        }
21    }
22
23    #[must_use]
24    /// Calls the corresponding Model I/O method on the wrapped Model I/O model io error counterpart.
25    pub fn code(&self) -> i32 {
26        self.code
27    }
28
29    #[must_use]
30    /// Calls the corresponding Model I/O method on the wrapped Model I/O model io error counterpart.
31    pub fn message(&self) -> &str {
32        &self.message
33    }
34}
35
36impl fmt::Display for ModelIoError {
37    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38        write!(f, "{} (status {})", self.message, self.code)
39    }
40}
41
42impl Error for ModelIoError {}