Skip to main content

diffsol_c/
error.rs

1// Convert diffsol errors to custom pydiffsol error type
2
3use diffsol::error::DiffsolError;
4use std::fmt;
5
6#[derive(Debug)]
7pub struct DiffsolRtError(DiffsolError);
8
9impl fmt::Display for DiffsolRtError {
10    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
11        write!(f, "{}", self.0)
12    }
13}
14
15impl std::error::Error for DiffsolRtError {}
16
17impl DiffsolRtError {
18    /// The same error, with `name` (for example the name of an argument) at the start of the
19    /// message.
20    pub(crate) fn named(self, name: &str) -> Self {
21        let message = match self.0 {
22            DiffsolError::Other(message) => message,
23            other => other.to_string(),
24        };
25        DiffsolError::Other(format!("{name}: {message}")).into()
26    }
27}
28
29impl From<DiffsolError> for DiffsolRtError {
30    fn from(other: DiffsolError) -> Self {
31        DiffsolRtError(other)
32    }
33}