use serde::{Deserialize, Serialize};
#[derive(Debug, thiserror::Error, Serialize, Deserialize)]
#[non_exhaustive]
pub enum TaraError {
#[error("invalid parameter: {0}")]
InvalidParameter(String),
#[error("math error: {0}")]
MathError(String),
#[error("spectral error: {0}")]
SpectralError(String),
#[error("model error: {0}")]
ModelError(String),
#[error("I/O error: {0}")]
Io(String),
}
impl From<std::io::Error> for TaraError {
fn from(err: std::io::Error) -> Self {
Self::Io(err.to_string())
}
}
pub type Result<T> = std::result::Result<T, TaraError>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn tara_error_serde_roundtrip() {
let errors = [
TaraError::InvalidParameter("bad mass".into()),
TaraError::MathError("overflow".into()),
TaraError::SpectralError("no lines".into()),
TaraError::ModelError("diverged".into()),
TaraError::Io("file not found".into()),
];
for err in &errors {
let json = serde_json::to_string(err).unwrap();
let back: TaraError = serde_json::from_str(&json).unwrap();
assert_eq!(err.to_string(), back.to_string());
}
}
#[test]
fn io_error_converts() {
let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "missing");
let tara_err: TaraError = io_err.into();
assert!(matches!(tara_err, TaraError::Io(_)));
}
}