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
use std::io::Error;
use std::io::ErrorKind;
use crate::pushover::constants;
use super::PushoverSound;
use super::AttachmentMessage;
pub struct AttachmentMessageBuilder {
build: AttachmentMessage,
}
impl AttachmentMessageBuilder {
pub fn new(user_key: &str, application_token: &str, message: &str) -> Self {
let mut build = AttachmentMessage::default();
build.user_key = user_key.to_owned();
build.app_token = application_token.to_owned();
build.message = message.to_owned();
AttachmentMessageBuilder {
build,
}
}
pub fn modify_message(mut self, message: &str) -> AttachmentMessageBuilder {
if message.trim().len() == 0 {
return self;
}
self.build.message = message.to_owned();
self
}
pub fn add_title(mut self, title: &str) -> AttachmentMessageBuilder {
if title.trim().len() == 0 {
self.build.title = None;
}
self.build.title = Some(title.to_owned());
self
}
pub fn remove_title(mut self) -> AttachmentMessageBuilder {
self.build.title = None;
self
}
pub fn add_url(mut self, url: &str, url_title: Option<&str>) -> AttachmentMessageBuilder {
if url.trim().len() == 0 {
self.build.url = None;
self.build.url_title = None;
return self;
}
self.build.url = Some(url.to_owned());
if url_title.is_some() {
self.build.url_title = Some(url_title.unwrap().to_owned());
}
self
}
pub fn remove_url(mut self) -> AttachmentMessageBuilder {
self.build.url = None;
self.build.url_title = None;
self
}
pub fn set_priority(mut self, priority: i8) -> AttachmentMessageBuilder {
if priority < -2 || priority > 2 {
self.build.priority = Some("0".into());
return self;
}
self.build.priority = Some(priority.to_string());
self
}
pub fn remove_priority(mut self) -> AttachmentMessageBuilder {
self.build.priority = Some("0".into());
self
}
pub fn set_sound(mut self, sound: PushoverSound) -> AttachmentMessageBuilder {
self.build.sound = Some(sound.to_string());
self
}
pub fn remove_sound(mut self) -> AttachmentMessageBuilder {
self.build.sound = None;
self
}
pub fn set_timestamp(mut self, unix_timestamp: u64) -> AttachmentMessageBuilder {
self.build.timestamp = Some(unix_timestamp.to_string());
self
}
pub fn remove_timestamp(mut self) -> AttachmentMessageBuilder {
self.build.timestamp = None;
self
}
pub fn add_device(mut self, device_name: &str) -> AttachmentMessageBuilder {
type Devices = Vec<String>;
if device_name.trim().len() == 0 {
return self;
}
if self.build.devices.is_none() {
self.build.devices = Some(vec!());
} else {
let clone: Vec<String> = self.build.devices.clone().unwrap();
if clone.contains(&device_name.into()) {
return self;
}
}
let mut replacement_list: Devices = self.build.devices.clone().unwrap();
replacement_list.push(device_name.to_owned());
self.build.devices = Some(replacement_list);
self
}
pub fn set_devices(mut self, device_names: Vec<&str>) -> AttachmentMessageBuilder {
let device_names = device_names.iter().map(|x| x.to_string()).collect::<Vec<String>>();
self.build.devices = Some(device_names);
self
}
pub fn merge_devices(mut self, device_names: Vec<&str>) -> AttachmentMessageBuilder {
for name in device_names {
self = self.add_device(name);
}
self
}
pub fn clear_devices_list(mut self) -> AttachmentMessageBuilder {
self.build.devices = None;
self
}
pub fn set_attachment(mut self, attachment_path: String) -> AttachmentMessageBuilder {
if attachment_path.trim().len() == 0 {
return self;
}
self.build.attachment = attachment_path;
self
}
pub fn build(self) -> Result<AttachmentMessage, Box<dyn std::error::Error>> {
if self.build.app_token.is_empty() {
return Err(Box::new(Error::new(ErrorKind::InvalidInput, "Application token is empty")));
}
if self.build.user_key.is_empty() {
return Err(Box::new(Error::new(ErrorKind::InvalidInput, "User key is empty")));
}
if self.build.message.is_empty() {
return Err(Box::new(Error::new(ErrorKind::InvalidInput, "Message is empty")));
}
if self.build.attachment.is_empty() {
return Err(Box::new(Error::new(ErrorKind::InvalidInput, "Attachment is empty")));
}
if !std::path::Path::new(&self.build.attachment).exists() {
return Err(Box::new(Error::new(ErrorKind::InvalidInput, "Attachment file doesn't exist.")));
}
let file_size = std::fs::metadata(&self.build.attachment).unwrap().len();
if file_size > constants::PUSHOVER_API_ATTACHMENT_MAX_SIZE_BYTES {
return Err(Box::new(Error::new(ErrorKind::InvalidInput, "Attachment file is too large. (> 2621440 bytes)")));
}
Ok(self.build.clone())
}
}