io_webdav/rfc6352/addressbook/delete.rs
1//! `delete-addressbook` coroutine: `DELETE` against an addressbook
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//! rfc4918::WebdavAuth,
15//! rfc6352::addressbook::delete::DeleteAddressbook,
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 mut coroutine =
26//! DeleteAddressbook::new(&base_url, &auth, "io-webdav", "/dav/addressbooks/", "contacts");
27//! let mut arg = None;
28//!
29//! loop {
30//! match coroutine.resume(arg.take()) {
31//! WebdavCoroutineState::Yielded(WebdavYield::WantsWrite(bytes)) => {
32//! stream.write_all(&bytes).unwrap();
33//! }
34//! WebdavCoroutineState::Yielded(WebdavYield::WantsRead) => {
35//! let n = stream.read(&mut buf).unwrap();
36//! arg = Some(&buf[..n]);
37//! }
38//! WebdavCoroutineState::Complete(Ok(_)) => break,
39//! WebdavCoroutineState::Complete(Err(err)) => panic!("{err}"),
40//! }
41//! }
42//! ```
43
44use alloc::vec::Vec;
45
46use log::trace;
47use url::Url;
48
49use crate::{
50 coroutine::*,
51 rfc4918::{
52 WebdavAuth,
53 delete::Delete,
54 send::{SendError, SendOk},
55 },
56 rfc6352::addressbook::join_path,
57};
58
59/// Coroutine that deletes an addressbook collection.
60#[derive(Debug)]
61pub struct DeleteAddressbook {
62 state: State,
63}
64
65impl DeleteAddressbook {
66 /// Builds a new `delete-addressbook` coroutine.
67 pub fn new(
68 base_url: &Url,
69 auth: &WebdavAuth,
70 user_agent: &str,
71 home_set_path: &str,
72 addressbook_id: &str,
73 ) -> Self {
74 let path = join_path(home_set_path, addressbook_id);
75 Self {
76 state: State::Delete(Delete::new(base_url, auth, user_agent, &path, None)),
77 }
78 }
79}
80
81impl WebdavCoroutine for DeleteAddressbook {
82 type Yield = WebdavYield;
83 type Return = Result<SendOk<Vec<u8>>, SendError>;
84
85 fn resume(&mut self, arg: Option<&[u8]>) -> WebdavCoroutineState<Self::Yield, Self::Return> {
86 trace!("sending request");
87 match &mut self.state {
88 State::Delete(delete) => delete.resume(arg),
89 }
90 }
91}
92
93#[derive(Debug)]
94enum State {
95 Delete(Delete),
96}