cradle-shared 0.1.1

Shared utilities in use by the Cradle agent and cli
Documentation
use std::error::Error;
use std::fmt::{Display, Formatter};

/// Error enum in use by the workspace
#[derive(Debug)]
pub enum CradleError {
    /// IO Error wrapper
    IoError(std::io::Error),
    /// Failed to spawn target (error code)
    SpawnError(i32),
    /// Invalid path specified
    InvalidPath,
    /// Invalid host specified
    InvalidHost,
    /// A Windows error has occurred (wrapper around `GetLastError()` pretty much)
    WindowsError(i32),
    /// An invalid value was specified
    InvalidValue(String),
    /// Failed to find the specified module
    ModuleNotFound(String),
    /// A libloading error has occurred
    LibloadError(String),
    /// Failed to load a plugin
    PluginLoadError(String),
}

impl Display for CradleError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            CradleError::IoError(e) => write!(f, "{e}"),
            CradleError::SpawnError(e) => write!(f, "a spawn error occurred. Code: 0x{e:.x} ({e})"),
            CradleError::InvalidPath => write!(f, "invalid path"),
            CradleError::InvalidHost => write!(f, "invalid host"),
            CradleError::WindowsError(e) => write!(f, "windows error: {e}"),
            CradleError::InvalidValue(e) => write!(f, "invalid value: {e}"),
            CradleError::ModuleNotFound(e) => write!(f, "module not found: {e}"),
            CradleError::LibloadError(e) => write!(f, "libloading error: {e}"),
            CradleError::PluginLoadError(e) => write!(f, "plugin loading error: {e}"),
        }
    }
}

/// Small `Result` wrapper
pub type CradleResult<T = ()> = Result<T, CradleError>;

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

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