io-webdav 0.1.0

WebDAV client library for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
//! # Standard, blocking WebDAV client
//!
//! Holds a single boxed stream (any blocking `Read + Write` impl) plus the
//! [`WebdavAuth`] credential, the user-facing pub options ([`base_url`],
//! [`user_agent`]) and the discovery caches ([`principal_url`],
//! [`calendar_home_set`], [`addressbook_home_set`]).
//!
//! The bare [`new`] constructor takes a pre-connected stream; callers handle
//! TCP and TLS themselves. With one of the TLS feature flags enabled
//! (`rustls-ring`, `rustls-aws`, `native-tls`), [`connect`] is also available
//! and handles `https://` URLs end-to-end via
//! [`pimalaya_stream::std::stream::StreamStd`].
//!
//! Discovery flows top-down from the configured [`base_url`] (the DAV
//! context root, resolved by pimconf's RFC 6764 discovery upstream):
//! [`current_user_principal`] resolves the principal URL;
//! [`calendar_home_set`] / [`addressbook_home_set`] resolve the per-RFC
//! home-set URL. Each step caches its result; higher-level methods return
//! [`MissingPrincipal`] / [`MissingCalendarHomeSet`] /
//! [`MissingAddressbookHomeSet`] when the cache is empty (mirrors io-jmap's
//! `MissingSession`).
//!
//! [`base_url`]: WebdavClientStd::base_url
//! [`user_agent`]: WebdavClientStd::user_agent
//! [`principal_url`]: WebdavClientStd::principal_url
//! [`calendar_home_set`]: WebdavClientStd::calendar_home_set
//! [`addressbook_home_set`]: WebdavClientStd::addressbook_home_set
//! [`new`]: WebdavClientStd::new
//! [`connect`]: WebdavClientStd::connect
//! [`current_user_principal`]: WebdavClientStd::current_user_principal
//! [`MissingPrincipal`]: WebdavClientStdError::MissingPrincipal
//! [`MissingCalendarHomeSet`]: WebdavClientStdError::MissingCalendarHomeSet
//! [`MissingAddressbookHomeSet`]: WebdavClientStdError::MissingAddressbookHomeSet

use core::fmt;

use alloc::{
    boxed::Box,
    collections::BTreeSet,
    format,
    string::{String, ToString},
    vec::Vec,
};

use std::io::{self, Read, Write};

#[cfg(any(
    feature = "rustls-aws",
    feature = "rustls-ring",
    feature = "native-tls"
))]
use pimalaya_stream::{std::stream::StreamStd, tls::Tls};
use thiserror::Error;
use url::Url;

use crate::{
    coroutine::*,
    rfc4791::{
        calendar::{
            Calendar, create::CreateCalendar, delete::DeleteCalendar, home_set::CalendarHomeSet,
            list::ListCalendars, update::UpdateCalendar,
        },
        item::{
            create::{CreateItem, CreateItemOk},
            delete::DeleteItem,
            list::{ItemEntry, ListItems},
            read::{ItemBody, ReadItem},
            update::{UpdateItem, UpdateItemOk},
        },
    },
    rfc4918::{
        GETETAG, WebdavAuth, coroutine::WebdavRedirectYield,
        follow_redirects::FollowRedirectsError, send::SendError,
    },
    rfc5397::current_user_principal::CurrentUserPrincipal,
    rfc6352::{
        addressbook::{
            Addressbook, create::CreateAddressbook, delete::DeleteAddressbook,
            home_set::AddressbookHomeSet, list::ListAddressbooks, update::UpdateAddressbook,
        },
        card::{
            CardEntry, CardRef,
            create::{CreateCard, CreateCardOk},
            delete::DeleteCard,
            enumerate::EnumCards,
            list::ListCards,
            multiget::MultigetCards,
            read::{CardBody, ReadCard},
            update::{UpdateCard, UpdateCardOk},
        },
    },
    rfc6578::sync_collection::{SyncCollection, SyncCollectionError, SyncDelta},
};

const READ_BUFFER_SIZE: usize = 16 * 1024;

const DEFAULT_USER_AGENT: &str = concat!("io-webdav/", env!("CARGO_PKG_VERSION"));

/// Errors returned by [`WebdavClientStd`].
#[derive(Debug, Error)]
pub enum WebdavClientStdError {
    /// A WebDAV request failed while being sent.
    #[error(transparent)]
    Send(#[from] SendError),
    /// A redirect-aware WebDAV send failed.
    #[error(transparent)]
    FollowRedirects(#[from] FollowRedirectsError),
    /// A `sync-collection` REPORT failed.
    #[error(transparent)]
    SyncCollection(#[from] SyncCollectionError),

    /// An underlying I/O operation failed.
    #[error(transparent)]
    Io(#[from] io::Error),

    /// TLS negotiation or the underlying TLS stack failed.
    #[cfg(any(
        feature = "rustls-aws",
        feature = "rustls-ring",
        feature = "native-tls"
    ))]
    #[error(transparent)]
    Tls(#[from] anyhow::Error),
    /// The target WebDAV URL carries no host.
    #[cfg(any(
        feature = "rustls-aws",
        feature = "rustls-ring",
        feature = "native-tls"
    ))]
    #[error("WebDAV URL `{0}` has no host")]
    UrlMissingHost(String),
    /// The target WebDAV URL uses a scheme other than `http` or `https`.
    #[cfg(any(
        feature = "rustls-aws",
        feature = "rustls-ring",
        feature = "native-tls"
    ))]
    #[error("WebDAV URL `{0}` has unsupported scheme `{1}` (expected `http` or `https`)")]
    UrlUnsupportedScheme(String, String),

    /// The server redirected during an operation that must not follow
    /// redirects.
    #[error("WebDAV server redirected to `{0}` during a non-redirectable operation")]
    UnexpectedRedirect(Url),

    /// The client has no principal URL yet; `current_user_principal`
    /// must run first.
    #[error("WebDAV client missing principal URL; call `current_user_principal` first")]
    MissingPrincipal,
    /// The client has no calendar home-set yet; `calendar_home_set` must
    /// run first.
    #[error("WebDAV client missing calendar home-set; call `calendar_home_set` first")]
    MissingCalendarHomeSet,
    /// The client has no addressbook home-set yet; `addressbook_home_set`
    /// must run first.
    #[error("WebDAV client missing addressbook home-set; call `addressbook_home_set` first")]
    MissingAddressbookHomeSet,
}

/// Std-blocking WebDAV client wrapping a single blocking stream.
pub struct WebdavClientStd {
    /// The active blocking stream. Public so higher-level crates can pump their
    /// own [`WebdavCoroutine`]s through it (as io-jmap exposes its stream),
    /// reusing this client's discovery cache.
    pub stream: Box<dyn WebdavStream>,

    auth: WebdavAuth,

    /// Base URL prepended to every request path.
    pub base_url: Url,

    /// `User-Agent` header value.
    pub user_agent: String,

    /// Cached principal URL (RFC 5397).
    pub principal_url: Option<Url>,

    /// Cached CalDAV home-set URL (RFC 4791 §6.2.1).
    pub calendar_home_set: Option<Url>,

    /// Cached CardDAV home-set URL (RFC 6352 §7.1.1).
    pub addressbook_home_set: Option<Url>,
}

impl fmt::Debug for WebdavClientStd {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("WebdavClientStd")
            .field("base_url", &self.base_url.as_str())
            .field("user_agent", &self.user_agent)
            .field(
                "principal_url",
                &self.principal_url.as_ref().map(Url::as_str),
            )
            .field(
                "calendar_home_set",
                &self.calendar_home_set.as_ref().map(Url::as_str),
            )
            .field(
                "addressbook_home_set",
                &self.addressbook_home_set.as_ref().map(Url::as_str),
            )
            .finish_non_exhaustive()
    }
}

impl WebdavClientStd {
    /// Builds a client around `stream`. The caller is responsible for
    /// opening the connection (TCP, TLS handshake if needed).
    pub fn new<S: Read + Write + Send + 'static>(
        stream: S,
        auth: WebdavAuth,
        base_url: Url,
    ) -> Self {
        Self {
            stream: Box::new(stream),
            auth,
            base_url,
            user_agent: DEFAULT_USER_AGENT.to_string(),
            principal_url: None,
            calendar_home_set: None,
            addressbook_home_set: None,
        }
    }

    /// Builds a client from a pre-connected stream and the full
    /// discovery state already in hand. Skips every discovery step.
    pub fn from_parts<S: Read + Write + Send + 'static>(
        stream: S,
        auth: WebdavAuth,
        base_url: Url,
        principal_url: Option<Url>,
        calendar_home_set: Option<Url>,
        addressbook_home_set: Option<Url>,
    ) -> Self {
        Self {
            stream: Box::new(stream),
            auth,
            base_url,
            user_agent: DEFAULT_USER_AGENT.to_string(),
            principal_url,
            calendar_home_set,
            addressbook_home_set,
        }
    }

    /// Connects to `url`'s host and runs the TLS handshake when the
    /// scheme is `https`. `http` goes through plain TCP. ALPN is set
    /// to `http/1.1`.
    #[cfg(any(
        feature = "rustls-aws",
        feature = "rustls-ring",
        feature = "native-tls"
    ))]
    pub fn connect(url: &Url, tls: &Tls, auth: WebdavAuth) -> Result<Self, WebdavClientStdError> {
        let host = url
            .host_str()
            .ok_or_else(|| WebdavClientStdError::UrlMissingHost(url.to_string()))?;

        let stream = match url.scheme() {
            "http" => StreamStd::connect_tcp(host, url.port().unwrap_or(80))?,
            "https" => StreamStd::connect_tls(host, url.port().unwrap_or(443), tls)?,
            scheme => {
                return Err(WebdavClientStdError::UrlUnsupportedScheme(
                    url.to_string(),
                    scheme.to_string(),
                ));
            }
        };

        Ok(Self::new(stream, auth, url.clone()))
    }

    /// Replaces the underlying stream; useful when discovery surfaces a
    /// new authority and the caller has to reconnect.
    pub fn set_stream<S: Read + Write + Send + 'static>(&mut self, stream: S) {
        self.stream = Box::new(stream);
    }

    /// Returns the active authentication scheme.
    pub fn auth(&self) -> &WebdavAuth {
        &self.auth
    }

    /// Runs any standard-shape coroutine (`Yield = WebdavYield`)
    /// against the client stream until completion. Redirect-aware
    /// discovery uses [`run_redirect`](Self::run_redirect) instead.
    fn run<C, T, E>(&mut self, mut coroutine: C) -> Result<T, WebdavClientStdError>
    where
        C: WebdavCoroutine<Yield = WebdavYield, Return = Result<T, E>>,
        E: Into<WebdavClientStdError>,
    {
        let mut buf = [0u8; READ_BUFFER_SIZE];
        let mut arg: Option<&[u8]> = None;

        let ret = loop {
            match coroutine.resume(arg.take()) {
                WebdavCoroutineState::Complete(ret) => break ret,
                WebdavCoroutineState::Yielded(yielded) => {
                    let n = pump(&mut *self.stream, &mut buf, yielded)?;
                    arg = n.map(|n| &buf[..n]);
                }
            }
        };

        ret.map_err(Into::into)
    }

    /// Runs a redirect-aware discovery coroutine (`Yield =
    /// WebdavRedirectYield`, `Return = Option<Url>`). A 3xx is surfaced
    /// as [`UnexpectedRedirect`] rather than followed: this client owns
    /// a single connected stream, so only the caller (who owns
    /// connection creation) can reconnect to the target and retry, e.g.
    /// via [`set_stream`] (mirrors io-http's `HttpClientStd`).
    ///
    /// [`UnexpectedRedirect`]: WebdavClientStdError::UnexpectedRedirect
    /// [`set_stream`]: WebdavClientStd::set_stream
    fn run_redirect(
        &mut self,
        coroutine: &mut dyn WebdavCoroutine<
            Yield = WebdavRedirectYield,
            Return = Result<Option<Url>, FollowRedirectsError>,
        >,
    ) -> Result<Option<Url>, WebdavClientStdError> {
        let mut buf = [0u8; READ_BUFFER_SIZE];
        let mut arg: Option<&[u8]> = None;

        loop {
            match coroutine.resume(arg.take()) {
                WebdavCoroutineState::Complete(Ok(url)) => return Ok(url),
                WebdavCoroutineState::Complete(Err(err)) => return Err(err.into()),
                WebdavCoroutineState::Yielded(WebdavRedirectYield::WantsRead) => {
                    let n = self.stream.read(&mut buf)?;
                    arg = Some(&buf[..n]);
                }
                WebdavCoroutineState::Yielded(WebdavRedirectYield::WantsWrite(bytes)) => {
                    self.stream.write_all(&bytes)?;
                    arg = None;
                }
                WebdavCoroutineState::Yielded(WebdavRedirectYield::WantsRedirect {
                    url, ..
                }) => {
                    return Err(WebdavClientStdError::UnexpectedRedirect(url));
                }
            }
        }
    }

    // ---- Discovery (RFC 5397 + per-RFC home-set) ------------------------

    /// Discovers the current user principal URL (RFC 5397) and caches
    /// it in [`principal_url`]. Subsequent calls return the cached
    /// value without hitting the network.
    ///
    /// [`principal_url`]: WebdavClientStd::principal_url
    pub fn current_user_principal(&mut self) -> Result<Url, WebdavClientStdError> {
        if let Some(url) = &self.principal_url {
            return Ok(url.clone());
        }

        let mut coroutine = CurrentUserPrincipal::new(&self.base_url, &self.auth, &self.user_agent);
        let url = self.run_redirect(&mut coroutine)?;
        let url = url.ok_or(WebdavClientStdError::MissingPrincipal)?;

        self.principal_url = Some(url.clone());
        Ok(url)
    }

    // ---- CalDAV (RFC 4791) ----------------------------------------------

    /// Discovers the CalDAV home-set URL (RFC 4791 §6.2.1) and caches
    /// it in [`calendar_home_set`]. Resolves [`principal_url`] first
    /// when it is not cached.
    ///
    /// [`calendar_home_set`]: WebdavClientStd::calendar_home_set
    /// [`principal_url`]: WebdavClientStd::principal_url
    pub fn calendar_home_set(&mut self) -> Result<Url, WebdavClientStdError> {
        if let Some(url) = &self.calendar_home_set {
            return Ok(url.clone());
        }

        let principal = self.current_user_principal()?;
        let path = principal.path().to_string();

        let mut coroutine =
            CalendarHomeSet::new(&self.base_url, &self.auth, &self.user_agent, &path);
        let url = self.run_redirect(&mut coroutine)?;
        let url = url.ok_or(WebdavClientStdError::MissingCalendarHomeSet)?;

        self.calendar_home_set = Some(url.clone());
        Ok(url)
    }

    /// Lists every calendar under the cached
    /// [`calendar_home_set`].
    ///
    /// [`calendar_home_set`]: WebdavClientStd::calendar_home_set
    pub fn list_calendars(&mut self) -> Result<BTreeSet<Calendar>, WebdavClientStdError> {
        let home = self
            .calendar_home_set
            .as_ref()
            .ok_or(WebdavClientStdError::MissingCalendarHomeSet)?;
        let path = home.path().to_string();

        let coroutine = ListCalendars::new(&self.base_url, &self.auth, &self.user_agent, &path);
        self.run(coroutine)
    }

    /// Creates a calendar collection under the cached
    /// [`calendar_home_set`].
    ///
    /// [`calendar_home_set`]: WebdavClientStd::calendar_home_set
    pub fn create_calendar(&mut self, calendar: &Calendar) -> Result<(), WebdavClientStdError> {
        let home = self
            .calendar_home_set
            .as_ref()
            .ok_or(WebdavClientStdError::MissingCalendarHomeSet)?;
        let path = home.path().to_string();

        let coroutine = CreateCalendar::new(
            &self.base_url,
            &self.auth,
            &self.user_agent,
            &path,
            calendar,
        );
        self.run(coroutine).map(|_| ())
    }

    /// Updates a calendar collection's properties.
    pub fn update_calendar(&mut self, calendar: &Calendar) -> Result<(), WebdavClientStdError> {
        let home = self
            .calendar_home_set
            .as_ref()
            .ok_or(WebdavClientStdError::MissingCalendarHomeSet)?;
        let path = home.path().to_string();

        let coroutine = UpdateCalendar::new(
            &self.base_url,
            &self.auth,
            &self.user_agent,
            &path,
            calendar,
        );
        self.run(coroutine)
    }

    /// Deletes a calendar collection.
    pub fn delete_calendar(&mut self, calendar_id: &str) -> Result<(), WebdavClientStdError> {
        let home = self
            .calendar_home_set
            .as_ref()
            .ok_or(WebdavClientStdError::MissingCalendarHomeSet)?;
        let path = home.path().to_string();

        let coroutine = DeleteCalendar::new(
            &self.base_url,
            &self.auth,
            &self.user_agent,
            &path,
            calendar_id,
        );
        self.run(coroutine).map(|_| ())
    }

    /// Lists every iCalendar item inside `calendar_id`. `comp_filter`
    /// is the optional VCALENDAR child filter (e.g.
    /// `<C:comp-filter name=\"VEVENT\" />`); pass an empty string to
    /// list every component type.
    pub fn list_items(
        &mut self,
        calendar_id: &str,
        comp_filter: &str,
    ) -> Result<BTreeSet<ItemEntry>, WebdavClientStdError> {
        let path = calendar_path(self.calendar_home_set.as_ref(), calendar_id)?;
        let coroutine = ListItems::new(
            &self.base_url,
            &self.auth,
            &self.user_agent,
            &path,
            comp_filter,
        );
        self.run(coroutine)
    }

    /// Reads a single calendar item's raw iCalendar bytes plus its
    /// ETag.
    pub fn read_item(
        &mut self,
        calendar_id: &str,
        item_id: &str,
    ) -> Result<ItemBody, WebdavClientStdError> {
        let path = calendar_path(self.calendar_home_set.as_ref(), calendar_id)?;
        let coroutine = ReadItem::new(&self.base_url, &self.auth, &self.user_agent, &path, item_id);
        self.run(coroutine)
    }

    /// Creates a calendar item by id.
    pub fn create_item(
        &mut self,
        calendar_id: &str,
        id: &str,
        ical: Vec<u8>,
    ) -> Result<CreateItemOk, WebdavClientStdError> {
        let path = calendar_path(self.calendar_home_set.as_ref(), calendar_id)?;
        let coroutine = CreateItem::new(
            &self.base_url,
            &self.auth,
            &self.user_agent,
            &path,
            id,
            ical,
        );
        self.run(coroutine)
    }

    /// Updates an existing calendar item.
    pub fn update_item(
        &mut self,
        calendar_id: &str,
        id: &str,
        ical: Vec<u8>,
        if_match: Option<&str>,
    ) -> Result<UpdateItemOk, WebdavClientStdError> {
        let path = calendar_path(self.calendar_home_set.as_ref(), calendar_id)?;
        let coroutine = UpdateItem::new(
            &self.base_url,
            &self.auth,
            &self.user_agent,
            &path,
            id,
            ical,
            if_match,
        );
        self.run(coroutine)
    }

    /// Deletes a calendar item.
    pub fn delete_item(
        &mut self,
        calendar_id: &str,
        item_id: &str,
        if_match: Option<&str>,
    ) -> Result<(), WebdavClientStdError> {
        let path = calendar_path(self.calendar_home_set.as_ref(), calendar_id)?;
        let coroutine = DeleteItem::new(
            &self.base_url,
            &self.auth,
            &self.user_agent,
            &path,
            item_id,
            if_match,
        );
        self.run(coroutine).map(|_| ())
    }

    // ---- CardDAV (RFC 6352) ---------------------------------------------

    /// Discovers the CardDAV home-set URL (RFC 6352 §7.1.1) and caches
    /// it in [`addressbook_home_set`].
    ///
    /// [`addressbook_home_set`]: WebdavClientStd::addressbook_home_set
    pub fn addressbook_home_set(&mut self) -> Result<Url, WebdavClientStdError> {
        if let Some(url) = &self.addressbook_home_set {
            return Ok(url.clone());
        }

        let principal = self.current_user_principal()?;
        let path = principal.path().to_string();

        let mut coroutine =
            AddressbookHomeSet::new(&self.base_url, &self.auth, &self.user_agent, &path);
        let url = self.run_redirect(&mut coroutine)?;
        let url = url.ok_or(WebdavClientStdError::MissingAddressbookHomeSet)?;

        self.addressbook_home_set = Some(url.clone());
        Ok(url)
    }

    /// Lists every addressbook under the cached
    /// [`addressbook_home_set`].
    ///
    /// [`addressbook_home_set`]: WebdavClientStd::addressbook_home_set
    pub fn list_addressbooks(&mut self) -> Result<BTreeSet<Addressbook>, WebdavClientStdError> {
        let home = self
            .addressbook_home_set
            .as_ref()
            .ok_or(WebdavClientStdError::MissingAddressbookHomeSet)?;
        let path = home.path().to_string();

        let coroutine = ListAddressbooks::new(&self.base_url, &self.auth, &self.user_agent, &path);
        self.run(coroutine)
    }

    /// Creates an addressbook collection under the cached
    /// [`addressbook_home_set`].
    ///
    /// [`addressbook_home_set`]: WebdavClientStd::addressbook_home_set
    pub fn create_addressbook(
        &mut self,
        addressbook: &Addressbook,
    ) -> Result<(), WebdavClientStdError> {
        let home = self
            .addressbook_home_set
            .as_ref()
            .ok_or(WebdavClientStdError::MissingAddressbookHomeSet)?;
        let path = home.path().to_string();

        let coroutine = CreateAddressbook::new(
            &self.base_url,
            &self.auth,
            &self.user_agent,
            &path,
            addressbook,
        );
        self.run(coroutine).map(|_| ())
    }

    /// Updates an addressbook collection's properties.
    pub fn update_addressbook(
        &mut self,
        addressbook: &Addressbook,
    ) -> Result<(), WebdavClientStdError> {
        let home = self
            .addressbook_home_set
            .as_ref()
            .ok_or(WebdavClientStdError::MissingAddressbookHomeSet)?;
        let path = home.path().to_string();

        let coroutine = UpdateAddressbook::new(
            &self.base_url,
            &self.auth,
            &self.user_agent,
            &path,
            addressbook,
        );
        self.run(coroutine)
    }

    /// Deletes an addressbook collection.
    pub fn delete_addressbook(&mut self, addressbook_id: &str) -> Result<(), WebdavClientStdError> {
        let home = self
            .addressbook_home_set
            .as_ref()
            .ok_or(WebdavClientStdError::MissingAddressbookHomeSet)?;
        let path = home.path().to_string();

        let coroutine = DeleteAddressbook::new(
            &self.base_url,
            &self.auth,
            &self.user_agent,
            &path,
            addressbook_id,
        );
        self.run(coroutine).map(|_| ())
    }

    /// Lists every card inside `addressbook_id`.
    pub fn list_cards(
        &mut self,
        addressbook_id: &str,
    ) -> Result<BTreeSet<CardEntry>, WebdavClientStdError> {
        let path = addressbook_path(self.addressbook_home_set.as_ref(), addressbook_id)?;
        let coroutine = ListCards::new(&self.base_url, &self.auth, &self.user_agent, &path);
        self.run(coroutine)
    }

    /// Enumerates card references (id plus ETag, no bodies) inside
    /// `addressbook_id`.
    pub fn enum_cards(
        &mut self,
        addressbook_id: &str,
    ) -> Result<BTreeSet<CardRef>, WebdavClientStdError> {
        let path = addressbook_path(self.addressbook_home_set.as_ref(), addressbook_id)?;
        let coroutine = EnumCards::new(&self.base_url, &self.auth, &self.user_agent, &path);
        self.run(coroutine)
    }

    /// Batch-fetches cards by id inside `addressbook_id` in a single
    /// round-trip.
    pub fn multiget_cards(
        &mut self,
        addressbook_id: &str,
        ids: &[&str],
    ) -> Result<Vec<CardEntry>, WebdavClientStdError> {
        let path = addressbook_path(self.addressbook_home_set.as_ref(), addressbook_id)?;
        let coroutine =
            MultigetCards::new(&self.base_url, &self.auth, &self.user_agent, &path, ids);
        self.run(coroutine)
    }

    /// Runs an incremental `sync-collection` REPORT (RFC 6578) against
    /// `addressbook_id`, requesting ETags only. Pass [`None`] as
    /// `sync_token` for an initial sync.
    pub fn sync_cards(
        &mut self,
        addressbook_id: &str,
        sync_token: Option<&str>,
    ) -> Result<SyncDelta, WebdavClientStdError> {
        let path = addressbook_path(self.addressbook_home_set.as_ref(), addressbook_id)?;
        let coroutine = SyncCollection::new(
            &self.base_url,
            &self.auth,
            &self.user_agent,
            &path,
            sync_token,
            &[GETETAG],
        );
        self.run(coroutine)
    }

    /// Reads a single card's raw vCard bytes plus its ETag.
    pub fn read_card(
        &mut self,
        addressbook_id: &str,
        card_id: &str,
    ) -> Result<CardBody, WebdavClientStdError> {
        let path = addressbook_path(self.addressbook_home_set.as_ref(), addressbook_id)?;
        let coroutine = ReadCard::new(&self.base_url, &self.auth, &self.user_agent, &path, card_id);
        self.run(coroutine)
    }

    /// Creates a card by id.
    pub fn create_card(
        &mut self,
        addressbook_id: &str,
        id: &str,
        vcard: Vec<u8>,
    ) -> Result<CreateCardOk, WebdavClientStdError> {
        let path = addressbook_path(self.addressbook_home_set.as_ref(), addressbook_id)?;
        let coroutine = CreateCard::new(
            &self.base_url,
            &self.auth,
            &self.user_agent,
            &path,
            id,
            vcard,
        );
        self.run(coroutine)
    }

    /// Updates an existing card.
    pub fn update_card(
        &mut self,
        addressbook_id: &str,
        id: &str,
        vcard: Vec<u8>,
        if_match: Option<&str>,
    ) -> Result<UpdateCardOk, WebdavClientStdError> {
        let path = addressbook_path(self.addressbook_home_set.as_ref(), addressbook_id)?;
        let coroutine = UpdateCard::new(
            &self.base_url,
            &self.auth,
            &self.user_agent,
            &path,
            id,
            vcard,
            if_match,
        );
        self.run(coroutine)
    }

    /// Deletes a card.
    pub fn delete_card(
        &mut self,
        addressbook_id: &str,
        card_id: &str,
        if_match: Option<&str>,
    ) -> Result<(), WebdavClientStdError> {
        let path = addressbook_path(self.addressbook_home_set.as_ref(), addressbook_id)?;
        let coroutine = DeleteCard::new(
            &self.base_url,
            &self.auth,
            &self.user_agent,
            &path,
            card_id,
            if_match,
        );
        self.run(coroutine).map(|_| ())
    }
}

/// Runs one standard I/O yield against the stream: writes the bytes, or
/// reads a chunk into `buf` and returns its length.
fn pump(
    stream: &mut dyn WebdavStream,
    buf: &mut [u8],
    yielded: WebdavYield,
) -> Result<Option<usize>, io::Error> {
    match yielded {
        WebdavYield::WantsRead => Ok(Some(stream.read(buf)?)),
        WebdavYield::WantsWrite(bytes) => {
            stream.write_all(&bytes)?;
            Ok(None)
        }
    }
}

fn calendar_path(home: Option<&Url>, calendar_id: &str) -> Result<String, WebdavClientStdError> {
    let home = home.ok_or(WebdavClientStdError::MissingCalendarHomeSet)?;
    let base = home.path().trim_end_matches('/');
    let id = calendar_id.trim_matches('/');
    Ok(format!("{base}/{id}"))
}

fn addressbook_path(
    home: Option<&Url>,
    addressbook_id: &str,
) -> Result<String, WebdavClientStdError> {
    let home = home.ok_or(WebdavClientStdError::MissingAddressbookHomeSet)?;
    let base = home.path().trim_end_matches('/');
    let id = addressbook_id.trim_matches('/');
    Ok(format!("{base}/{id}"))
}

/// Marker for everything the client can run against; auto-implemented
/// for any blocking `Read + Write + Send` impl.
pub trait WebdavStream: Read + Write + Send {}
impl<T: Read + Write + Send + ?Sized> WebdavStream for T {}