cuj-tui 0.1.0

cui — a read-only TUI browser for cuj vaults
Documentation
//! cui-level error conditions: thin wrappers over the cuj library
//! errors, mapped to the same stable exit codes (cuj SPEC §10.3).

use std::fmt;

#[derive(Debug)]
pub enum Error {
    /// A cuj / substrate error, surfaced with its own exit code.
    Cuj(cuj::Error),
    /// Command-line or ex-command usage error.
    Usage(String),
    Io(std::io::Error),
}

impl Error {
    pub fn exit_code(&self) -> i32 {
        match self {
            Error::Cuj(e) => e.exit_code(),
            Error::Usage(_) => 2,
            Error::Io(_) => 1,
        }
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Error::Cuj(e) => write!(f, "{e}"),
            Error::Usage(m) => write!(f, "usage: {m}"),
            Error::Io(e) => write!(f, "io: {e}"),
        }
    }
}

impl std::error::Error for Error {}

impl From<cuj::Error> for Error {
    fn from(e: cuj::Error) -> Self {
        Error::Cuj(e)
    }
}

impl From<std::io::Error> for Error {
    fn from(e: std::io::Error) -> Self {
        Error::Io(e)
    }
}

impl From<metatheca::Error> for Error {
    fn from(e: metatheca::Error) -> Self {
        Error::Cuj(e.into())
    }
}

impl From<taxopsis::Error> for Error {
    fn from(e: taxopsis::Error) -> Self {
        Error::Cuj(e.into())
    }
}

impl From<logopsis::Error> for Error {
    fn from(e: logopsis::Error) -> Self {
        Error::Cuj(e.into())
    }
}

impl From<zetetes::Error> for Error {
    fn from(e: zetetes::Error) -> Self {
        Error::Cuj(cuj::Error::Substrate(format!("zetetes: {e}")))
    }
}

pub type Result<T> = std::result::Result<T, Error>;