aboutsummaryrefslogtreecommitdiff
path: root/aero-dav/src/syncencoder.rs
blob: 55f7ad6729aeaeaeeb582944cd1cff86a7c54663 (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
use quick_xml::events::{BytesText, Event};
use quick_xml::Error as QError;

use super::synctypes::*;
use super::types::Extension;
use super::xml::{IWrite, QWrite, Writer};

impl QWrite for Property {
    async fn qwrite(&self, xml: &mut Writer<impl IWrite>) -> Result<(), QError> {
        match self {
            Self::SyncToken(token) => token.qwrite(xml).await,
        }
    }
}

impl QWrite for PropertyRequest {
    async fn qwrite(&self, xml: &mut Writer<impl IWrite>) -> Result<(), QError> {
        match self {
            Self::SyncToken => {
                let start = xml.create_dav_element("sync-token");
                xml.q.write_event_async(Event::Empty(start)).await
            }
        }
    }
}

impl QWrite for ReportTypeName {
    async fn qwrite(&self, xml: &mut Writer<impl IWrite>) -> Result<(), QError> {
        match self {
            Self::SyncCollection => {
                let start = xml.create_dav_element("sync-collection");
                xml.q.write_event_async(Event::Empty(start)).await
            }
        }
    }
}

impl QWrite for Multistatus {
    async fn qwrite(&self, xml: &mut Writer<impl IWrite>) -> Result<(), QError> {
        self.sync_token.qwrite(xml).await
    }
}

impl<E: Extension> QWrite for SyncCollection<E> {
    async fn qwrite(&self, xml: &mut Writer<impl IWrite>) -> Result<(), QError> {
        let start = xml.create_dav_element("sync-collection");
        let end = start.to_end();

        xml.q.write_event_async(Event::Start(start.clone())).await?;
        self.sync_token.qwrite(xml).await?;
        self.sync_level.qwrite(xml).await?;
        if let Some(limit) = &self.limit {
            limit.qwrite(xml).await?;
        }
        self.prop.qwrite(xml).await?;
        xml.q.write_event_async(Event::End(end)).await
    }
}

impl QWrite for SyncTokenRequest {
    async fn qwrite(&self, xml: &mut Writer<impl IWrite>) -> Result<(), QError> {
        let start = xml.create_dav_element("sync-token");

        match self {
            Self::InitialSync => xml.q.write_event_async(Event::Empty(start)).await,
            Self::IncrementalSync(uri) => {
                let end = start.to_end();
                xml.q.write_event_async(Event::Start(start.clone())).await?;
                xml.q
                    .write_event_async(Event::Text(BytesText::new(uri.as_str())))
                    .await?;
                xml.q.write_event_async(Event::End(end)).await
            }
        }
    }
}

impl QWrite for SyncToken {
    async fn qwrite(&self, xml: &mut Writer<impl IWrite>) -> Result<(), QError> {
        let start = xml.create_dav_element("sync-token");
        let end = start.to_end();

        xml.q.write_event_async(Event::Start(start.clone())).await?;
        xml.q
            .write_event_async(Event::Text(BytesText::new(self.0.as_str())))
            .await?;
        xml.q.write_event_async(Event::End(end)).await
    }
}

impl QWrite for SyncLevel {
    async fn qwrite(&self, xml: &mut Writer<impl IWrite>) -> Result<(), QError> {
        let start = xml.create_dav_element("sync-level");
        let end = start.to_end();
        let text = match self {
            Self::One => "1",
            Self::Infinite => "infinite",
        };

        xml.q.write_event_async(Event::Start(start.clone())).await?;
        xml.q
            .write_event_async(Event::Text(BytesText::new(text)))
            .await?;
        xml.q.write_event_async(Event::End(end)).await
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::realization::{self, All};
    use crate::types as dav;
    use crate::versioningtypes as vers;
    use crate::xml::Node;
    use crate::xml::Reader;
    use tokio::io::AsyncWriteExt;

    async fn serialize_deserialize<T: Node<T>>(src: &T) {
        let mut buffer = Vec::new();
        let mut tokio_buffer = tokio::io::BufWriter::new(&mut buffer);
        let q = quick_xml::writer::Writer::new_with_indent(&mut tokio_buffer, b' ', 4);
        let ns_to_apply = vec![
            ("xmlns:D".into(), "DAV:".into()),
            ("xmlns:C".into(), "urn:ietf:params:xml:ns:caldav".into()),
        ];
        let mut writer = Writer { q, ns_to_apply };

        src.qwrite(&mut writer).await.expect("xml serialization");
        tokio_buffer.flush().await.expect("tokio buffer flush");
        let got = std::str::from_utf8(buffer.as_slice()).unwrap();

        // deserialize
        let mut rdr = Reader::new(quick_xml::NsReader::from_reader(got.as_bytes()))
            .await
            .unwrap();
        let res = rdr.find().await.unwrap();

        // check
        assert_eq!(src, &res);
    }

    #[tokio::test]
    async fn sync_level() {
        serialize_deserialize(&SyncLevel::One).await;
        serialize_deserialize(&SyncLevel::Infinite).await;
    }

    #[tokio::test]
    async fn sync_token_request() {
        serialize_deserialize(&SyncTokenRequest::InitialSync).await;
        serialize_deserialize(&SyncTokenRequest::IncrementalSync(
            "http://example.com/ns/sync/1232".into(),
        ))
        .await;
    }

    #[tokio::test]
    async fn sync_token() {
        serialize_deserialize(&SyncToken("http://example.com/ns/sync/1232".into())).await;
    }

    #[tokio::test]
    async fn sync_collection() {
        serialize_deserialize(&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]),
        })
        .await;

        serialize_deserialize(&SyncCollection::<All> {
            sync_token: SyncTokenRequest::InitialSync,
            sync_level: SyncLevel::Infinite,
            limit: None,
            prop: dav::PropName(vec![dav::PropertyRequest::GetEtag]),
        })
        .await;
    }

    #[tokio::test]
    async fn prop_req() {
        serialize_deserialize(&dav::PropName::<All>(vec![
            dav::PropertyRequest::Extension(realization::PropertyRequest::Sync(
                PropertyRequest::SyncToken,
            )),
        ]))
        .await;
    }

    #[tokio::test]
    async fn prop_val() {
        serialize_deserialize(&dav::PropValue::<All>(vec![
            dav::Property::Extension(realization::Property::Sync(Property::SyncToken(SyncToken(
                "http://example.com/ns/sync/1232".into(),
            )))),
            dav::Property::Extension(realization::Property::Vers(
                vers::Property::SupportedReportSet(vec![vers::SupportedReport(
                    vers::ReportName::Extension(realization::ReportTypeName::Sync(
                        ReportTypeName::SyncCollection,
                    )),
                )]),
            )),
        ]))
        .await;
    }

    #[tokio::test]
    async fn multistatus_ext() {
        serialize_deserialize(&dav::Multistatus::<All> {
            responses: vec![dav::Response {
                status_or_propstat: dav::StatusOrPropstat::Status(
                    vec![dav::Href("/".into())],
                    dav::Status(http::status::StatusCode::OK),
                ),
                error: None,
                location: None,
                responsedescription: None,
            }],
            responsedescription: None,
            extension: Some(realization::Multistatus::Sync(Multistatus {
                sync_token: SyncToken("http://example.com/ns/sync/1232".into()),
            })),
        })
        .await;
    }
}