tray_icon/
tray_icon_id.rs

1use std::{convert::Infallible, str::FromStr};
2
3/// An unique id that is associated with a tray icon.
4#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Default, Hash)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6pub struct TrayIconId(pub String);
7
8impl TrayIconId {
9    /// Create a new tray icon id.
10    pub fn new<S: AsRef<str>>(id: S) -> Self {
11        Self(id.as_ref().to_string())
12    }
13}
14
15impl AsRef<str> for TrayIconId {
16    fn as_ref(&self) -> &str {
17        self.0.as_ref()
18    }
19}
20
21impl<T: ToString> From<T> for TrayIconId {
22    fn from(value: T) -> Self {
23        Self::new(value.to_string())
24    }
25}
26
27impl FromStr for TrayIconId {
28    type Err = Infallible;
29
30    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
31        Ok(Self::new(s))
32    }
33}
34
35impl PartialEq<&str> for TrayIconId {
36    fn eq(&self, other: &&str) -> bool {
37        self.0 == *other
38    }
39}
40
41impl PartialEq<&str> for &TrayIconId {
42    fn eq(&self, other: &&str) -> bool {
43        self.0 == *other
44    }
45}
46
47impl PartialEq<String> for TrayIconId {
48    fn eq(&self, other: &String) -> bool {
49        self.0 == *other
50    }
51}
52
53impl PartialEq<String> for &TrayIconId {
54    fn eq(&self, other: &String) -> bool {
55        self.0 == *other
56    }
57}
58
59impl PartialEq<&String> for TrayIconId {
60    fn eq(&self, other: &&String) -> bool {
61        self.0 == **other
62    }
63}
64
65impl PartialEq<&TrayIconId> for TrayIconId {
66    fn eq(&self, other: &&TrayIconId) -> bool {
67        other.0 == self.0
68    }
69}
70
71#[cfg(test)]
72mod test {
73    use crate::TrayIconId;
74
75    #[test]
76    fn is_eq() {
77        assert_eq!(TrayIconId::new("t"), "t",);
78        assert_eq!(TrayIconId::new("t"), String::from("t"));
79        assert_eq!(TrayIconId::new("t"), &String::from("t"));
80        assert_eq!(TrayIconId::new("t"), TrayIconId::new("t"));
81        assert_eq!(TrayIconId::new("t"), &TrayIconId::new("t"));
82        assert_eq!(&TrayIconId::new("t"), &TrayIconId::new("t"));
83        assert_eq!(TrayIconId::new("t").as_ref(), "t");
84    }
85}