vstorage/
dav.rs

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
//! Common bits and pieces shared between CalDAV and CardDAV

use http::{StatusCode, Uri};
use libdav::{
    dav::{ListedResource, WebDavError},
    sd::BootstrapError,
};
use log::warn;

use crate::{base::ItemRef, CollectionId, CollectionIdError, Error, ErrorKind, Result};

/// Generate a path for a collection expected to have id `id`.
pub(crate) fn path_for_collection_in_home_set(home_set: &Uri, id: &str) -> String {
    // TODO: can be simplified with: https://github.com/hyperium/http/pull/623
    let mut path = match home_set.clone().into_parts().path_and_query {
        Some(ref pq) => pq.path(),
        None => "/",
    }
    .to_owned();

    if let Some(index) = path.find('?') {
        path.truncate(index + 1);
    }

    if !path.ends_with('/') {
        path.push('/');
    }
    path.push_str(id);
    path.push('/');
    path
}

pub(crate) fn collection_href_for_item(item_href: &str) -> Result<&str> {
    let mut parts = item_href.rsplitn(2, '/');
    let _resource = parts.next();
    let collection_href = parts
        .next()
        .ok_or_else(|| Error::from(ErrorKind::InvalidInput))?;
    Ok(collection_href)
}

/// Returns the last path component.
#[inline]
pub(crate) fn collection_id_for_href(href: &str) -> Result<CollectionId, CollectionIdError> {
    href.trim_matches('/') // Remove any trailing slashes.
        .rsplit('/')
        .next()
        .expect("rsplit always returns at least one item")
        .parse()
}

pub(crate) fn parse_list_items(response: Vec<ListedResource>) -> Result<Vec<ItemRef>> {
    // TODO: should actually check that href's path matches the requested path.
    if response.len() == 1 && response[0].status == Some(StatusCode::NOT_FOUND) {
        return Err(ErrorKind::DoesNotExist.into());
    }

    response
        .into_iter()
        .filter_map(|r| match (r.status, r.details.etag) {
            (Some(StatusCode::OK) | None, Some(etag)) => Some(Ok(ItemRef {
                href: r.href,
                etag: etag.into(),
            })),
            (Some(status), _) => {
                warn!("Got status code {status} for item: {}.", r.href);
                None
            }
            (_, None) => Some(Err(ErrorKind::InvalidData.error("missing Etag"))),
        })
        .collect()
}

impl From<BootstrapError> for Error {
    fn from(value: BootstrapError) -> Self {
        // TODO: not implemented
        Error::new(ErrorKind::Uncategorised, value)
    }
}

impl From<libdav::dav::WebDavError> for Error {
    fn from(value: libdav::dav::WebDavError) -> Self {
        match value {
            WebDavError::BadStatusCode(StatusCode::NOT_FOUND) => {
                Error::new(ErrorKind::DoesNotExist, value)
            }
            WebDavError::BadStatusCode(StatusCode::FORBIDDEN) => {
                Error::new(ErrorKind::AccessDenied, value)
            }
            err => Error::new(ErrorKind::Uncategorised, err), // TODO
        }
    }
}

pub(crate) fn join_hrefs(collection_href: &str, item_href: &str) -> String {
    if item_href.starts_with('/') {
        return item_href.to_string();
    }

    let mut href = collection_href
        .strip_suffix('/')
        .unwrap_or(collection_href)
        .to_string();
    href.push('/');
    href.push_str(item_href);
    href
}