Debian Codegraph @

src/urgency.rs

raw symbols SCIP
1 use crate::error::ErrorKind;
2
3 /// Levels of Urgency.
4 ///
5 /// # Specification
6 /// > Developers must use their own judgement when deciding the urgency of a notification. Typically, if the majority of programs are using the same level for a specific type of urgency, other applications should follow them.
7 /// >
8 /// > For low and normal urgencies, server implementations may display the notifications how they choose. They should, however, have a sane expiration timeout dependent on the urgency level.
9 /// >
10 /// > **Critical notifications should not automatically expire**, as they are things that the user will most likely want to know about. They should only be closed when the user dismisses them, for example, by clicking on the notification.
11 ///
12 /// <cite> — see [Galago](http://www.galago-project.org/specs/notification/0.9/x320.html) or [Gnome](https://developer.gnome.org/notification-spec/#urgency-levels) specification.</cite>
13 ///
14 /// # Example
15 /// ```no_run
16 /// # use notify_rust::*;
17 /// # fn _doc() -> Result<(), Box<dyn std::error::Error>> {
18 /// Notification::new()
19 /// .summary("oh no")
20 /// .icon("dialog-warning")
21 /// .urgency(Urgency::Critical)
22 /// .show()?;
23 /// # Ok(())
24 /// # }
25 /// ```
26 ///
27 #[derive(Eq, PartialEq, Hash, Copy, Clone, Debug)]
28 pub enum Urgency {
29 /// The behavior for `Low` urgency depends on the notification server.
30 Low = 0,
31 /// The behavior for `Normal` urgency depends on the notification server.
32 Normal = 1,
33 /// A critical notification will not time out.
34 Critical = 2,
35 }
36
37 impl TryFrom<&str> for Urgency {
38 type Error = crate::error::Error;
39
40 #[rustfmt::skip]
41 fn try_from(string: &str) -> Result<Urgency, Self::Error> {
42 match string.to_lowercase().as_ref() {
43 "low" |
44 "lo" => Ok(Urgency::Low),
45 "normal" |
46 "medium" => Ok(Urgency::Normal),
47 "critical" |
48 "high" |
49 "hi" => Ok(Urgency::Critical),
50 _ => Err(ErrorKind::Conversion(format!("invalid input {string:?}")).into())
51 }
52 }
53 }
54
55 impl From<Option<u64>> for Urgency {
56 fn from(maybe_int: Option<u64>) -> Urgency {
57 match maybe_int {
58 Some(0) => Urgency::Low,
59 Some(x) if x >= 2 => Urgency::Critical,
60 _ => Urgency::Normal,
61 }
62 }
63 }
64
65 // TODO: remove this in v5.0
66 // #[cfg(not(feature = "server"))]
67 impl From<u64> for Urgency {
68 fn from(int: u64) -> Urgency {
69 match int {
70 0 => Urgency::Low,
71 1 => Urgency::Normal,
72 2..=u64::MAX => Urgency::Critical,
73 }
74 }
75 }
76
77 // // TODO: make this the default in v5.0
78 // #[cfg(feature = "server")]
79 // impl From<u8> for Urgency {
80 // fn from(int: u8) -> Urgency {
81 // match int {
82 // 0 => Urgency::Low,
83 // 1 => Urgency::Normal,
84 // 2..=std::u8::MAX => Urgency::Critical,
85 // }
86 // }
87 // }