#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Unsupported {
TrayAnchor,
ClientPositioning,
}
impl std::fmt::Display for Unsupported {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Unsupported::TrayAnchor => f.write_str("tray-anchored styled popup"),
Unsupported::ClientPositioning => f.write_str("client-side toplevel positioning"),
}
}
}
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
Unsupported(Unsupported),
BadIcon(String),
TrayInstall(String),
MainThread,
ThreadSpawn(String),
Platform(String),
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::Unsupported(u) => write!(f, "unsupported on this platform: {u}"),
Error::BadIcon(m) => write!(f, "bad icon: {m}"),
Error::TrayInstall(m) => write!(f, "tray install failed: {m}"),
Error::MainThread => f.write_str("must be created on the main thread"),
Error::ThreadSpawn(m) => write!(f, "failed to spawn the muri tray thread: {m}"),
Error::Platform(m) => write!(f, "platform error: {m}"),
}
}
}
impl std::error::Error for Error {}
pub type Result<T> = std::result::Result<T, Error>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn structured_variants_display_sensibly() {
assert_eq!(
Error::TrayInstall("Shell_NotifyIcon(NIM_ADD) failed".into()).to_string(),
"tray install failed: Shell_NotifyIcon(NIM_ADD) failed"
);
assert_eq!(
Error::MainThread.to_string(),
"must be created on the main thread"
);
assert_eq!(
Error::ThreadSpawn("resource limit".into()).to_string(),
"failed to spawn the muri tray thread: resource limit"
);
assert_eq!(
Error::BadIcon("empty".into()).to_string(),
"bad icon: empty"
);
assert_eq!(
Error::Unsupported(Unsupported::TrayAnchor).to_string(),
"unsupported on this platform: tray-anchored styled popup"
);
}
#[cfg(feature = "muda-compat")]
#[test]
fn structured_kinds_map_onto_the_compat_error_surface() {
use crate::compat::muda::Error as MudaError;
assert!(matches!(
MudaError::from(Error::TrayInstall("x".into())),
MudaError::Platform(_)
));
assert!(matches!(
MudaError::from(Error::MainThread),
MudaError::Platform(_)
));
assert!(matches!(
MudaError::from(Error::BadIcon("y".into())),
MudaError::BadIcon(_)
));
}
}