use crate::error::Error;
use crate::request::notification::{DefaultAlert, DefaultSound, NotificationOptions, WebPushAlert};
use erased_serde::Serialize;
use serde_json::{self, Value};
use std::collections::BTreeMap;
use std::fmt::Debug;
#[derive(Debug, Clone, Serialize)]
pub struct Payload<'a> {
#[serde(skip)]
pub options: NotificationOptions<'a>,
#[serde(skip)]
pub device_token: &'a str,
pub aps: APS<'a>,
#[serde(flatten)]
pub data: BTreeMap<&'a str, Value>,
}
pub trait PayloadLike: serde::Serialize + Debug {
#[allow(clippy::wrong_self_convention)]
fn to_json_string(&self) -> Result<String, Error> {
Ok(serde_json::to_string(&self)?)
}
fn get_device_token(&self) -> &str;
fn get_options(&self) -> &NotificationOptions;
}
impl<'a> PayloadLike for Payload<'a> {
fn get_device_token(&self) -> &'a str {
self.device_token
}
fn get_options(&self) -> &NotificationOptions {
&self.options
}
}
impl<'a> Payload<'a> {
pub fn add_custom_data(&mut self, root_key: &'a str, data: &dyn Serialize) -> Result<&mut Self, Error> {
self.data.insert(root_key, serde_json::to_value(data)?);
Ok(self)
}
}
#[derive(Serialize, Default, Debug, Clone)]
#[serde(rename_all = "kebab-case")]
#[allow(clippy::upper_case_acronyms)]
pub struct APS<'a> {
#[serde(skip_serializing_if = "Option::is_none")]
pub alert: Option<APSAlert<'a>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub badge: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub sound: Option<APSSound<'a>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub thread_id: Option<&'a str>,
#[serde(skip_serializing_if = "Option::is_none")]
pub content_available: Option<u8>,
#[serde(skip_serializing_if = "Option::is_none")]
pub category: Option<&'a str>,
#[serde(skip_serializing_if = "Option::is_none")]
pub mutable_content: Option<u8>,
#[serde(skip_serializing_if = "Option::is_none")]
pub url_args: Option<&'a [&'a str]>,
}
#[derive(Serialize, Debug, Clone)]
#[serde(untagged)]
pub enum APSAlert<'a> {
Default(DefaultAlert<'a>),
WebPush(WebPushAlert<'a>),
Body(&'a str),
}
#[derive(Serialize, Debug, Clone)]
#[serde(untagged)]
pub enum APSSound<'a> {
Critical(DefaultSound<'a>),
Sound(&'a str),
}