aboutsummaryrefslogtreecommitdiff
path: root/aero-dav/src/syncdecoder.rs
blob: 8e035ab990c020fd55aefeda319bf48ea1f83fc2 (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
use quick_xml::events::Event;

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

impl<E: dav::Extension> QRead<SyncCollection<E>> for SyncCollection<E> {
    async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
        xml.open(DAV_URN, "sync-collection").await?;
        let (mut sync_token, mut sync_level, mut limit, mut prop) = (None, None, None, None);
        loop {
            let mut dirty = false;
            xml.maybe_read(&mut sync_token, &mut dirty).await?;
            xml.maybe_read(&mut sync_level, &mut dirty).await?;
            xml.maybe_read(&mut limit, &mut dirty).await?;
            xml.maybe_read(&mut prop, &mut dirty).await?;

            if !dirty {
                match xml.peek() {
                    Event::End(_) => break,
                    _ => xml.skip().await?,
                };
            }
        }

        xml.close().await?;
        match (sync_token, sync_level, prop) {
            (Some(sync_token), Some(sync_level), Some(prop)) => Ok(SyncCollection {
                sync_token,
                sync_level,
                limit,
                prop,
            }),
            _ => Err(ParsingError::MissingChild),
        }
    }
}

impl QRead<SyncTokenRequest> for SyncTokenRequest {
    async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
        xml.open(DAV_URN, "sync-token").await?;
        let token = match xml.tag_string().await {
            Ok(v) => SyncTokenRequest::IncrementalSync(v),
            Err(ParsingError::Recoverable) => SyncTokenRequest::InitialSync,
            Err(e) => return Err(e),
        };
        xml.close().await?;
        Ok(token)
    }
}

impl QRead<SyncToken> for SyncToken {
    async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
        xml.open(DAV_URN, "sync-token").await?;
        let token = xml.tag_string().await?;
        xml.close().await?;
        Ok(SyncToken(token))
    }
}

impl QRead<SyncLevel> for SyncLevel {
    async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
        xml.open(DAV_URN, "sync-level").await?;
        let lvl = match xml.tag_string().await?.to_lowercase().as_str() {
            "1" => SyncLevel::One,
            "infinite" => SyncLevel::Infinite,
            _ => return Err(ParsingError::InvalidValue),
        };
        xml.close().await?;
        Ok(lvl)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::realization::All;
    use crate::types as dav;
    use crate::versioningtypes as vers;
    use crate::xml::Node;

    async fn deserialize<T: Node<T>>(src: &str) -> T {
        let mut rdr = Reader::new(quick_xml::NsReader::from_reader(src.as_bytes()))
            .await
            .unwrap();
        rdr.find().await.unwrap()
    }

    #[tokio::test]
    async fn sync_level() {
        {
            let expected = SyncLevel::One;
            let src = r#"<D:sync-level xmlns:D="DAV:">1</D:sync-level>"#;
            let got = deserialize::<SyncLevel>(src).await;
            assert_eq!(got, expected);
        }
        {
            let expected = SyncLevel::Infinite;
            let src = r#"<D:sync-level xmlns:D="DAV:">infinite</D:sync-level>"#;
            let got = deserialize::<SyncLevel>(src).await;
            assert_eq!(got, expected);
        }
    }

    #[tokio::test]
    async fn sync_token_request() {
        {
            let expected = SyncTokenRequest::InitialSync;
            let src = r#"<D:sync-token xmlns:D="DAV:"/>"#;
            let got = deserialize::<SyncTokenRequest>(src).await;
            assert_eq!(got, expected);
        }
        {
            let expected =
                SyncTokenRequest::IncrementalSync("http://example.com/ns/sync/1232".into());
            let src =
                r#"<D:sync-token xmlns:D="DAV:">http://example.com/ns/sync/1232</D:sync-token>"#;
            let got = deserialize::<SyncTokenRequest>(src).await;
            assert_eq!(got, expected);
        }
    }

    #[tokio::test]
    async fn sync_token() {
        let expected = SyncToken("http://example.com/ns/sync/1232".into());
        let src = r#"<D:sync-token xmlns:D="DAV:">http://example.com/ns/sync/1232</D:sync-token>"#;
        let got = deserialize::<SyncToken>(src).await;
        assert_eq!(got, expected);
    }

    #[tokio::test]
    async fn sync_collection() {
        {
            let expected = SyncCollection::<All> {
                sync_token: SyncTokenRequest::IncrementalSync(
                    "http://example.com/ns/sync/1232".into(),
                ),
                sync_level: SyncLevel::One,
                limit: Some(vers::Limit(vers::NResults(100))),
                prop: dav::PropName(vec![dav::PropertyRequest::GetEtag]),
            };
            let src = r#"<D:sync-collection xmlns:D="DAV:">
                <D:sync-token>http://example.com/ns/sync/1232</D:sync-token>
                <D:sync-level>1</D:sync-level>
                <D:limit>
                    <D:nresults>100</D:nresults>
                </D:limit>
                <D:prop>
                    <D:getetag/>
                </D:prop>
            </D:sync-collection>"#;
            let got = deserialize::<SyncCollection<All>>(src).await;
            assert_eq!(got, expected);
        }

        {
            let expected = SyncCollection::<All> {
                sync_token: SyncTokenRequest::InitialSync,
                sync_level: SyncLevel::Infinite,
                limit: None,
                prop: dav::PropName(vec![dav::PropertyRequest::GetEtag]),
            };
            let src = r#"<D:sync-collection xmlns:D="DAV:">
                <D:sync-token/>
                <D:sync-level>infinite</D:sync-level>
                <D:prop>
                    <D:getetag/>
                </D:prop>
            </D:sync-collection>"#;
            let got = deserialize::<SyncCollection<All>>(src).await;
            assert_eq!(got, expected);
        }
    }
}