From e135844c30b05be74a9df4582fb6c07d1ea5a648 Mon Sep 17 00:00:00 2001 From: Kim Bauters Date: Tue, 21 May 2024 14:35:45 +0100 Subject: [PATCH 1/2] rewrite devices to device (one) --- src/pushover/data/attachment_message.rs | 8 +-- .../data/attachment_message_builder.rs | 47 +++------------ src/pushover/data/message.rs | 8 +-- src/pushover/data/message_builder.rs | 47 +++------------ src/tests/github_actions_friendly.rs | 59 +++---------------- 5 files changed, 30 insertions(+), 139 deletions(-) diff --git a/src/pushover/data/attachment_message.rs b/src/pushover/data/attachment_message.rs index f40b6d5..d220ff1 100644 --- a/src/pushover/data/attachment_message.rs +++ b/src/pushover/data/attachment_message.rs @@ -30,8 +30,8 @@ pub struct AttachmentMessage { pub sound: Option, /// A Unix timestamp of your message's date and time to display to the user, rather than the time your message is received by our API pub timestamp: Option, // Year 2038 proof :p - /// A list of device names to send the push notifications to, if you want to limit the notification to certain devices. - pub devices: Option>, + /// A device name to send the push notifications to, if you want to limit the notification to a certain device. + pub device: Option, /// A TTL (Time to Live) in seconds, after which the message will be automatically deleted from the recipient's inbox. /// Setting *ttl* to None or 0 prevents this auto removal. pub ttl: Option, @@ -49,7 +49,7 @@ impl AttachmentMessage { .text("priority", self.priority.unwrap_or("".into())) .text("sound", self.sound.clone().unwrap_or("".into())) .text("timestamp", self.timestamp.unwrap_or("".into())) - .text("device", self.devices.clone().unwrap_or(vec!()).join(",")) + .text("device", self.device.unwrap_or("".into())) .text("ttl", self.ttl.unwrap_or(0).to_string()) .file("attachment", self.attachment.clone()) } @@ -68,7 +68,7 @@ impl Default for AttachmentMessage { priority: None, sound: None, timestamp: None, - devices: None, + device: None, ttl: None, } } diff --git a/src/pushover/data/attachment_message_builder.rs b/src/pushover/data/attachment_message_builder.rs index 196ba8d..72f85a7 100644 --- a/src/pushover/data/attachment_message_builder.rs +++ b/src/pushover/data/attachment_message_builder.rs @@ -1,5 +1,6 @@ use std::io::Error; use std::io::ErrorKind; +use crate::MessageBuilder; use crate::pushover::constants; @@ -156,48 +157,16 @@ impl AttachmentMessageBuilder { } /// Add a device name to send the notification to. - /// - /// Ignores if the device name is already in the list. - pub fn add_device(mut self, device_name: &str) -> AttachmentMessageBuilder { - type Devices = Vec; - - if device_name.trim().len() == 0 { - return self; - } - - if self.build.devices.is_none() { - self.build.devices = Some(vec!()); - } else { - let clone: Vec = 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 - } - - /// Overrides the current devices list with device_names - pub fn set_devices(mut self, device_names: Vec<&str>) -> AttachmentMessageBuilder { - let device_names = device_names.iter().map(|x| x.to_string()).collect::>(); - self.build.devices = Some(device_names); - self - } - - /// Merges the current devices list with device_names, duplicates are eliminated - pub fn merge_devices(mut self, device_names: Vec<&str>) -> AttachmentMessageBuilder { - for name in device_names { - self = self.add_device(name); - } + /// + /// Overrides the current device if a new device name is set. + pub fn set_device(mut self, device_name: &str) -> AttachmentMessageBuilder { + self.build.device = Some(device_name.to_string()); self } - pub fn clear_devices_list(mut self) -> AttachmentMessageBuilder { - self.build.devices = None; + /// Clears the device if set. + pub fn remove_device(mut self) -> AttachmentMessageBuilder { + self.build.device = None; self } diff --git a/src/pushover/data/message.rs b/src/pushover/data/message.rs index acdc9be..651c503 100644 --- a/src/pushover/data/message.rs +++ b/src/pushover/data/message.rs @@ -2,7 +2,7 @@ use serde::Serialize; #[derive(Debug, Clone, Serialize)] /** -A message to be used in conjuction with the send_pushover_request function. +A message to be used in conjunction with the send_pushover_request function. Note: It is preferred to create a Message through the MessageBuilder. **/ @@ -36,9 +36,9 @@ pub struct Message { /// A Unix timestamp of your message's date and time to display to the user, rather than the time your message is received by our API #[serde(skip_serializing_if = "Option::is_none")] pub timestamp: Option, // Year 2038 proof :p - /// A list of device names to send the push notifications to, if you want to limit the notification to certain devices. + /// A device name to send the push notifications to, if you want to limit the notification to a certain device. #[serde(skip_serializing_if = "Option::is_none")] - pub devices: Option>, + pub device: Option, /// A TTL (Time to Live) in seconds, after which the message will be automatically deleted from the recipient's inbox. /// Setting *ttl* to None or 0 prevents this auto removal. #[serde(skip_serializing_if = "Option::is_none")] @@ -57,7 +57,7 @@ impl Default for Message { priority: None, sound: None, timestamp: None, - devices: None, + device: None, ttl: None, } } diff --git a/src/pushover/data/message_builder.rs b/src/pushover/data/message_builder.rs index 57b11e1..4a87e0b 100644 --- a/src/pushover/data/message_builder.rs +++ b/src/pushover/data/message_builder.rs @@ -148,49 +148,16 @@ impl MessageBuilder { } /// Add a device name to send the notification to. - /// - /// Ignores if the device name is already in the list. - pub fn add_device(mut self, device_name: &str) -> MessageBuilder { - type Devices = Vec; - - if device_name.trim().len() == 0 { - return self; - } - - if self.build.devices.is_none() { - self.build.devices = Some(vec!()); - } else { - let clone: Vec = 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 - } - - /// Overrides the current devices list with device_names - pub fn set_devices(mut self, device_names: Vec<&str>) -> MessageBuilder { - let device_names = device_names.iter().map(|x| x.to_string()).collect::>(); - self.build.devices = Some(device_names); - self - } - - /// Merges the current devices list with device_names, duplicates are eliminated - pub fn merge_devices(mut self, device_names: Vec<&str>) -> MessageBuilder { - for name in device_names { - self = self.add_device(name); - } + /// + /// Overrides the current device if a new device name is set. + pub fn set_device(mut self, device_name: &str) -> MessageBuilder { + self.build.device = Some(device_name.to_string()); self } - /// Clears the devices list entirely - pub fn clear_devices_list(mut self) -> MessageBuilder { - self.build.devices = None; + /// Clears the device if set. + pub fn remove_device(mut self) -> MessageBuilder { + self.build.device = None; self } diff --git a/src/tests/github_actions_friendly.rs b/src/tests/github_actions_friendly.rs index 92dbdb0..d3e066d 100644 --- a/src/tests/github_actions_friendly.rs +++ b/src/tests/github_actions_friendly.rs @@ -1,5 +1,3 @@ -use std::vec; - use crate::{AttachmentMessageBuilder, Message, MessageBuilder, PushoverSound}; #[test] @@ -20,9 +18,8 @@ pub fn test_message_builder() { .set_priority(100) // "Error": Out of the [-2, 2] boundary -> Should be reset to 0 .set_sound(PushoverSound::CASHREGISTER) .set_timestamp(1635861224) - .add_device("device a") - .add_device("device b") - .add_device("device a") // Duplicate, should not be added + .set_device("device a") + .set_device("device b") // should overwrite existing device .build(); assert_eq!(mfull.user_key, "abc".to_owned()); @@ -34,56 +31,14 @@ pub fn test_message_builder() { assert_eq!(mfull.priority, Some(0)); assert_eq!(mfull.sound, Some("cashregister".to_owned())); assert_eq!(mfull.timestamp, Some(1635861224)); - assert_eq!(mfull.devices.as_ref().unwrap().len(), 2 as usize); - assert_eq!(mfull.devices, Some(vec!["device a".to_owned(), "device b".to_owned()])); - - /* Test devices list */ - /* Test set devices list */ - let devices_list: Vec<&str> = vec![ - "device a", - "device b", - "device c", - ]; - let mnext: Message = MessageBuilder::new("abc", "def", "testing") - .set_devices(devices_list.clone()) - .build(); - let list_from_mnext: Vec = mnext.devices.unwrap(); - - // It's ok to compare Vec with Vec<&str> - assert_eq!(list_from_mnext, devices_list); - - /* Test merge devices list */ - let devices_list_1: Vec<&str> = vec![ - "device a", - "device b", - ]; - let devices_list_2: Vec<&str> = vec![ - "device b", - "device c", - "device a", - ]; - let expected: Vec = vec! [ - "device a".to_owned(), - "device b".to_owned(), - "device c".to_owned(), - ]; - let mnext_merge: Message = MessageBuilder::new("abc", "def", "testing") - .set_devices(devices_list_1) - .merge_devices(devices_list_2) - .build(); - let list_from_mnext_merge: Vec = mnext_merge.devices.unwrap(); - assert_eq!(list_from_mnext_merge, expected); + assert_eq!(mfull.device, Some("device b".to_owned())); - /* Test clear devices list */ - let devices_list_toclear: Vec<&str> = vec![ - "device a", - "device b", - ]; + /* Test clear device */ let mnext_clear: Message = MessageBuilder::new("abc", "def", "testing") - .set_devices(devices_list_toclear) - .clear_devices_list() + .set_device("device a") + .remove_device() .build(); - assert_eq!(mnext_clear.devices, None); + assert_eq!(mnext_clear.device, None); } #[test] From 97f276279771c3d4e9c41709b43d1fb509fda25e Mon Sep 17 00:00:00 2001 From: Kim Bauters Date: Tue, 21 May 2024 14:57:39 +0100 Subject: [PATCH 2/2] remove unused use statement --- src/pushover/data/attachment_message_builder.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/pushover/data/attachment_message_builder.rs b/src/pushover/data/attachment_message_builder.rs index 72f85a7..d550efd 100644 --- a/src/pushover/data/attachment_message_builder.rs +++ b/src/pushover/data/attachment_message_builder.rs @@ -1,6 +1,5 @@ use std::io::Error; use std::io::ErrorKind; -use crate::MessageBuilder; use crate::pushover::constants;