use crate::error::Error;
use std::fmt;
#[derive(Debug, Clone)]
pub struct CollapseId<'a> {
pub value: &'a str,
}
impl<'a> CollapseId<'a> {
pub fn new(value: &'a str) -> Result<CollapseId<'a>, Error> {
if value.len() > 64 {
Err(Error::InvalidOptions(String::from(
"The collapse-id is too big. Maximum 64 bytes.",
)))
} else {
Ok(CollapseId { value })
}
}
}
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
pub enum PushType {
#[default]
Alert,
Background,
Location,
Voip,
FileProvider,
Mdm,
LiveActivity,
PushToTalk,
}
impl fmt::Display for PushType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
PushType::Alert => "alert",
PushType::Background => "background",
PushType::Location => "location",
PushType::Voip => "voip",
PushType::FileProvider => "fileprovider",
PushType::Mdm => "mdm",
PushType::LiveActivity => "liveactivity",
PushType::PushToTalk => "pushtotalk",
})
}
}
#[derive(Debug, Default, Clone)]
pub struct NotificationOptions<'a> {
pub apns_id: Option<&'a str>,
pub apns_push_type: Option<PushType>,
pub apns_expiration: Option<u64>,
pub apns_priority: Option<Priority>,
pub apns_topic: Option<&'a str>,
pub apns_collapse_id: Option<CollapseId<'a>>,
}
#[derive(Debug, Clone)]
pub enum Priority {
High,
Normal,
}
impl fmt::Display for Priority {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let priority = match self {
Priority::High => "10",
Priority::Normal => "5",
};
write!(f, "{}", priority)
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::str;
#[test]
fn test_collapse_id_under_64_chars() {
let collapse_id = CollapseId::new("foo").unwrap();
assert_eq!("foo", collapse_id.value);
}
#[test]
fn test_collapse_id_over_64_chars() {
let mut long_string = Vec::with_capacity(65);
long_string.extend_from_slice(&[65; 65]);
let collapse_id = CollapseId::new(str::from_utf8(&long_string).unwrap());
assert!(collapse_id.is_err());
}
}