Skip to main content

config_easy/
error.rs

1use std::error::Error;
2use std::fmt;
3
4/// A custom error type for the configuration menu, encompassing various error scenarios that may occur during menu operation.
5#[derive(Debug)]
6pub enum ConfigEasyError {
7    /// An error that occurred while interacting with the settings store.
8    Store(Box<dyn Error + Send + Sync>),
9
10    /// An error that occurred during input/output operations, such as reading from or writing to the terminal.
11    Io(std::io::Error),
12
13    /// An error indicating that a provided SQL identifier (e.g., table name, column name) is invalid.
14    InvalidIdentifier(String),
15
16    /// An error indicating that the user made an invalid selection in the menu (e.g. out of range).
17    InvalidSelection,
18
19    /// An error indicating that validation of a proposed setting value failed.
20    ValidationFailed { key: String, message: String },
21
22    /// An error indicating that a provided action key is invalid (e.g., empty or numeric).
23    InvalidActionKey(String),
24
25    /// A custom action attempted to use a reserved menu key.
26    ReservedActionKey(String),
27
28    /// A custom action attempted to use a key already used by another action.
29    DuplicateActionKey(String),
30
31    /// An error indicating that a custom action callback failed.
32    ActionFailed { key: String, message: String },
33}
34
35impl fmt::Display for ConfigEasyError {
36    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37        match self {
38            Self::Store(error) => write!(f, "settings store error: {error}"),
39            Self::Io(error) => write!(f, "I/O error: {error}"),
40            Self::InvalidIdentifier(identifier) => {
41                write!(f, "invalid SQL identifier: {identifier}")
42            }
43            Self::InvalidSelection => f.write_str("invalid menu selection"),
44            Self::ValidationFailed { key, message } => {
45                write!(f, "validation failed for '{key}': {message}")
46            }
47            Self::InvalidActionKey(key) => write!(f, "invalid action key: {key}"),
48            Self::ReservedActionKey(key) => write!(f, "reserved action key: {key}"),
49            Self::DuplicateActionKey(key) => write!(f, "duplicate action key: {key}"),
50            Self::ActionFailed { key, message } => {
51                write!(f, "action '{key}' failed: {message}")
52            }
53        }
54    }
55}
56
57impl Error for ConfigEasyError {
58    fn source(&self) -> Option<&(dyn Error + 'static)> {
59        match self {
60            Self::Store(error) => Some(error.as_ref()),
61            Self::Io(error) => Some(error),
62            Self::InvalidIdentifier(_)
63            | Self::InvalidSelection
64            | Self::ValidationFailed { .. }
65            | Self::InvalidActionKey(_)
66            | Self::ReservedActionKey(_)
67            | Self::DuplicateActionKey(_)
68            | Self::ActionFailed { .. } => None,
69        }
70    }
71}
72
73impl From<std::io::Error> for ConfigEasyError {
74    fn from(error: std::io::Error) -> Self {
75        Self::Io(error)
76    }
77}
78
79impl From<Box<dyn Error + Send + Sync>> for ConfigEasyError {
80    fn from(error: Box<dyn Error + Send + Sync>) -> Self {
81        Self::Store(error)
82    }
83}