Skip to main content

starweaver_cli/
error.rs

1//! CLI error types.
2
3use std::{io, path::PathBuf};
4
5/// CLI result alias.
6pub type CliResult<T> = Result<T, CliError>;
7
8/// CLI failure.
9#[derive(Debug, thiserror::Error)]
10pub enum CliError {
11    /// Command-line parser failed.
12    #[error("{0}")]
13    Usage(String),
14    /// Command-line parser requested display output.
15    #[error("{0}")]
16    Display(String),
17    /// Configuration loading failed.
18    #[error("configuration error: {0}")]
19    Config(String),
20    /// Local storage failed.
21    #[error("storage error: {0}")]
22    Storage(String),
23    /// Requested record was missing.
24    #[error("not found: {0}")]
25    NotFound(String),
26    /// Optional product capability is not installed or does not support the request.
27    #[error("unsupported capability: {0}")]
28    Unsupported(String),
29    /// Filesystem operation failed.
30    #[error("filesystem error at {}: {source}", path.display())]
31    Io {
32        /// Path involved in the failure.
33        path: PathBuf,
34        /// Source IO error.
35        #[source]
36        source: io::Error,
37    },
38    /// Runtime execution failed.
39    #[error("run failed: {0}")]
40    Run(String),
41    /// Serialization failed.
42    #[error("serialization error: {0}")]
43    Serialization(String),
44}
45
46impl From<serde_json::Error> for CliError {
47    fn from(error: serde_json::Error) -> Self {
48        Self::Serialization(error.to_string())
49    }
50}
51
52impl From<toml::de::Error> for CliError {
53    fn from(error: toml::de::Error) -> Self {
54        Self::Config(error.to_string())
55    }
56}
57
58impl From<toml::ser::Error> for CliError {
59    fn from(error: toml::ser::Error) -> Self {
60        Self::Config(error.to_string())
61    }
62}
63
64/// Map an IO error with a path.
65pub fn io_error(path: impl Into<PathBuf>, source: io::Error) -> CliError {
66    CliError::Io {
67        path: path.into(),
68        source,
69    }
70}