use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub struct NotificationAction {
pub label: String,
pub link: String,
#[serde(rename = "type")]
pub method: String,
#[serde(default, rename = "primary")]
pub is_primary: bool,
}
impl NotificationAction {
pub const fn new(label: String, link: String, method: String, is_primary: bool) -> Self {
Self {
label,
link,
method,
is_primary,
}
}
pub fn label(&self) -> &str {
&self.label
}
pub fn link(&self) -> &str {
&self.link
}
pub fn method(&self) -> &str {
&self.method
}
pub const fn is_primary(&self) -> bool {
self.is_primary
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parses_action_from_json() {
let json = r#"{
"label": "Accept",
"link": "https://example.com/share/accept",
"type": "POST",
"primary": true
}"#;
let action: NotificationAction = serde_json::from_str(json).unwrap();
assert_eq!(action.label(), "Accept");
assert_eq!(action.link(), "https://example.com/share/accept");
assert_eq!(action.method(), "POST");
assert!(action.is_primary());
}
#[test]
fn defaults_is_primary_to_false() {
let json = r#"{
"label": "Decline",
"link": "https://example.com/share/decline",
"type": "DELETE"
}"#;
let action: NotificationAction = serde_json::from_str(json).unwrap();
assert_eq!(action.label(), "Decline");
assert!(!action.is_primary());
}
#[test]
fn serializes_matching_wire_field_names() {
let action = NotificationAction::new(
"View".to_owned(),
"https://example.com/view".to_owned(),
"GET".to_owned(),
false,
);
let value = serde_json::to_value(&action).unwrap();
assert_eq!(value["type"], "GET");
assert_eq!(value["primary"], false);
}
}