pub mod create;
pub mod delete;
pub mod home_set;
pub mod list;
pub mod update;
use alloc::{format, string::String, vec::Vec};
use serde::{Deserialize, Serialize};
use crate::rfc4918::{
DISPLAYNAME, GETCTAG, Namespace, Property, RESOURCETYPE, prop_set_body, report_query_body,
};
#[derive(Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd, Deserialize, Serialize)]
pub struct Calendar {
pub id: String,
pub display_name: Option<String>,
pub description: Option<String>,
pub color: Option<String>,
pub ctag: Option<String>,
pub tz: Option<String>,
}
pub const CALDAV: Namespace = Namespace {
uri: "urn:ietf:params:xml:ns:caldav",
prefix: "C",
};
pub const INFIT: Namespace = Namespace {
uri: "http://inf-it.com/ns/ab/",
prefix: "I",
};
pub const CALENDAR: Property = Property {
ns: CALDAV,
local: "calendar",
};
pub const CALENDAR_HOME_SET: Property = Property {
ns: CALDAV,
local: "calendar-home-set",
};
pub const CALENDAR_DESCRIPTION: Property = Property {
ns: CALDAV,
local: "calendar-description",
};
pub const CALENDAR_TIMEZONE: Property = Property {
ns: CALDAV,
local: "calendar-timezone",
};
pub const CALENDAR_DATA: Property = Property {
ns: CALDAV,
local: "calendar-data",
};
pub const CALENDAR_COLOR: Property = Property {
ns: INFIT,
local: "calendar-color",
};
pub const CALENDAR_QUERY: Property = Property {
ns: CALDAV,
local: "calendar-query",
};
pub const MKCALENDAR: Property = Property {
ns: CALDAV,
local: "mkcalendar",
};
pub const LIST_PROPS: &[Property] = &[
RESOURCETYPE,
DISPLAYNAME,
CALENDAR_DESCRIPTION,
CALENDAR_COLOR,
GETCTAG,
CALENDAR_TIMEZONE,
];
pub fn join_path(home: &str, id: &str) -> String {
let home = home.trim_end_matches('/');
let id = id.trim_start_matches('/');
format!("{home}/{id}/")
}
pub fn mkcalendar_body(set: &[(Property, &str)]) -> Vec<u8> {
prop_set_body(MKCALENDAR, set)
}
pub fn property_set(calendar: &Calendar) -> Vec<(Property, &str)> {
let mut set = Vec::new();
if let Some(name) = &calendar.display_name {
set.push((DISPLAYNAME, name.as_str()));
}
if let Some(color) = &calendar.color {
set.push((CALENDAR_COLOR, color.as_str()));
}
if let Some(description) = &calendar.description {
set.push((CALENDAR_DESCRIPTION, description.as_str()));
}
set
}
pub fn calendar_query_body(props: &[Property], comp_filter: &str) -> Vec<u8> {
let filter = format!(
"<C:filter><C:comp-filter name=\"VCALENDAR\">{comp_filter}</C:comp-filter></C:filter>"
);
report_query_body(CALENDAR_QUERY, &[CALDAV], props, &filter)
}