Skip to main content

cradle_shared/
errors.rs

1use std::error::Error;
2use std::fmt::{Display, Formatter};
3
4/// Error enum in use by the workspace
5#[derive(Debug)]
6pub enum CradleError {
7    /// IO Error wrapper
8    IoError(std::io::Error),
9    /// Failed to spawn target (error code)
10    SpawnError(i32),
11    /// Invalid path specified
12    InvalidPath,
13    /// Invalid host specified
14    InvalidHost,
15    /// A Windows error has occurred (wrapper around `GetLastError()` pretty much)
16    WindowsError(i32),
17    /// An invalid value was specified
18    InvalidValue(String),
19    /// Failed to find the specified module
20    ModuleNotFound(String),
21    /// A libloading error has occurred
22    LibloadError(String),
23    /// Failed to load a plugin
24    PluginLoadError(String),
25}
26
27impl Display for CradleError {
28    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
29        match self {
30            CradleError::IoError(e) => write!(f, "{e}"),
31            CradleError::SpawnError(e) => write!(f, "a spawn error occurred. Code: 0x{e:.x} ({e})"),
32            CradleError::InvalidPath => write!(f, "invalid path"),
33            CradleError::InvalidHost => write!(f, "invalid host"),
34            CradleError::WindowsError(e) => write!(f, "windows error: {e}"),
35            CradleError::InvalidValue(e) => write!(f, "invalid value: {e}"),
36            CradleError::ModuleNotFound(e) => write!(f, "module not found: {e}"),
37            CradleError::LibloadError(e) => write!(f, "libloading error: {e}"),
38            CradleError::PluginLoadError(e) => write!(f, "plugin loading error: {e}"),
39        }
40    }
41}
42
43/// Small `Result` wrapper
44pub type CradleResult<T = ()> = Result<T, CradleError>;
45
46impl Error for CradleError {}
47impl From<std::io::Error> for CradleError {
48    fn from(e: std::io::Error) -> Self {
49        CradleError::IoError(e)
50    }
51}
52
53impl From<CradleError> for std::io::Error {
54    fn from(e: CradleError) -> Self {
55        std::io::Error::other(e)
56    }
57}