1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct NtfyMsg {
    pub topic: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub message: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tags: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub priority: Option<NtfyPriority>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub attach: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub filename: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub click: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub action: Option<Vec<NtfyAction>>,
}

impl NtfyMsg {
    pub fn new(topic: &str) -> NtfyMsg {
        NtfyMsg {
            topic: String::from(topic),
            message: None,
            title: None,
            tags: None,
            priority: None,
            attach: None,
            filename: None,
            click: None,
            action: None,
        }
    }

    pub fn builder(topic: &str) -> NtfyMsgBuilder {
        NtfyMsgBuilder::new(topic)
    }
}

pub struct NtfyMsgBuilder {
    msg: NtfyMsg,
}

impl NtfyMsgBuilder {
    pub fn new(topic: &str) -> NtfyMsgBuilder {
        NtfyMsgBuilder {
            msg: NtfyMsg::new(topic),
        }
    }

    pub fn topic(mut self, topic: &str) -> NtfyMsgBuilder {
        self.msg.topic = String::from(topic);
        self
    }

    pub fn message(mut self, message: &str) -> NtfyMsgBuilder {
        self.msg.message = Some(String::from(message));
        self
    }

    pub fn title(mut self, title: &str) -> NtfyMsgBuilder {
        self.msg.title = Some(String::from(title));
        self
    }

    pub fn tags(mut self, tags: Vec<String>) -> NtfyMsgBuilder {
        self.msg.tags = Some(tags);
        self
    }

    pub fn add_tag(mut self, tag: &str) -> NtfyMsgBuilder {
        if self.msg.tags.is_none() {
            self.msg.tags = Some(vec![String::from(tag)]);
        } else {
            self.msg.tags.as_mut().unwrap().push(String::from(tag));
        }
        self
    }

    pub fn priority(mut self, priority: NtfyPriority) -> NtfyMsgBuilder {
        self.msg.priority = Some(priority);
        self
    }

    pub fn attach(mut self, attach: &str) -> NtfyMsgBuilder {
        self.msg.attach = Some(String::from(attach));
        self
    }

    pub fn filename(mut self, filename: &str) -> NtfyMsgBuilder {
        self.msg.filename = Some(String::from(filename));
        self
    }

    pub fn click(mut self, click: &str) -> NtfyMsgBuilder {
        self.msg.click = Some(String::from(click));
        self
    }

    pub fn actions(mut self, actions: Vec<NtfyAction>) -> NtfyMsgBuilder {
        self.msg.action = Some(actions);
        self
    }

    pub fn add_action(mut self, action: NtfyAction) -> NtfyMsgBuilder {
        if self.msg.action.is_none() {
            self.msg.action = Some(vec![action]);
        } else {
            self.msg.action.as_mut().unwrap().push(action);
        }
        self
    }

    pub fn build(self) -> NtfyMsg {
        self.msg
    }
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct NtfyAction {
    pub action: NtfyActionType,
    pub label: String,
    pub url: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub clear: Option<bool>,
}

impl NtfyAction {
    pub fn new(label: String, url: String) -> NtfyAction {
        NtfyAction {
            action: NtfyActionType::View,
            label,
            url,
            clear: None,
        }
    }
}

#[repr(u8)]
#[non_exhaustive]
#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
#[serde(from = "u8")]
#[serde(into = "u8")]
pub enum NtfyPriority {
    Minimum,
    Low,
    Default,
    High,
    Maximum,
}

impl Default for NtfyPriority {
    fn default() -> Self {
        NtfyPriority::Default
    }
}

impl From<NtfyPriority> for u8 {
    fn from(priority: NtfyPriority) -> Self {
        match priority {
            NtfyPriority::Minimum => 1,
            NtfyPriority::Low => 2,
            NtfyPriority::Default => 3,
            NtfyPriority::High => 4,
            NtfyPriority::Maximum => 5,
        }
    }
}

impl From<u8> for NtfyPriority {
    fn from(value: u8) -> Self {
        match value {
            1 => NtfyPriority::Minimum,
            2 => NtfyPriority::Low,
            4 => NtfyPriority::High,
            5 => NtfyPriority::Maximum,
            _ => NtfyPriority::Default,
        }
    }
}

#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum NtfyActionType {
    View,
}

impl Default for NtfyActionType {
    fn default() -> Self {
        NtfyActionType::View
    }
}

// Responses

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct NtfyResponse {
    pub id: String,
    pub time: u64,
    pub event: String,
    pub topic: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub message: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tags: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub priority: Option<NtfyPriority>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub click: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub action: Option<Vec<NtfyAction>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub attachment: Option<NtfyAttachment>,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct NtfyAttachment {
    pub name: String,
    pub url: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub r#type: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub size: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub expires: Option<u64>,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct NtfyErrorResponse {
    pub code: u64,
    pub http: u16,
    pub error: String,
    pub link: String,
}