Skip to main content

euv_cli/error/
impl.rs

1use crate::*;
2
3/// Implements `Display` for `EuvError` to provide human-readable error messages.
4impl std::fmt::Display for EuvError {
5    /// Formats the error message based on the variant.
6    ///
7    /// # Arguments
8    ///
9    /// - `&mut std::fmt::Formatter` - The formatter.
10    ///
11    /// # Returns
12    ///
13    /// - `std::fmt::Result` - The result of the formatting operation.
14    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
15        match self {
16            EuvError::Io { message, error } => write!(formatter, "{message}: {error}"),
17            EuvError::IoPath {
18                message,
19                path,
20                error,
21            } => {
22                write!(formatter, "{message} '{}': {error}", path.display())
23            }
24            EuvError::Utf8 { message, error } => write!(formatter, "{message}: {error}"),
25            EuvError::Server(message) => write!(formatter, "{message}"),
26            EuvError::Message(message) => write!(formatter, "{message}"),
27            EuvError::Watch(error) => write!(formatter, "{error}"),
28        }
29    }
30}
31
32/// Implements `Error` for `EuvError` so it integrates with the Rust error ecosystem.
33impl std::error::Error for EuvError {
34    /// Returns the lower-level source of this error, if any.
35    ///
36    /// # Returns
37    ///
38    /// - `Option<&(dyn std::error::Error + 'static)>` - The underlying error source.
39    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
40        match self {
41            EuvError::Io { error, .. } => Some(error),
42            EuvError::IoPath { error, .. } => Some(error),
43            EuvError::Utf8 { error, .. } => Some(error),
44            EuvError::Watch(error) => Some(error),
45            EuvError::Server(_) | EuvError::Message(_) => None,
46        }
47    }
48}
49
50/// Implements `From<notify::Error>` for `EuvError` to enable `?` operator conversion.
51impl From<notify::Error> for EuvError {
52    /// Converts a `notify::Error` into `EuvError::Watch`.
53    ///
54    /// # Arguments
55    ///
56    /// - `notify::Error` - The file watcher error to convert.
57    ///
58    /// # Returns
59    ///
60    /// - `EuvError` - The converted `Watch` variant.
61    fn from(error: notify::Error) -> Self {
62        EuvError::Watch(error)
63    }
64}