Skip to main content

cui/
error.rs

1//! cui-level error conditions: thin wrappers over the cuj library
2//! errors, mapped to the same stable exit codes (cuj SPEC §10.3).
3
4use std::fmt;
5
6#[derive(Debug)]
7pub enum Error {
8    /// A cuj / substrate error, surfaced with its own exit code.
9    Cuj(cuj::Error),
10    /// Command-line or ex-command usage error.
11    Usage(String),
12    Io(std::io::Error),
13}
14
15impl Error {
16    pub fn exit_code(&self) -> i32 {
17        match self {
18            Error::Cuj(e) => e.exit_code(),
19            Error::Usage(_) => 2,
20            Error::Io(_) => 1,
21        }
22    }
23}
24
25impl fmt::Display for Error {
26    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27        match self {
28            Error::Cuj(e) => write!(f, "{e}"),
29            Error::Usage(m) => write!(f, "usage: {m}"),
30            Error::Io(e) => write!(f, "io: {e}"),
31        }
32    }
33}
34
35impl std::error::Error for Error {}
36
37impl From<cuj::Error> for Error {
38    fn from(e: cuj::Error) -> Self {
39        Error::Cuj(e)
40    }
41}
42
43impl From<std::io::Error> for Error {
44    fn from(e: std::io::Error) -> Self {
45        Error::Io(e)
46    }
47}
48
49impl From<metatheca::Error> for Error {
50    fn from(e: metatheca::Error) -> Self {
51        Error::Cuj(e.into())
52    }
53}
54
55impl From<taxopsis::Error> for Error {
56    fn from(e: taxopsis::Error) -> Self {
57        Error::Cuj(e.into())
58    }
59}
60
61impl From<logopsis::Error> for Error {
62    fn from(e: logopsis::Error) -> Self {
63        Error::Cuj(e.into())
64    }
65}
66
67impl From<zetetes::Error> for Error {
68    fn from(e: zetetes::Error) -> Self {
69        Error::Cuj(cuj::Error::Substrate(format!("zetetes: {e}")))
70    }
71}
72
73pub type Result<T> = std::result::Result<T, Error>;