Skip to main content

dark_light/
error.rs

1use std::fmt::Display;
2
3/// An error that can occur when detecting the system theme mode.
4#[derive(Debug)]
5pub enum Error {
6    /// If an I/O error occurs.
7    Io(std::io::Error),
8    /// If the XDG Desktop Portal could not be communicated with.
9    XdgDesktopPortal(String),
10    /// If the timeout is reached.
11    Timeout,
12    /// Failed to get persistent domain for Apple Global Domain.
13    PersistentDomainFailed,
14    /// If the window could not be found.
15    WindowNotFound,
16    /// If the media query could not be executed.
17    MediaQueryFailed,
18    /// If the media query is not supported.
19    MediaQueryNotSupported,
20    /// If watching for theme changes is not supported on this platform.
21    Unsupported,
22}
23
24impl Display for Error {
25    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
26        match self {
27            Error::Io(error) => write!(f, "I/O error: {}", error),
28            Error::XdgDesktopPortal(err) => write!(f, "XDG Desktop Portal error: {}", err),
29            Error::Timeout => write!(f, "Timeout reached"),
30            Error::PersistentDomainFailed => {
31                write!(f, "Failed to get persistent domain for Apple Global Domain")
32            }
33            Error::WindowNotFound => write!(f, "Window not found"),
34            Error::MediaQueryFailed => write!(f, "Media query failed"),
35            Error::MediaQueryNotSupported => write!(f, "Media query not supported"),
36            Error::Unsupported => write!(
37                f,
38                "Watching for theme changes is not supported on this platform"
39            ),
40        }
41    }
42}
43
44impl std::error::Error for Error {}
45
46impl From<std::io::Error> for Error {
47    fn from(error: std::io::Error) -> Self {
48        Error::Io(error)
49    }
50}