nextcloud_notifications/
action.rs1use serde::{Deserialize, Serialize};
4
5#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
12pub struct NotificationAction {
13 pub label: String,
15
16 pub link: String,
18
19 #[serde(rename = "type")]
22 pub method: String,
23
24 #[serde(default, rename = "primary")]
26 pub is_primary: bool,
27}
28
29impl NotificationAction {
30 pub const fn new(label: String, link: String, method: String, is_primary: bool) -> Self {
32 Self {
33 label,
34 link,
35 method,
36 is_primary,
37 }
38 }
39
40 pub fn label(&self) -> &str {
42 &self.label
43 }
44
45 pub fn link(&self) -> &str {
47 &self.link
48 }
49
50 pub fn method(&self) -> &str {
52 &self.method
53 }
54
55 pub const fn is_primary(&self) -> bool {
57 self.is_primary
58 }
59}
60
61#[cfg(test)]
62mod tests {
63 use super::*;
64
65 #[test]
66 fn parses_action_from_json() {
67 let json = r#"{
68 "label": "Accept",
69 "link": "https://example.com/share/accept",
70 "type": "POST",
71 "primary": true
72 }"#;
73
74 let action: NotificationAction = serde_json::from_str(json).unwrap();
75 assert_eq!(action.label(), "Accept");
76 assert_eq!(action.link(), "https://example.com/share/accept");
77 assert_eq!(action.method(), "POST");
78 assert!(action.is_primary());
79 }
80
81 #[test]
82 fn defaults_is_primary_to_false() {
83 let json = r#"{
84 "label": "Decline",
85 "link": "https://example.com/share/decline",
86 "type": "DELETE"
87 }"#;
88
89 let action: NotificationAction = serde_json::from_str(json).unwrap();
90 assert_eq!(action.label(), "Decline");
91 assert!(!action.is_primary());
92 }
93
94 #[test]
95 fn serializes_matching_wire_field_names() {
96 let action = NotificationAction::new(
97 "View".to_owned(),
98 "https://example.com/view".to_owned(),
99 "GET".to_owned(),
100 false,
101 );
102
103 let value = serde_json::to_value(&action).unwrap();
104 assert_eq!(value["type"], "GET");
105 assert_eq!(value["primary"], false);
106 }
107}