Skip to main content

ctrl_freeq/
error.rs

1//! The crate's error type.
2//!
3//! The Python original raises exceptions from deep inside its setup code.  Here every failure is a typed value that
4//! propagates through [`Result`], so a caller - the GUI in particular - can show it instead of crashing.
5
6use std::fmt;
7
8/// Everything that can go wrong inside ctrl-freeq.
9#[derive(Debug, Clone, PartialEq)]
10pub enum Error {
11    /// The configuration is missing a value, has an inconsistent one, or names something unknown.
12    Config(String),
13    /// A valid request for something this version does not implement, such as an algorithm name from the Python
14    /// package that has not been ported.
15    NotSupported(String),
16    /// Two operands have shapes that do not line up.
17    Dimension(String),
18    /// A numerical routine failed or produced a non-finite value.
19    Numerical(String),
20    /// Reading or writing a file failed.
21    Io(String),
22}
23
24impl fmt::Display for Error {
25    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26        match self {
27            Error::Config(s) => write!(f, "invalid configuration: {s}"),
28            Error::NotSupported(s) => write!(f, "not supported: {s}"),
29            Error::Dimension(s) => write!(f, "dimension mismatch: {s}"),
30            Error::Numerical(s) => write!(f, "numerical failure: {s}"),
31            Error::Io(s) => write!(f, "io error: {s}"),
32        }
33    }
34}
35
36impl std::error::Error for Error {}
37
38impl From<std::io::Error> for Error {
39    fn from(e: std::io::Error) -> Self {
40        Error::Io(e.to_string())
41    }
42}
43
44/// Convenience alias used throughout the crate.
45pub type Result<T> = std::result::Result<T, Error>;