rtdlib/types/
notification.rs

1
2use crate::types::*;
3use crate::errors::*;
4use uuid::Uuid;
5
6
7
8
9/// Contains information about a notification
10#[derive(Debug, Clone, Default, Serialize, Deserialize)]
11pub struct Notification {
12  #[doc(hidden)]
13  #[serde(rename(serialize = "@type", deserialize = "@type"))]
14  td_name: String,
15  #[doc(hidden)]
16  #[serde(rename(serialize = "@extra", deserialize = "@extra"))]
17  extra: Option<String>,
18  /// Unique persistent identifier of this notification
19  id: i64,
20  /// Notification date
21  date: i64,
22  /// True, if the notification was initially silent
23  is_silent: bool,
24  /// Notification type
25  #[serde(rename(serialize = "type", deserialize = "type"))] type_: NotificationType,
26  
27}
28
29impl RObject for Notification {
30  #[doc(hidden)] fn td_name(&self) -> &'static str { "notification" }
31  #[doc(hidden)] fn extra(&self) -> Option<String> { self.extra.clone() }
32  fn to_json(&self) -> RTDResult<String> { Ok(serde_json::to_string(self)?) }
33}
34
35
36
37impl Notification {
38  pub fn from_json<S: AsRef<str>>(json: S) -> RTDResult<Self> { Ok(serde_json::from_str(json.as_ref())?) }
39  pub fn builder() -> RTDNotificationBuilder {
40    let mut inner = Notification::default();
41    inner.td_name = "notification".to_string();
42    inner.extra = Some(Uuid::new_v4().to_string());
43    RTDNotificationBuilder { inner }
44  }
45
46  pub fn id(&self) -> i64 { self.id }
47
48  pub fn date(&self) -> i64 { self.date }
49
50  pub fn is_silent(&self) -> bool { self.is_silent }
51
52  pub fn type_(&self) -> &NotificationType { &self.type_ }
53
54}
55
56#[doc(hidden)]
57pub struct RTDNotificationBuilder {
58  inner: Notification
59}
60
61impl RTDNotificationBuilder {
62  pub fn build(&self) -> Notification { self.inner.clone() }
63
64   
65  pub fn id(&mut self, id: i64) -> &mut Self {
66    self.inner.id = id;
67    self
68  }
69
70   
71  pub fn date(&mut self, date: i64) -> &mut Self {
72    self.inner.date = date;
73    self
74  }
75
76   
77  pub fn is_silent(&mut self, is_silent: bool) -> &mut Self {
78    self.inner.is_silent = is_silent;
79    self
80  }
81
82   
83  pub fn type_<T: AsRef<NotificationType>>(&mut self, type_: T) -> &mut Self {
84    self.inner.type_ = type_.as_ref().clone();
85    self
86  }
87
88}
89
90impl AsRef<Notification> for Notification {
91  fn as_ref(&self) -> &Notification { self }
92}
93
94impl AsRef<Notification> for RTDNotificationBuilder {
95  fn as_ref(&self) -> &Notification { &self.inner }
96}
97
98
99