use std::fmt;
use super::{CatalogError, ConfigError, PreparedToolsError, ServeError, WatchError};
#[derive(Debug)]
#[non_exhaustive]
pub struct RunError {
repr: RunErrorRepr,
}
#[derive(Debug)]
enum RunErrorRepr {
Config(ConfigError),
Catalog(CatalogError),
Prepare(PreparedToolsError),
Watch(WatchError),
Serve(ServeError),
Runtime(std::io::Error),
}
impl From<ConfigError> for RunError {
fn from(source: ConfigError) -> RunError {
RunError {
repr: RunErrorRepr::Config(source),
}
}
}
impl From<CatalogError> for RunError {
fn from(source: CatalogError) -> RunError {
RunError {
repr: RunErrorRepr::Catalog(source),
}
}
}
impl From<PreparedToolsError> for RunError {
fn from(source: PreparedToolsError) -> RunError {
RunError {
repr: RunErrorRepr::Prepare(source),
}
}
}
impl From<WatchError> for RunError {
fn from(source: WatchError) -> RunError {
RunError {
repr: RunErrorRepr::Watch(source),
}
}
}
impl From<ServeError> for RunError {
fn from(source: ServeError) -> RunError {
RunError {
repr: RunErrorRepr::Serve(source),
}
}
}
impl RunError {
pub(crate) fn runtime(source: std::io::Error) -> RunError {
RunError {
repr: RunErrorRepr::Runtime(source),
}
}
}
impl fmt::Display for RunError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.repr {
RunErrorRepr::Config(_) => f.write_str("load the configuration"),
RunErrorRepr::Catalog(_) => f.write_str("resolve the prompt catalog"),
RunErrorRepr::Prepare(_) => f.write_str("prepare the tool environment"),
RunErrorRepr::Watch(_) => f.write_str("start the prompt watcher"),
RunErrorRepr::Serve(_) => f.write_str("serve the transport"),
RunErrorRepr::Runtime(_) => f.write_str("build the async runtime"),
}
}
}
impl std::error::Error for RunError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match &self.repr {
RunErrorRepr::Config(source) => Some(source),
RunErrorRepr::Catalog(source) => Some(source),
RunErrorRepr::Prepare(source) => Some(source),
RunErrorRepr::Watch(source) => Some(source),
RunErrorRepr::Serve(source) => Some(source),
RunErrorRepr::Runtime(source) => Some(source),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum RunErrorKind {
Config,
Catalog,
Prepare,
Watch,
Serve,
Runtime,
}
impl RunError {
#[must_use]
pub fn kind(&self) -> RunErrorKind {
match &self.repr {
RunErrorRepr::Config(_) => RunErrorKind::Config,
RunErrorRepr::Catalog(_) => RunErrorKind::Catalog,
RunErrorRepr::Prepare(_) => RunErrorKind::Prepare,
RunErrorRepr::Watch(_) => RunErrorKind::Watch,
RunErrorRepr::Serve(_) => RunErrorKind::Serve,
RunErrorRepr::Runtime(_) => RunErrorKind::Runtime,
}
}
}