webdav_xml/elements/
propstat.rs

1// SPDX-FileCopyrightText: d-k-bo <d-k-bo@mailbox.org>
2//
3// SPDX-License-Identifier: MIT OR Apache-2.0
4
5use crate::{
6    elements::{Properties, ResponseDescription, Status},
7    value::ValueMap,
8    Element, Error, OptionExt, Value, DAV_NAMESPACE, DAV_PREFIX,
9};
10
11/// The `propstat` XML element as defined in [RFC 4918](http://webdav.org/specs/rfc4918.html#ELEMENT_propstat).
12#[derive(Clone, Debug, PartialEq)]
13pub struct Propstat {
14    pub prop: Properties,
15    pub status: Status,
16    // pub error: Option<Error>,
17    pub responsedescription: Option<ResponseDescription>,
18}
19
20impl Element for Propstat {
21    const NAMESPACE: &'static str = DAV_NAMESPACE;
22    const PREFIX: &'static str = DAV_PREFIX;
23    const LOCAL_NAME: &'static str = "propstat";
24}
25
26impl TryFrom<&Value> for Propstat {
27    type Error = Error;
28
29    fn try_from(value: &Value) -> Result<Self, Self::Error> {
30        let map = value.to_map()?;
31        Ok(Self {
32            prop: map.get().required::<Properties>()??,
33            status: map.get().required::<Status>()??,
34            responsedescription: map.get().transpose()?,
35        })
36    }
37}
38
39impl From<Propstat> for Value {
40    fn from(
41        Propstat {
42            prop,
43            status,
44            responsedescription,
45        }: Propstat,
46    ) -> Value {
47        let mut map = ValueMap::new();
48
49        map.insert::<Properties>(prop.into());
50        map.insert::<Status>(status.into());
51        if let Some(responsedescription) = responsedescription {
52            map.insert::<ResponseDescription>(responsedescription.into())
53        }
54
55        Value::Map(map)
56    }
57}