use jmap_types::{Id, UTCDate};
use serde::{Deserialize, Serialize};
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct VacationResponse {
pub id: Id,
pub is_enabled: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub from_date: Option<UTCDate>,
#[serde(skip_serializing_if = "Option::is_none")]
pub to_date: Option<UTCDate>,
#[serde(skip_serializing_if = "Option::is_none")]
pub subject: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub text_body: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub html_body: Option<String>,
}
impl VacationResponse {
pub fn new(id: Id, is_enabled: bool) -> Self {
Self {
id,
is_enabled,
from_date: None,
to_date: None,
subject: None,
text_body: None,
html_body: None,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn vacation_response_full_roundtrip() {
let json = r#"{"id":"singleton","isEnabled":true,"fromDate":"2024-06-01T00:00:00Z","toDate":"2024-06-30T23:59:59Z","subject":"Out of office","textBody":"I am out of the office.","htmlBody":"<p>I am out of the office.</p>"}"#;
let vr: VacationResponse = serde_json::from_str(json).expect("must parse");
assert_eq!(vr.id, "singleton");
assert!(vr.is_enabled);
assert_eq!(
vr.from_date.as_ref().map(|d| d.as_ref()),
Some("2024-06-01T00:00:00Z")
);
assert_eq!(vr.subject.as_deref(), Some("Out of office"));
assert_eq!(vr.text_body.as_deref(), Some("I am out of the office."));
let back = serde_json::to_string(&vr).expect("serialize");
assert_eq!(back, json);
}
#[test]
fn vacation_response_minimal_roundtrip() {
let json = r#"{"id":"singleton","isEnabled":false}"#;
let vr: VacationResponse = serde_json::from_str(json).expect("must parse");
assert_eq!(vr.id, "singleton");
assert!(!vr.is_enabled);
assert!(vr.from_date.is_none());
assert!(vr.to_date.is_none());
assert!(vr.subject.is_none());
assert!(vr.text_body.is_none());
assert!(vr.html_body.is_none());
let back = serde_json::to_string(&vr).expect("serialize");
assert_eq!(back, json);
}
#[test]
fn vacation_response_none_fields_omitted() {
let vr = VacationResponse {
id: Id::from("singleton"),
is_enabled: false,
from_date: None,
to_date: None,
subject: None,
text_body: None,
html_body: None,
};
let json = serde_json::to_string(&vr).expect("serialize");
assert!(!json.contains("fromDate"), "fromDate must be absent");
assert!(!json.contains("toDate"), "toDate must be absent");
assert!(!json.contains("subject"), "subject must be absent");
assert!(!json.contains("textBody"), "textBody must be absent");
assert!(!json.contains("htmlBody"), "htmlBody must be absent");
}
}