io_webdav/rfc4791/calendar/update.rs
1//! `update-calendar` coroutine: `PROPPATCH` against a calendar
2//! collection.
3//!
4//! # Example
5//!
6//! ```rust,no_run
7//! use std::{
8//! io::{Read, Write},
9//! net::TcpStream,
10//! };
11//!
12//! use io_webdav::{
13//! coroutine::{WebdavCoroutine, WebdavCoroutineState, WebdavYield},
14//! rfc4791::calendar::{Calendar, update::UpdateCalendar},
15//! rfc4918::WebdavAuth,
16//! };
17//! use url::Url;
18//!
19//! // Ready stream needed (TCP-connected, TLS-negociated)
20//! let mut stream = TcpStream::connect("dav.example.org:443").unwrap();
21//! let mut buf = [0u8; 4096];
22//!
23//! let base_url: Url = "https://dav.example.org/".parse().unwrap();
24//! let auth = WebdavAuth::None;
25//! let calendar = Calendar {
26//! id: "personal".into(),
27//! color: Some("#ff0000".into()),
28//! ..Default::default()
29//! };
30//! let mut coroutine =
31//! UpdateCalendar::new(&base_url, &auth, "io-webdav", "/dav/calendars/", &calendar);
32//! let mut arg = None;
33//!
34//! loop {
35//! match coroutine.resume(arg.take()) {
36//! WebdavCoroutineState::Yielded(WebdavYield::WantsWrite(bytes)) => {
37//! stream.write_all(&bytes).unwrap();
38//! }
39//! WebdavCoroutineState::Yielded(WebdavYield::WantsRead) => {
40//! let n = stream.read(&mut buf).unwrap();
41//! arg = Some(&buf[..n]);
42//! }
43//! WebdavCoroutineState::Complete(Ok(())) => break,
44//! WebdavCoroutineState::Complete(Err(err)) => panic!("{err}"),
45//! }
46//! }
47//! ```
48
49use log::trace;
50use url::Url;
51
52use crate::{
53 coroutine::*,
54 rfc4791::calendar::{Calendar, join_path, property_set},
55 rfc4918::{WebdavAuth, proppatch::Proppatch, send::SendError},
56};
57
58/// Coroutine that updates a calendar collection's properties.
59#[derive(Debug)]
60pub struct UpdateCalendar {
61 state: State,
62}
63
64impl UpdateCalendar {
65 /// Builds a new `update-calendar` coroutine targeting
66 /// `home_set_path` joined with `calendar.id`.
67 pub fn new(
68 base_url: &Url,
69 auth: &WebdavAuth,
70 user_agent: &str,
71 home_set_path: &str,
72 calendar: &Calendar,
73 ) -> Self {
74 let path = join_path(home_set_path, &calendar.id);
75 let set = property_set(calendar);
76 let proppatch = Proppatch::new(base_url, auth, user_agent, &path, &set);
77 Self {
78 state: State::Proppatch(proppatch),
79 }
80 }
81}
82
83impl WebdavCoroutine for UpdateCalendar {
84 type Yield = WebdavYield;
85 type Return = Result<(), SendError>;
86
87 fn resume(&mut self, arg: Option<&[u8]>) -> WebdavCoroutineState<Self::Yield, Self::Return> {
88 trace!("sending request");
89 match &mut self.state {
90 State::Proppatch(proppatch) => proppatch.resume(arg),
91 }
92 }
93}
94
95#[derive(Debug)]
96enum State {
97 Proppatch(Proppatch),
98}