io_webdav/rfc4791/calendar/
list.rs1use alloc::{collections::BTreeSet, string::ToString};
47
48use log::trace;
49use url::Url;
50
51use crate::{
52 coroutine::*,
53 rfc4791::calendar::{
54 CALENDAR, CALENDAR_COLOR, CALENDAR_DESCRIPTION, CALENDAR_TIMEZONE, Calendar, LIST_PROPS,
55 },
56 rfc4918::{
57 DISPLAYNAME, GETCTAG, RESOURCETYPE, ResponseEntry, WebdavAuth, propfind::Propfind,
58 send::SendError, trace_unrecognized,
59 },
60 webdav_try,
61};
62
63#[derive(Debug)]
65pub struct ListCalendars {
66 state: State,
67}
68
69impl ListCalendars {
70 pub fn new(base_url: &Url, auth: &WebdavAuth, user_agent: &str, home_set_path: &str) -> Self {
72 let propfind = Propfind::new(base_url, auth, user_agent, home_set_path, 1, LIST_PROPS);
73 Self {
74 state: State::Propfind(propfind),
75 }
76 }
77}
78
79impl WebdavCoroutine for ListCalendars {
80 type Yield = WebdavYield;
81 type Return = Result<BTreeSet<Calendar>, SendError>;
82
83 fn resume(&mut self, arg: Option<&[u8]>) -> WebdavCoroutineState<Self::Yield, Self::Return> {
84 trace!("sending request");
85 match &mut self.state {
86 State::Propfind(propfind) => {
87 let multistatus = webdav_try!(propfind, arg);
88 let calendars = multistatus
89 .responses
90 .iter()
91 .filter_map(from_entry)
92 .collect();
93 WebdavCoroutineState::Complete(Ok(calendars))
94 }
95 }
96 }
97}
98
99fn from_entry(entry: &ResponseEntry) -> Option<Calendar> {
100 if !entry.has_resource_type(RESOURCETYPE, CALENDAR) {
101 trace!("skip non-calendar response {}", entry.href);
102 return None;
103 }
104
105 let id = entry.id();
106 if id.is_empty() {
107 return None;
108 }
109
110 trace_unrecognized(entry, LIST_PROPS);
111
112 Some(Calendar {
113 id: id.to_string(),
114 display_name: entry.text(DISPLAYNAME).map(ToString::to_string),
115 description: entry.text(CALENDAR_DESCRIPTION).map(ToString::to_string),
116 color: entry.text(CALENDAR_COLOR).map(ToString::to_string),
117 ctag: entry.text(GETCTAG).map(ToString::to_string),
118 tz: entry.text(CALENDAR_TIMEZONE).map(ToString::to_string),
119 })
120}
121
122#[derive(Debug)]
123enum State {
124 Propfind(Propfind),
125}