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
412
413
414
415
416
417
418
419
420
421
// This file is part of rss.
//
// Copyright © 2015-2021 The rust-syndication Developers
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the MIT License and/or Apache 2.0 License.

use std::collections::BTreeMap;
use std::io::Write;

use quick_xml::Error as XmlError;
use quick_xml::Writer;

use crate::extension::util::get_extension_values;
use crate::extension::Extension;

use crate::toxml::{ToXml, WriterExt};

/// The Dublin Core XML namespace.
pub const NAMESPACE: &str = "http://purl.org/dc/elements/1.1/";

/// A Dublin Core element extension.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Default, Clone, PartialEq)]
#[cfg_attr(feature = "builders", derive(Builder))]
#[cfg_attr(
    feature = "builders",
    builder(
        setter(into),
        default,
        build_fn(name = "build_impl", private, error = "never::Never")
    )
)]
pub struct DublinCoreExtension {
    /// An entity responsible for making contributions to the resource.
    #[cfg_attr(feature = "builders", builder(setter(each = "contributor")))]
    pub contributors: Vec<String>,
    /// The spatial or temporal topic of the resource, the spatial applicability of the resource,
    /// or the jurisdiction under which the resource is relevant.
    #[cfg_attr(feature = "builders", builder(setter(each = "coverage")))]
    pub coverages: Vec<String>,
    /// An entity primarily responsible for making the resource.
    #[cfg_attr(feature = "builders", builder(setter(each = "creator")))]
    pub creators: Vec<String>,
    /// A point or period of time associated with an event in the lifecycle of the resource.
    #[cfg_attr(feature = "builders", builder(setter(each = "date")))]
    pub dates: Vec<String>,
    /// An account of the resource.
    #[cfg_attr(feature = "builders", builder(setter(each = "description")))]
    pub descriptions: Vec<String>,
    /// The file format, physical medium, or dimensions of the resource.
    #[cfg_attr(feature = "builders", builder(setter(each = "format")))]
    pub formats: Vec<String>,
    /// An unambiguous reference to the resource within a given context.
    #[cfg_attr(feature = "builders", builder(setter(each = "identifier")))]
    pub identifiers: Vec<String>,
    /// A language of the resource.
    #[cfg_attr(feature = "builders", builder(setter(each = "language")))]
    pub languages: Vec<String>,
    /// An entity responsible for making the resource available.
    #[cfg_attr(feature = "builders", builder(setter(each = "publisher")))]
    pub publishers: Vec<String>,
    /// A related resource.
    #[cfg_attr(feature = "builders", builder(setter(each = "relation")))]
    pub relations: Vec<String>,
    /// Information about rights held in and over the resource.
    #[cfg_attr(feature = "builders", builder(setter(each = "right")))]
    pub rights: Vec<String>,
    /// A related resource from which the described resource is derived.
    #[cfg_attr(feature = "builders", builder(setter(each = "source")))]
    pub sources: Vec<String>,
    /// The topic of the resource.
    #[cfg_attr(feature = "builders", builder(setter(each = "subject")))]
    pub subjects: Vec<String>,
    /// A name given to the resource.
    #[cfg_attr(feature = "builders", builder(setter(each = "title")))]
    pub titles: Vec<String>,
    /// The nature or genre of the resource.
    #[cfg_attr(feature = "builders", builder(setter(each = "r#type")))]
    pub types: Vec<String>,
}

impl DublinCoreExtension {
    /// Return the contributors to the resource.
    pub fn contributors(&self) -> &[String] {
        &self.contributors
    }

    /// Return a mutable slice of the contributors to the resource.
    pub fn contributors_mut(&mut self) -> &mut [String] {
        &mut self.contributors
    }

    /// Set the contributors to the resource.
    pub fn set_contributors<V>(&mut self, contributors: V)
    where
        V: Into<Vec<String>>,
    {
        self.contributors = contributors.into();
    }

    /// Return the spatial or temporal topics of the resource, the spatial applicabilities of the
    /// resource, or the jurisdictions under which the resource is relevant.
    pub fn coverages(&self) -> &[String] {
        &self.coverages
    }

    /// Return a mutable slice of the spatial or temporal topics of the resource, the spatial
    /// applicabilities of the resource, or the jurisdictions under which the resource is relevant.
    pub fn coverages_mut(&mut self) -> &mut [String] {
        &mut self.coverages
    }

    /// Set the spatial or temporal topics of the resource, the spatial applicabilities of the
    /// resource, or the jurisdictions under which the resource is relevant.
    pub fn set_coverages<V>(&mut self, coverages: V)
    where
        V: Into<Vec<String>>,
    {
        self.coverages = coverages.into();
    }

    /// Return the creators of the resource.
    pub fn creators(&self) -> &[String] {
        &self.creators
    }

    /// Return a mutable slice of the creators of the resource.
    pub fn creators_mut(&mut self) -> &mut [String] {
        &mut self.creators
    }

    /// Set the creators of the resource.
    pub fn set_creators<V>(&mut self, creators: V)
    where
        V: Into<Vec<String>>,
    {
        self.creators = creators.into();
    }

    /// Return the times associated with the resource.
    pub fn dates(&self) -> &[String] {
        &self.dates
    }

    /// Return a mutable slice of the times associated with the resource.
    pub fn dates_mut(&mut self) -> &mut [String] {
        &mut self.dates
    }

    /// Set the times associated with the resource.
    pub fn set_dates<V>(&mut self, dates: V)
    where
        V: Into<Vec<String>>,
    {
        self.dates = dates.into();
    }

    /// Return the descriptions of the resource.
    pub fn descriptions(&self) -> &[String] {
        &self.descriptions
    }

    /// Return a mutable slice of the descriptions of the resource.
    pub fn descriptions_mut(&mut self) -> &mut [String] {
        &mut self.descriptions
    }

    /// Set the descriptions of the resource.
    pub fn set_descriptions<V>(&mut self, descriptions: V)
    where
        V: Into<Vec<String>>,
    {
        self.descriptions = descriptions.into();
    }

    /// Return the file formats, physical mediums, or dimensions of the resource.
    pub fn formats(&self) -> &[String] {
        &self.formats
    }

    /// Return a mutable slice of the file formats, physical mediums, or
    /// dimensions of the resource.
    pub fn formats_mut(&mut self) -> &mut [String] {
        &mut self.formats
    }

    /// Set the file formats, physical mediums, or dimensions of the resource.
    pub fn set_formats<V>(&mut self, formats: V)
    where
        V: Into<Vec<String>>,
    {
        self.formats = formats.into();
    }

    /// Return the identifiers of the resource.
    pub fn identifiers(&self) -> &[String] {
        &self.identifiers
    }

    /// Return a mutable slice of the identifiers of the resource.
    pub fn identifiers_mut(&mut self) -> &mut [String] {
        &mut self.identifiers
    }

    /// Set the identifiers of the resource.
    pub fn set_identifiers<V>(&mut self, identifiers: V)
    where
        V: Into<Vec<String>>,
    {
        self.identifiers = identifiers.into();
    }

    /// Return the languages of the resource.
    pub fn languages(&self) -> &[String] {
        &self.languages
    }

    /// Return a mutable slice of the languages of the resource.
    pub fn languages_mut(&mut self) -> &mut [String] {
        &mut self.languages
    }

    /// Set the languages of the resource.
    pub fn set_languages<V>(&mut self, languages: V)
    where
        V: Into<Vec<String>>,
    {
        self.languages = languages.into();
    }

    /// Return the publishers of the resource.
    pub fn publishers(&self) -> &[String] {
        &self.publishers
    }

    /// Return a mutable slice of the publishers of the resource.
    pub fn publishers_mut(&mut self) -> &mut [String] {
        &mut self.publishers
    }

    /// Set the publishers of the resource.
    pub fn set_publishers<V>(&mut self, publishers: V)
    where
        V: Into<Vec<String>>,
    {
        self.publishers = publishers.into();
    }

    /// Return the related resources.
    pub fn relations(&self) -> &[String] {
        &self.relations
    }

    /// Return a mutable slice of the related resources.
    pub fn relations_mut(&mut self) -> &mut [String] {
        &mut self.relations
    }

    /// Set the related resources.
    pub fn set_relations<V>(&mut self, relations: V)
    where
        V: Into<Vec<String>>,
    {
        self.relations = relations.into();
    }

    /// Return the information about rights held in and over the resource.
    pub fn rights(&self) -> &[String] {
        &self.rights
    }

    /// Return a mutable slice of the information about rights held in and over
    /// the resource.
    pub fn rights_mut(&mut self) -> &mut [String] {
        &mut self.rights
    }

    /// Set the information about rights held in and over the resource.
    pub fn set_rights<V>(&mut self, rights: V)
    where
        V: Into<Vec<String>>,
    {
        self.rights = rights.into();
    }

    /// Return the sources of the resource.
    pub fn sources(&self) -> &[String] {
        &self.sources
    }

    /// Return a mutable slice of the sources of the resource.
    pub fn sources_mut(&mut self) -> &mut [String] {
        &mut self.sources
    }

    /// Set the sources of the resource.
    pub fn set_sources<V>(&mut self, sources: V)
    where
        V: Into<Vec<String>>,
    {
        self.sources = sources.into();
    }

    /// Return the topics of the resource.
    pub fn subjects(&self) -> &[String] {
        &self.subjects
    }

    /// Return a mutable slice of the subjects of the resource.
    pub fn subjects_mut(&mut self) -> &mut [String] {
        &mut self.subjects
    }

    /// Set the topics of the resource.
    pub fn set_subjects<V>(&mut self, subjects: V)
    where
        V: Into<Vec<String>>,
    {
        self.subjects = subjects.into();
    }

    /// Return the titles of the resource.
    pub fn titles(&self) -> &[String] {
        &self.titles
    }

    /// Return a mutable slice of the titles of the resource.
    pub fn titles_mut(&mut self) -> &mut [String] {
        &mut self.titles
    }

    /// Set the titles of the resource.
    pub fn set_titles<V>(&mut self, titles: V)
    where
        V: Into<Vec<String>>,
    {
        self.titles = titles.into();
    }

    /// Return the natures or genres of the resource.
    pub fn types(&self) -> &[String] {
        &self.types
    }

    /// Return a mutable slice of the natures or genres of the resource.
    pub fn types_mut(&mut self) -> &mut [String] {
        &mut self.types
    }

    /// set the natures or genres of the resource.
    pub fn set_types<V>(&mut self, types: V)
    where
        V: Into<Vec<String>>,
    {
        self.types = types.into();
    }
}

impl DublinCoreExtension {
    /// Creates a `DublinCoreExtension` using the specified `BTreeMap`.
    pub fn from_map(map: BTreeMap<String, Vec<Extension>>) -> Self {
        let mut ext = DublinCoreExtension::default();
        for (key, v) in map {
            match key.as_str() {
                "contributor" => ext.contributors = get_extension_values(v),
                "coverage" => ext.coverages = get_extension_values(v),
                "creator" => ext.creators = get_extension_values(v),
                "date" => ext.dates = get_extension_values(v),
                "description" => ext.descriptions = get_extension_values(v),
                "format" => ext.formats = get_extension_values(v),
                "identifier" => ext.identifiers = get_extension_values(v),
                "language" => ext.languages = get_extension_values(v),
                "publisher" => ext.publishers = get_extension_values(v),
                "relation" => ext.relations = get_extension_values(v),
                "rights" => ext.rights = get_extension_values(v),
                "source" => ext.sources = get_extension_values(v),
                "subject" => ext.subjects = get_extension_values(v),
                "title" => ext.titles = get_extension_values(v),
                "type" => ext.types = get_extension_values(v),
                _ => {}
            }
        }
        ext
    }
}

impl ToXml for DublinCoreExtension {
    fn to_xml<W: Write>(&self, writer: &mut Writer<W>) -> Result<(), XmlError> {
        writer.write_text_elements("dc:contributor", &self.contributors)?;
        writer.write_text_elements("dc:coverage", &self.coverages)?;
        writer.write_text_elements("dc:creator", &self.creators)?;
        writer.write_text_elements("dc:date", &self.dates)?;
        writer.write_text_elements("dc:description", &self.descriptions)?;
        writer.write_text_elements("dc:format", &self.formats)?;
        writer.write_text_elements("dc:identifier", &self.identifiers)?;
        writer.write_text_elements("dc:language", &self.languages)?;
        writer.write_text_elements("dc:publisher", &self.publishers)?;
        writer.write_text_elements("dc:relation", &self.relations)?;
        writer.write_text_elements("dc:rights", &self.rights)?;
        writer.write_text_elements("dc:source", &self.sources)?;
        writer.write_text_elements("dc:subject", &self.subjects)?;
        writer.write_text_elements("dc:title", &self.titles)?;
        writer.write_text_elements("dc:type", &self.types)?;
        Ok(())
    }

    fn used_namespaces(&self) -> BTreeMap<String, String> {
        let mut namespaces = BTreeMap::new();
        namespaces.insert("dc".to_owned(), NAMESPACE.to_owned());
        namespaces
    }
}

#[cfg(feature = "builders")]
impl DublinCoreExtensionBuilder {
    /// Builds a new `DublinCoreExtension`.
    pub fn build(&self) -> DublinCoreExtension {
        self.build_impl().unwrap()
    }
}