webdav_xml/properties/
resourcetype.rs

1// SPDX-FileCopyrightText: d-k-bo <d-k-bo@mailbox.org>
2//
3// SPDX-License-Identifier: MIT OR Apache-2.0
4
5use indexmap::IndexMap;
6
7use crate::{
8    element::ElementExt, value::ValueMap, Element, Error, Value, DAV_NAMESPACE, DAV_PREFIX,
9};
10
11/// The `resourcetype` property as defined in
12/// [RFC 4918](http://webdav.org/specs/rfc4918.html#PROPERTY_resourcetype).
13#[derive(Clone, Debug, PartialEq)]
14pub struct ResourceType(ValueMap);
15
16impl Element for ResourceType {
17    const NAMESPACE: &'static str = DAV_NAMESPACE;
18    const PREFIX: &'static str = DAV_PREFIX;
19    const LOCAL_NAME: &'static str = "resourcetype";
20}
21
22impl ResourceType {
23    pub fn empty() -> Self {
24        Self(Default::default())
25    }
26    pub fn collection() -> Self {
27        Self({
28            let mut map = IndexMap::with_capacity(1);
29            map.insert(Collection::element_name(), Value::Empty);
30            ValueMap::from(map)
31        })
32    }
33    pub fn is_collection(&self) -> bool {
34        self.0
35            .as_ref()
36            .contains_key(&Collection::element_name::<&str>())
37    }
38    pub fn get<'v, T>(&'v self) -> Option<Result<T, Error>>
39    where
40        T: Element + TryFrom<&'v Value, Error = Error>,
41    {
42        self.0.get::<T>()
43    }
44}
45
46impl TryFrom<&Value> for ResourceType {
47    type Error = Error;
48
49    fn try_from(value: &Value) -> Result<Self, Self::Error> {
50        value.to_map().cloned().map(Self)
51    }
52}
53
54impl From<ResourceType> for Value {
55    fn from(ResourceType(map): ResourceType) -> Value {
56        Value::Map(map)
57    }
58}
59
60/// The `collection` XML element as defined in
61/// [RFC 4918](http://webdav.org/specs/rfc4918.html#ELEMENT_collection).
62pub struct Collection;
63
64impl Element for Collection {
65    const NAMESPACE: &'static str = DAV_NAMESPACE;
66    const PREFIX: &'static str = DAV_PREFIX;
67    const LOCAL_NAME: &'static str = "collection";
68}