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    /// Filesystem operation failed.
27    #[error("filesystem error at {}: {source}", path.display())]
28    Io {
29        /// Path involved in the failure.
30        path: PathBuf,
31        /// Source IO error.
32        #[source]
33        source: io::Error,
34    },
35    /// Runtime execution failed.
36    #[error("run failed: {0}")]
37    Run(String),
38    /// Serialization failed.
39    #[error("serialization error: {0}")]
40    Serialization(String),
41}
42
43impl From<rusqlite::Error> for CliError {
44    fn from(error: rusqlite::Error) -> Self {
45        Self::Storage(error.to_string())
46    }
47}
48
49impl From<serde_json::Error> for CliError {
50    fn from(error: serde_json::Error) -> Self {
51        Self::Serialization(error.to_string())
52    }
53}
54
55impl From<toml::de::Error> for CliError {
56    fn from(error: toml::de::Error) -> Self {
57        Self::Config(error.to_string())
58    }
59}
60
61impl From<toml::ser::Error> for CliError {
62    fn from(error: toml::ser::Error) -> Self {
63        Self::Config(error.to_string())
64    }
65}
66
67/// Map an IO error with a path.
68pub fn io_error(path: impl Into<PathBuf>, source: io::Error) -> CliError {
69    CliError::Io {
70        path: path.into(),
71        source,
72    }
73}