colorparser_css/
error.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/// Represents errors that can occur when handling colors in Windows.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum ErrorKind {
    /// Error when the provided hex color format is invalid.
    InvalidHex,
    /// Error when the provided RGB color format is invalid.
    InvalidRgb,
    /// Error when the provided HSL color format is invalid.
    InvalidHsl,
    /// Error when the provided color function is invalid.
    InvalidFunction,
    /// error when the provided gradient format is invalid.
    InvalidGradient,
    /// error when the provided gradient coordinates is invalid.
    InvalidGradientCoordinates,
    /// Error when the provided darken format is invalid.
    InvalidDarken,
    /// Error when the provided lighten format is invalid.
    InvalidLighten,
    // Error when unknown.
    InvalidUnknown,
}

impl core::fmt::Display for ErrorKind {
    /// Formats the `ErrorKind` enum into a human-readable string for display purposes.
    ///
    /// This is used when the error kind is displayed directly (e.g., in an error message).
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::InvalidHex => write!(f, "invalid hex format"),
            Self::InvalidRgb => write!(f, "invalid rgb format"),
            Self::InvalidHsl => write!(f, "invalid hsl format"),
            Self::InvalidGradient => write!(f, "invalid gradient format"),
            Self::InvalidGradientCoordinates => write!(f, "invalid gradient coordinates format"),
            Self::InvalidDarken => write!(f, "invalid darken format"),
            Self::InvalidLighten => write!(f, "invalid lighten format"),
            Self::InvalidFunction => write!(f, "invalid color function"),
            Self::InvalidUnknown => write!(f, "invalid unknown format"),
        }
    }
}

#[derive(Clone)]
pub struct Error {
    kind: ErrorKind,
    message: String,
}

impl core::fmt::Debug for Error {
    fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        let mut debug = fmt.debug_struct("Error");
        debug
            .field("kind", &self.kind())
            .field("message", &self.message())
            .finish()
    }
}

impl Eq for Error {}

impl PartialEq for Error {
    fn eq(&self, other: &Self) -> bool {
        self.kind == other.kind
    }
}

impl core::hash::Hash for Error {
    fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
        self.kind.hash(state);
    }
}

impl PartialOrd for Error {
    fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Error {
    fn cmp(&self, other: &Self) -> core::cmp::Ordering {
        self.kind.cmp(&other.kind)
    }
}

impl Error {
    /// Creates a new `Error` instance with a specified kind and a message.
    /// If the message is empty, it will use an empty message for the error.
    ///
    /// # Parameters
    /// - `kind`: The type of error (e.g., `InvalidGradientCoordinates`, `InvalidAccent`).
    /// - `message`: A message describing the error. This can be an empty string.
    ///
    /// # Returns
    /// A new `Error` instance with the specified error kind and message.
    pub fn new<T: AsRef<str>>(kind: ErrorKind, message: T) -> Self {
        let message: &str = message.as_ref();
        Self {
            kind,
            message: message.to_string(),
        }
    }

    /// Retrieves the kind of the error.
    ///
    /// # Returns
    /// A reference to the `ErrorKind` variant that represents the type of error.
    pub fn kind(&self) -> ErrorKind {
        self.kind.clone()
    }

    /// Retrieves the error message, if provided.
    ///
    /// # Returns
    /// The error message as a string slice. If no message is provided, an empty string is returned.
    pub fn message(&self) -> String {
        self.message.clone()
    }
}

impl core::fmt::Display for Error {
    /// Formats the `Error` struct for user-facing display.
    ///
    /// If a message is provided, it includes the message along with the error kind.
    /// If no message is provided, only the error kind is displayed.
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        if self.message.is_empty() {
            write!(f, "{}", self.kind)
        } else {
            write!(f, "{} ({})", self.kind, self.message)
        }
    }
}

impl std::error::Error for Error {}

/// A custom `Result` type that returns `Error` in case of failure.
///
/// This type is used for handling errors related to shell operations. It wraps the standard `Result` type but replaces the error type with our custom `Error` type.
pub type Result<T> = core::result::Result<T, Error>;