Skip to main content

runx_runtime/tool_catalogs/
error.rs

1use std::fmt;
2use std::io;
3use std::path::PathBuf;
4
5#[derive(Debug)]
6pub enum ToolCatalogError {
7    Io {
8        action: &'static str,
9        path: PathBuf,
10        source: io::Error,
11    },
12    Json {
13        action: &'static str,
14        path: PathBuf,
15        source: serde_json::Error,
16    },
17    InvalidManifest {
18        path: PathBuf,
19        message: String,
20    },
21    InvalidRequest(String),
22    NotFound(String),
23}
24
25impl ToolCatalogError {
26    pub(crate) fn io(action: &'static str, path: impl Into<PathBuf>, source: io::Error) -> Self {
27        Self::Io {
28            action,
29            path: path.into(),
30            source,
31        }
32    }
33
34    pub(crate) fn json(
35        action: &'static str,
36        path: impl Into<PathBuf>,
37        source: serde_json::Error,
38    ) -> Self {
39        Self::Json {
40            action,
41            path: path.into(),
42            source,
43        }
44    }
45
46    pub(crate) fn concise_message(&self) -> String {
47        match self {
48            Self::InvalidManifest { message, .. }
49            | Self::InvalidRequest(message)
50            | Self::NotFound(message) => message.clone(),
51            Self::Io {
52                action,
53                path,
54                source,
55            } => format!("{action} {}: {source}", path.display()),
56            Self::Json {
57                action,
58                path,
59                source,
60            } => format!("{action} {}: {source}", path.display()),
61        }
62    }
63}
64
65impl fmt::Display for ToolCatalogError {
66    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
67        match self {
68            Self::Io {
69                action,
70                path,
71                source,
72            } => write!(formatter, "{action} {}: {source}", path.display()),
73            Self::Json {
74                action,
75                path,
76                source,
77            } => write!(formatter, "{action} {}: {source}", path.display()),
78            Self::InvalidManifest { path, message } => {
79                write!(formatter, "{}: {message}", path.display())
80            }
81            Self::InvalidRequest(message) => formatter.write_str(message),
82            Self::NotFound(message) => formatter.write_str(message),
83        }
84    }
85}
86
87impl std::error::Error for ToolCatalogError {}