Skip to main content

ldtray/
error.rs

1use std::fmt;
2
3/// Errors produced by `ldtray`.
4///
5/// The most important variants for daemons are [`Error::Unsupported`] and
6/// [`Error::LibraryLoad`]: both mean "no tray here, but nothing is wrong with
7/// your program". Treating them as non-fatal is the intended usage.
8#[derive(Debug)]
9#[non_exhaustive]
10pub enum Error {
11    /// The current platform has no tray backend, or no display/session is
12    /// available (for example a headless server, or an unknown target OS).
13    Unsupported,
14    /// A required platform library could not be loaded at runtime. The string
15    /// carries the underlying loader message. This is the expected error on a
16    /// machine where the GUI stack simply is not installed.
17    LibraryLoad(String),
18    /// The backend was reachable but rejected an operation. The string carries a
19    /// human-readable description.
20    Backend(String),
21    /// The event loop is no longer running, so a [`crate::TrayHandle`] command
22    /// could not be delivered.
23    Disconnected,
24}
25
26impl fmt::Display for Error {
27    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28        match self {
29            Error::Unsupported => f.write_str("tray is not supported in this environment"),
30            Error::LibraryLoad(msg) => write!(f, "failed to load platform library: {msg}"),
31            Error::Backend(msg) => write!(f, "tray backend error: {msg}"),
32            Error::Disconnected => f.write_str("tray event loop is no longer running"),
33        }
34    }
35}
36
37impl std::error::Error for Error {}
38
39/// Convenience alias for results returned by this crate.
40pub type Result<T> = std::result::Result<T, Error>;