oparl-types 0.8.0

Type definitions for the OParl protocol
Documentation
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// SPDX-FileCopyrightText: Politik im Blick developers
// SPDX-FileCopyrightText: Wolfgang Silbermayr <wolfgang@silbermayr.at>
//
// SPDX-License-Identifier: AGPL-3.0-or-later OR EUPL-1.2

use crate::{
    BodyUrl, Consultation, Date, DateTime, File, Keyword, Location, Name, OrganizationUrl,
    PaperUrl, PersonUrl, Url, namespace::PaperNamespaceUrl,
};

#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Paper {
    pub id: PaperUrl,

    #[serde(rename = "type")]
    pub namespace: PaperNamespaceUrl,

    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub body: Option<BodyUrl>,

    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub name: Option<Name>,

    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub reference: Option<String>,

    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub date: Option<Date>,

    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub paper_type: Option<String>,

    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub related_paper: Vec<PaperUrl>,

    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub superordinated_paper: Vec<PaperUrl>,

    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub subordinated_paper: Vec<PaperUrl>,

    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub main_file: Option<File>,

    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub auxiliary_file: Vec<File>,

    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub location: Vec<Location>,

    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub originator_person: Vec<PersonUrl>,

    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub under_direction_of: Vec<OrganizationUrl>,

    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub originator_organization: Vec<OrganizationUrl>,

    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub consultation: Vec<Consultation>,

    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub license: Option<Url>,

    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub keyword: Vec<Keyword>,

    pub created: DateTime,

    pub modified: DateTime,

    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub web: Option<Url>,

    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub deleted: Option<bool>,

    #[serde(default, flatten)]
    pub extensions: serde_json::Map<String, serde_json::Value>,
}

#[cfg(test)]
mod serde_tests {
    use pretty_assertions::assert_eq;
    use serde_json::json;
    use time::macros::{date, datetime};

    use super::Paper;
    use crate::{
        Consultation, File, Location, Sha1Sum,
        namespace::{
            ConsultationNamespaceUrl, FileNamespaceUrl, LocationNamespaceUrl, PaperNamespaceUrl,
        },
    };

    fn example_paper() -> Paper {
        let geojson_feature = geojson::GeometryValue::Point {
            coordinates: [7.03291, 50.98249].into(),
        };

        Paper {
            id: "https://oparl.example.org/paper/749"
                .parse()
                .expect("value must be parseable as id"),
            namespace: PaperNamespaceUrl::Identifier,
            body: Some(
                "https://oparl.example.org/bodies/1"
                    .parse()
                    .expect("value must be parseable as url"),
            ),
            name: Some("Antwort auf Anfrage 1200/2014".into()),
            reference: Some("1234/2014".into()),
            date: Some(date!(2014 - 04 - 04).into()),
            paper_type: Some("Beantwortung einer Anfrage".into()),
            related_paper: vec![
                "https://oparl.example.org/paper/699"
                    .parse()
                    .expect("value must be parseable as url"),
            ],
            superordinated_paper: vec![],
            subordinated_paper: vec![],
            main_file: Some(File {
                id: "https://oparl.example.org/files/57737"
                    .parse()
                    .expect("value must be parseable as id"),
                namespace: FileNamespaceUrl::Identifier,
                file_name: Some("anlage_1_zur_anfrage.pdf".into()),
                name: Some("Anlage 1 zur Anfrage".into()),
                mime_type: Some("application/pdf".into()),
                date: Some(date!(2013 - 01 - 04).into()),
                size: Some(82930),
                sha1_checksum: Some(Sha1Sum::from([
                    0xd7, 0x49, 0x75, 0x1a, 0xf4, 0x4a, 0x32, 0xc8, 0x18, 0xb9, 0xb1, 0xe1, 0x51,
                    0x52, 0x51, 0xc6, 0x77, 0x34, 0xf5, 0xd2,
                ])),
                sha512_checksum: None,
                text: None,
                access_url: "https://oparl.example.org/files/57737.pdf"
                    .parse()
                    .expect("value must be parseable as url"),
                download_url: Some(
                    "https://oparl.example.org/files/download/57737.pdf"
                        .parse()
                        .expect("value must be parseable as url"),
                ),
                external_service_url: None,
                master_file: None,
                derivative_file: vec![],
                file_license: None,
                meeting: vec![],
                agenda_item: vec![],
                paper: vec![],
                license: Some(
                    "http://www.opendefinition.org/licenses/cc-by"
                        .parse()
                        .expect("value must be parseable as url"),
                ),
                keyword: vec![],
                created: datetime!(2013-01-04 07:54:13 +01:00).into(),
                modified: datetime!(2013-01-04 07:54:13 +01:00).into(),
                web: None,
                deleted: None,
                extensions: serde_json::Map::new(),
            }),
            auxiliary_file: vec![File {
                id: "https://oparl.example.org/files/57739"
                    .parse()
                    .expect("value must be parseable as id"),
                namespace: FileNamespaceUrl::Identifier,
                file_name: Some("anlage.pdf".into()),
                name: Some("Anlage 1 zur Anfrage".into()),
                mime_type: Some("application/pdf".into()),
                date: Some(date!(2013 - 01 - 04).into()),
                size: Some(82930),
                sha1_checksum: Some(Sha1Sum::from([
                    0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32, 0x55, 0xbf, 0xef, 0x95,
                    0x60, 0x18, 0x90, 0xaf, 0xd8, 0x07, 0x09,
                ])),
                sha512_checksum: None,
                text: Some("Der Übersichtsplan zeigt alle Ebenen des ...".into()),
                access_url: "https://oparl.example.org/files/57739.pdf"
                    .parse()
                    .expect("value must be parseable as url"),
                download_url: Some(
                    "https://oparl.example.org/files/download/57739.pdf"
                        .parse()
                        .expect("value must be parseable as url"),
                ),
                external_service_url: None,
                master_file: Some(
                    "https://oparl.example.org/files/57738"
                        .parse()
                        .expect("value must be parseable as id"),
                ),
                derivative_file: vec![],
                file_license: None,
                meeting: vec![],
                agenda_item: vec![],
                paper: vec![],
                license: Some(
                    "http://www.opendefinition.org/licenses/cc-by"
                        .parse()
                        .expect("value must be parseable as url"),
                ),
                keyword: vec![],
                created: datetime!(2013-01-04 07:54:13 +01:00).into(),
                modified: datetime!(2013-01-04 07:54:13 +01:00).into(),
                web: None,
                deleted: None,
                extensions: serde_json::Map::new(),
            }],
            location: vec![Location {
                id: "https://oparl.example.org/locations/29856"
                    .parse()
                    .expect("value must be parseable as id"),
                namespace: LocationNamespaceUrl::Identifier,
                description: Some("Honschaftsstraße 312, Köln".to_string()),
                geojson: Some(geojson_feature.into()),
                street_address: None,
                room: None,
                postal_code: None,
                sub_locality: None,
                locality: None,
                bodies: vec![],
                organizations: vec![],
                persons: vec![],
                meetings: vec![],
                papers: vec![],
                license: None,
                keyword: vec![],
                created: datetime!(2012-01-08 14:05:27 +01:00).into(),
                modified: datetime!(2012-01-08 14:05:27 +01:00).into(),
                web: None,
                deleted: None,
                extensions: serde_json::Map::new(),
            }],
            originator_person: vec![
                "https://oparl.example.org/person/2000"
                    .parse()
                    .expect("value must be parseable as url"),
                "https://oparl.example.org/person/1000"
                    .parse()
                    .expect("value must be parseable as url"),
            ],
            under_direction_of: vec![
                "https://oparl.example.org/organization/2000"
                    .parse()
                    .expect("value must be parseable as url"),
            ],
            originator_organization: vec![
                "https://oparl.example.org/organization/2000"
                    .parse()
                    .expect("value must be parseable as url"),
                "https://oparl.example.org/organization/1000"
                    .parse()
                    .expect("value must be parseable as url"),
            ],
            consultation: vec![Consultation {
                id: "https://oparl.example.org/consultation/47594"
                    .parse()
                    .expect("value must be parseable as id"),
                namespace: ConsultationNamespaceUrl::Identifier,
                paper: None,
                agenda_item: Some(
                    "https://oparl.example.org/agendaitem/15569"
                        .parse()
                        .expect("value must be parseable as url"),
                ),
                meeting: Some(
                    "https://oparl.example.org/meeting/243"
                        .parse()
                        .expect("value must be parseable as url"),
                ),
                organization: vec![
                    "https://oparl.example.org/organization/96"
                        .parse()
                        .expect("value must be parseable as url"),
                ],
                authoritative: Some(false),
                role: Some("Beschlussfassung".into()),
                license: None,
                keyword: vec![],
                created: datetime!(2012-01-08 14:05:27 +01:00).into(),
                modified: datetime!(2012-01-08 14:05:27 +01:00).into(),
                web: None,
                deleted: None,
                extensions: serde_json::Map::new(),
            }],
            license: None,
            keyword: vec![],
            created: datetime!(2013-01-08 12:05:27 +01:00).into(),
            modified: datetime!(2013-01-08 12:05:27 +01:00).into(),
            web: None,
            deleted: None,
            extensions: serde_json::Map::new(),
        }
    }

    fn example_paper_json() -> serde_json::Value {
        json!({
    "id": "https://oparl.example.org/paper/749",
    "type": "https://schema.oparl.org/1.1/Paper",
    "body": "https://oparl.example.org/bodies/1",
    "name": "Antwort auf Anfrage 1200/2014",
    "reference": "1234/2014",
    "date": "2014-04-04",
    "paperType": "Beantwortung einer Anfrage",
    "relatedPaper": [
        "https://oparl.example.org/paper/699"
    ],
    "mainFile": {
        "id": "https://oparl.example.org/files/57737",
        "type": "https://schema.oparl.org/1.1/File",
        "name": "Anlage 1 zur Anfrage",
        "fileName": "anlage_1_zur_anfrage.pdf",
        "mimeType": "application/pdf",
        "date": "2013-01-04",
        "sha1Checksum": "d749751af44a32c818b9b1e1515251c67734f5d2",
        "size": 82930,
        "accessUrl": "https://oparl.example.org/files/57737.pdf",
        "downloadUrl": "https://oparl.example.org/files/download/57737.pdf",
        "license": "http://www.opendefinition.org/licenses/cc-by",
        "created": "2013-01-04T07:54:13+01:00",
        "modified": "2013-01-04T07:54:13+01:00"
    },
    "auxiliaryFile": [
        {
            "id": "https://oparl.example.org/files/57739",
            "type": "https://schema.oparl.org/1.1/File",
            "name": "Anlage 1 zur Anfrage",
            "fileName": "anlage.pdf",
            "mimeType": "application/pdf",
            "date": "2013-01-04",
            "sha1Checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
            "size": 82930,
            "accessUrl": "https://oparl.example.org/files/57739.pdf",
            "downloadUrl": "https://oparl.example.org/files/download/57739.pdf",
            "text": "Der Übersichtsplan zeigt alle Ebenen des ...",
            "masterFile": "https://oparl.example.org/files/57738",
            "license": "http://www.opendefinition.org/licenses/cc-by",
            "created": "2013-01-04T07:54:13+01:00",
            "modified": "2013-01-04T07:54:13+01:00"
        }
    ],
    "location": [
        {
            "id": "https://oparl.example.org/locations/29856",
            "type": "https://schema.oparl.org/1.1/Location",
            "description": "Honschaftsstraße 312, Köln",
            "created": "2012-01-08T14:05:27+01:00",
            "modified": "2012-01-08T14:05:27+01:00",
            "geojson": {
                "type": "Point",
                "coordinates": [
                    7.03291,
                    50.98249
                ]
            }
        }
    ],
    "originatorPerson": [
        "https://oparl.example.org/person/2000",
        "https://oparl.example.org/person/1000"
    ],
    "originatorOrganization": [
        "https://oparl.example.org/organization/2000",
        "https://oparl.example.org/organization/1000"
    ],
    "consultation": [
        {
            "id": "https://oparl.example.org/consultation/47594",
            "type": "https://schema.oparl.org/1.1/Consultation",
            "agendaItem": "https://oparl.example.org/agendaitem/15569",
            "meeting": "https://oparl.example.org/meeting/243",
            "organization": [
                "https://oparl.example.org/organization/96"
            ],
            "authoritative": false,
            "role": "Beschlussfassung",
            "created": "2012-01-08T14:05:27+01:00",
            "modified": "2012-01-08T14:05:27+01:00"
        }
    ],
    "underDirectionOf": [
        "https://oparl.example.org/organization/2000"
    ],
    "created": "2013-01-08T12:05:27+01:00",
    "modified": "2013-01-08T12:05:27+01:00"})
    }

    #[test]
    fn serialize() {
        assert_eq!(json!(example_paper()), example_paper_json());
    }

    #[test]
    fn deserialize_good() {
        let deserialized: Paper = serde_json::from_value(example_paper_json())
            .expect("value must be deserializable as Paper");
        assert_eq!(deserialized, example_paper());
    }

    #[test]
    fn deserialize_bad() {
        assert!(serde_json::from_value::<Paper>(json!("xyzabcd")).is_err());
        assert!(serde_json::from_value::<Paper>(json!(true)).is_err());
        assert!(serde_json::from_value::<Paper>(json!(123)).is_err());
    }
}