aboutsummaryrefslogtreecommitdiff
path: root/src/dav/decoder.rs
blob: a7fdca56f12e10830452030a2dfbeacc74ef0669 (plain) (blame)
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
use std::borrow::Cow;
use std::future::Future;

use quick_xml::events::{Event, BytesStart, BytesDecl, BytesText};
use quick_xml::events::attributes::AttrError;
use quick_xml::name::{Namespace, QName, PrefixDeclaration, ResolveResult, ResolveResult::*};
use quick_xml::reader::NsReader;
use tokio::io::AsyncBufRead;

use super::types::*;
use super::error::ParsingError;
use super::xml::{QRead, Reader, IRead, DAV_URN, CAL_URN};

impl<E: Extension> QRead<PropFind<E>> for PropFind<E> {
    async fn qread(xml: &mut Reader<impl IRead>) -> Result<Option<Self>, ParsingError> {
        // Find propfind
        xml.tag_start(DAV_URN, "propfind").await?;
        // Find any tag
        let propfind: PropFind<E> = loop {
            match xml.peek() {
                Event::Start(_) if xml.is_tag(DAV_URN, "allprop") => {
                    xml.tag_start(DAV_URN, "allprop").await?;
                    let r = PropFind::AllProp(Include::qread(xml).await?);
                    xml.tag_stop(DAV_URN, "allprop").await?;
                    break r
                },
                Event::Start(_) if xml.is_tag(DAV_URN, "prop") => {
                    let propname = PropName::qread(xml).await?.ok_or(ParsingError::MissingChild)?;
                    break PropFind::Prop(propname);
                },
                Event::Empty(_) if xml.is_tag(DAV_URN, "allprop") => {
                    xml.next().await?;
                    break PropFind::AllProp(None)
                },
                Event::Empty(_) if xml.is_tag(DAV_URN, "propname") => {
                    xml.next().await?;
                    break PropFind::PropName
                },
                _ => { xml.skip().await?; },
            }
        };

        // Close tag
        xml.tag_stop(DAV_URN, "propfind").await?;

        Ok(Some(propfind))
    }
}


impl<E: Extension> QRead<Include<E>> for Include<E> {
    async fn qread(xml: &mut Reader<impl IRead>) -> Result<Option<Self>, ParsingError> {
        xml.tag_start(DAV_URN, "include").await?;
        let mut acc: Vec<PropertyRequest<E>> = Vec::new();
        loop {
            match xml.peek() {
                Event::Start(_) | Event::Empty(_) => { 
                    PropertyRequest::qread(xml).await?.map(|v| acc.push(v)); 
                },
                Event::End(_) if xml.is_tag(DAV_URN, "include") => break,
                _ => { xml.skip().await?; },
            }
        }
        xml.tag_stop(DAV_URN, "include").await?;
        Ok(Some(Include(acc)))
    }
}

impl<E: Extension> QRead<PropName<E>> for PropName<E> {
    async fn qread(xml: &mut Reader<impl IRead>) -> Result<Option<Self>, ParsingError> {
        xml.tag_start(DAV_URN, "prop").await?;
        let mut acc: Vec<PropertyRequest<E>> = Vec::new();
        loop {
            match xml.peek() {
                Event::Start(_) | Event::Empty(_) => {
                    PropertyRequest::qread(xml).await?.map(|v| acc.push(v));
                },
                Event::End(_) if xml.is_tag(DAV_URN, "prop") => break,
                _ => { xml.skip().await?; },
            }
        }
        xml.tag_stop(DAV_URN, "prop").await?;
        Ok(Some(PropName(acc)))
    }
}

impl<E: Extension> QRead<PropertyRequest<E>> for PropertyRequest<E> {
    async fn qread(xml: &mut Reader<impl IRead>) -> Result<Option<Self>, ParsingError> {
        loop {
            let bs = match xml.peek() {
                Event::Start(b) | Event::Empty(b) => b,
                _ => { 
                    xml.skip().await?; 
                    continue
                },
            };

            let mut maybe_res = None;

            // Option 1: a pure DAV property
            let (ns, loc) = xml.rdr.resolve_element(bs.name());
            if matches!(ns, Bound(Namespace(ns)) if ns == DAV_URN) {
                maybe_res = match loc.into_inner() {
                    b"creationdate" => Some(PropertyRequest::CreationDate),
                    b"displayname" => Some(PropertyRequest::DisplayName),
                    b"getcontentlanguage" => Some(PropertyRequest::GetContentLanguage),
                    b"getcontentlength" => Some(PropertyRequest::GetContentLength),
                    b"getcontenttype" => Some(PropertyRequest::GetContentType),
                    b"getetag" => Some(PropertyRequest::GetEtag),
                    b"getlastmodified" => Some(PropertyRequest::GetLastModified),
                    b"lockdiscovery" => Some(PropertyRequest::LockDiscovery),
                    b"resourcetype" => Some(PropertyRequest::ResourceType),
                    b"supportedlock" => Some(PropertyRequest::SupportedLock),
                    _ => None,
                };
            }

            // Option 2: an extension property
            if maybe_res.is_none() {
                maybe_res = E::PropertyRequest::qread(xml).await?.map(PropertyRequest::Extension);
            }

            // Close the current tag
            xml.skip().await?; 

            return Ok(maybe_res)
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::dav::realization::Core;

    #[tokio::test]
    async fn basic_propfind_propname() {
        let src = r#"<?xml version="1.0" encoding="utf-8" ?>
<rando/>
<garbage><old/></garbage>
<D:propfind xmlns:D="DAV:">
    <D:propname/>
</D:propfind>
"#;

        let mut rdr = Reader::new(NsReader::from_reader(src.as_bytes())).await.unwrap();
        let got = PropFind::<Core>::qread(&mut rdr).await.unwrap().unwrap();

        assert_eq!(got, PropFind::<Core>::PropName);
    }

    #[tokio::test]
    async fn basic_propfind_prop() {
        let src = r#"<?xml version="1.0" encoding="utf-8" ?>
<rando/>
<garbage><old/></garbage>
<D:propfind xmlns:D="DAV:">
    <D:prop>
        <D:displayname/>
        <D:getcontentlength/>
        <D:getcontenttype/>
        <D:getetag/>
        <D:getlastmodified/>
        <D:resourcetype/>
        <D:supportedlock/>
    </D:prop>
</D:propfind>
"#;

        let mut rdr = Reader::new(NsReader::from_reader(src.as_bytes())).await.unwrap();
        let got = PropFind::<Core>::qread(&mut rdr).await.unwrap().unwrap();

        assert_eq!(got, PropFind::Prop(PropName(vec![
            PropertyRequest::DisplayName,
            PropertyRequest::GetContentLength,
            PropertyRequest::GetContentType,
            PropertyRequest::GetEtag,
            PropertyRequest::GetLastModified,
            PropertyRequest::ResourceType,
            PropertyRequest::SupportedLock,
        ])));
    }
}