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
// This file is part of rss.
//
// Copyright © 2015-2017 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 error::Error;
use extension::Extension;
use extension::remove_extension_values;
use quick_xml::XmlWriter;
use quick_xml::error::Error as XmlError;
use std::collections::HashMap;
use toxml::{ToXml, XmlWriterExt};
/// The Dublin Core XML namespace.
pub static NAMESPACE: &'static str = "http://purl.org/dc/elements/1.1/";
/// A Dublin Core element extension.
#[derive(Debug, Default, Clone, PartialEq)]
pub struct DublinCoreExtension {
/// An entity responsible for making contributions to the resource.
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.
coverages: Vec<String>,
/// An entity primarily responsible for making the resource.
creators: Vec<String>,
/// A point or period of time associated with an event in the lifecycle of the resource.
dates: Vec<String>,
/// An account of the resource.
descriptions: Vec<String>,
/// The file format, physical medium, or dimensions of the resource.
formats: Vec<String>,
/// An unambiguous reference to the resource within a given context.
identifiers: Vec<String>,
/// A language of the resource.
languages: Vec<String>,
/// An entity responsible for making the resource available.
publishers: Vec<String>,
/// A related resource.
relations: Vec<String>,
/// Information about rights held in and over the resource.
rights: Vec<String>,
/// A related resource from which the described resource is derived.
sources: Vec<String>,
/// The topic of the resource.
subjects: Vec<String>,
/// A name given to the resource.
titles: Vec<String>,
/// The nature or genre of the resource.
resource_types: Vec<String>,
}
impl DublinCoreExtension {
/// Get the contributors that exists under `DublinCoreExtension`.
pub fn contributors(&self) -> &[String] {
&self.contributors
}
/// Get the coverages that exists under `DublinCoreExtension`.
pub fn coverages(&self) -> &[String] {
&self.coverages
}
/// Get the creators that exists under `DublinCoreExtension`.
pub fn creators(&self) -> &[String] {
&self.creators
}
/// Get the dates that exists under `DublinCoreExtension`.
pub fn dates(&self) -> &[String] {
&self.dates
}
/// Get the descriptions that exists under `DublinCoreExtension`.
pub fn descriptions(&self) -> &[String] {
&self.descriptions
}
/// Get the formats that exists under `DublinCoreExtension`.
pub fn formats(&self) -> &[String] {
&self.formats
}
/// Get the identifiers that exists under `DublinCoreExtension`.
pub fn identifiers(&self) -> &[String] {
&self.identifiers
}
/// Get the languages that exists under `DublinCoreExtension`.
pub fn languages(&self) -> &[String] {
&self.languages
}
/// Get the publishers that exists under `DublinCoreExtension`.
pub fn publishers(&self) -> &[String] {
&self.publishers
}
/// Get the relations that exists under `DublinCoreExtension`.
pub fn relations(&self) -> &[String] {
&self.relations
}
/// Get the rights that exists under `DublinCoreExtension`.
pub fn rights(&self) -> &[String] {
&self.rights
}
/// Get the sources that exists under `DublinCoreExtension`.
pub fn sources(&self) -> &[String] {
&self.sources
}
/// Get the subjects that exists under `DublinCoreExtension`.
pub fn subjects(&self) -> &[String] {
&self.subjects
}
/// Get the titles that exists under `DublinCoreExtension`.
pub fn titles(&self) -> &[String] {
&self.titles
}
/// Get the resource_types that exists under `DublinCoreExtension`.
pub fn resource_types(&self) -> &[String] {
&self.resource_types
}
}
impl DublinCoreExtension {
/// Creates a DublinCoreExtension using the specified hashmap.
pub fn from_map(mut map: HashMap<String, Vec<Extension>>) -> Self {
let mut ext = DublinCoreExtension::default();
ext.contributors = remove_extension_values(&mut map, "contributor").unwrap_or_default();
ext.coverages = remove_extension_values(&mut map, "coverage").unwrap_or_default();
ext.creators = remove_extension_values(&mut map, "creator").unwrap_or_default();
ext.dates = remove_extension_values(&mut map, "date").unwrap_or_default();
ext.descriptions = remove_extension_values(&mut map, "description").unwrap_or_default();
ext.formats = remove_extension_values(&mut map, "format").unwrap_or_default();
ext.identifiers = remove_extension_values(&mut map, "identifier").unwrap_or_default();
ext.languages = remove_extension_values(&mut map, "language").unwrap_or_default();
ext.publishers = remove_extension_values(&mut map, "publisher").unwrap_or_default();
ext.relations = remove_extension_values(&mut map, "relation").unwrap_or_default();
ext.rights = remove_extension_values(&mut map, "rights").unwrap_or_default();
ext.sources = remove_extension_values(&mut map, "source").unwrap_or_default();
ext.subjects = remove_extension_values(&mut map, "subject").unwrap_or_default();
ext.titles = remove_extension_values(&mut map, "title").unwrap_or_default();
ext.resource_types = remove_extension_values(&mut map, "type").unwrap_or_default();
ext
}
}
impl ToXml for DublinCoreExtension {
fn to_xml<W: ::std::io::Write>(&self, writer: &mut XmlWriter<W>) -> Result<(), XmlError> {
writer
.write_text_elements(b"dc:contributor", &self.contributors)?;
writer.write_text_elements(b"dc:coverage", &self.coverages)?;
writer.write_text_elements(b"dc:creator", &self.creators)?;
writer.write_text_elements(b"dc:date", &self.dates)?;
writer
.write_text_elements(b"dc:description", &self.descriptions)?;
writer.write_text_elements(b"dc:format", &self.formats)?;
writer
.write_text_elements(b"dc:identifier", &self.identifiers)?;
writer.write_text_elements(b"dc:language", &self.languages)?;
writer
.write_text_elements(b"dc:publisher", &self.publishers)?;
writer.write_text_elements(b"dc:relation", &self.relations)?;
writer.write_text_elements(b"dc:rights", &self.rights)?;
writer.write_text_elements(b"dc:source", &self.sources)?;
writer.write_text_elements(b"dc:subject", &self.subjects)?;
writer.write_text_elements(b"dc:title", &self.titles)?;
writer.write_text_elements(b"dc:type", &self.resource_types)
}
}
/// This `DublinCoreExtensionBuilder` struct creates the `DublinCoreExtension`.
#[derive(Debug, Default, Clone)]
pub struct DublinCoreExtensionBuilder {
/// An entity responsible for making contributions to the resource.
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.
coverages: Vec<String>,
/// An entity primarily responsible for making the resource.
creators: Vec<String>,
/// A point or period of time associated with an event in the lifecycle of the resource.
dates: Vec<String>,
/// An account of the resource.
descriptions: Vec<String>,
/// The file format, physical medium, or dimensions of the resource.
formats: Vec<String>,
/// An unambiguous reference to the resource within a given context.
identifiers: Vec<String>,
/// A language of the resource.
languages: Vec<String>,
/// An entity responsible for making the resource available.
publishers: Vec<String>,
/// A related resource.
relations: Vec<String>,
/// Information about rights held in and over the resource.
rights: Vec<String>,
/// A related resource from which the described resource is derived.
sources: Vec<String>,
/// The topic of the resource.
subjects: Vec<String>,
/// A name given to the resource.
titles: Vec<String>,
/// The nature or genre of the resource.
resource_types: Vec<String>,
}
impl DublinCoreExtensionBuilder {
/// Construct a new `DublinCoreExtensionBuilder` and return default values.
///
/// # Examples
///
/// ```
/// use rss::extension::dublincore::DublinCoreExtensionBuilder;
///
/// let dublin_builder = DublinCoreExtensionBuilder::new();
/// ```
pub fn new() -> DublinCoreExtensionBuilder {
DublinCoreExtensionBuilder::default()
}
/// Set the contributors that exists under `DublinCoreExtension`.
pub fn contributors(mut self, contributors: Vec<String>) -> DublinCoreExtensionBuilder {
self.contributors = contributors;
self
}
/// Set the coverages that exists under `DublinCoreExtension`.
pub fn coverages(mut self, coverages: Vec<String>) -> DublinCoreExtensionBuilder {
self.coverages = coverages;
self
}
/// Set the creators that exists under `DublinCoreExtension`.
pub fn creators(mut self, creators: Vec<String>) -> DublinCoreExtensionBuilder {
self.creators = creators;
self
}
/// Set the dates that exists under `DublinCoreExtension`.
pub fn dates(mut self, dates: Vec<String>) -> DublinCoreExtensionBuilder {
self.dates = dates;
self
}
/// Set the descriptions that exists under `DublinCoreExtension`.
pub fn descriptions(mut self, descriptions: Vec<String>) -> DublinCoreExtensionBuilder {
self.descriptions = descriptions;
self
}
/// Set the formats that exists under `DublinCoreExtension`.
pub fn formats(mut self, formats: Vec<String>) -> DublinCoreExtensionBuilder {
self.formats = formats;
self
}
/// Set the identifiers that exists under `DublinCoreExtension`.
pub fn identifiers(mut self, identifiers: Vec<String>) -> DublinCoreExtensionBuilder {
self.identifiers = identifiers;
self
}
/// Set the languages that exists under `DublinCoreExtension`.
pub fn languages(mut self, languages: Vec<String>) -> DublinCoreExtensionBuilder {
self.languages = languages;
self
}
/// Set the publishers that exists under `DublinCoreExtension`.
pub fn publishers(mut self, publishers: Vec<String>) -> DublinCoreExtensionBuilder {
self.publishers = publishers;
self
}
/// Set the relations that exists under `DublinCoreExtension`.
pub fn relations(mut self, relations: Vec<String>) -> DublinCoreExtensionBuilder {
self.relations = relations;
self
}
/// Set the rights that exists under `DublinCoreExtension`.
pub fn rights(mut self, rights: Vec<String>) -> DublinCoreExtensionBuilder {
self.rights = rights;
self
}
/// Set the sources that exists under `DublinCoreExtension`.
pub fn sources(mut self, sources: Vec<String>) -> DublinCoreExtensionBuilder {
self.sources = sources;
self
}
/// Set the subjects that exists under `DublinCoreExtension`.
pub fn subjects(mut self, subjects: Vec<String>) -> DublinCoreExtensionBuilder {
self.subjects = subjects;
self
}
/// Set the titles that exists under `DublinCoreExtension`.
pub fn titles(mut self, titles: Vec<String>) -> DublinCoreExtensionBuilder {
self.titles = titles;
self
}
/// Set the resource_types that exists under `DublinCoreExtension`.
pub fn resource_types(mut self, resource_types: Vec<String>) -> DublinCoreExtensionBuilder {
self.resource_types = resource_types;
self
}
/// Construct the `DublinCoreExtension` from the `DublinCoreExtensionBuilder`.
pub fn finalize(self) -> Result<DublinCoreExtension, Error> {
Ok(DublinCoreExtension {
contributors: self.contributors,
coverages: self.coverages,
creators: self.creators,
dates: self.dates,
descriptions: self.descriptions,
formats: self.formats,
identifiers: self.identifiers,
languages: self.languages,
publishers: self.publishers,
relations: self.relations,
rights: self.rights,
sources: self.sources,
subjects: self.subjects,
titles: self.titles,
resource_types: self.resource_types,
})
}
}