marc 3.1.1

Reader and builder for MARC 21 Bibliographic format
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
422
423
424
425
426
427
428
429
430
431
432
433
434
//! # Module to convert MARC 21 records to MARC XML

use crate::{Error, Field, Record, Result, Subfield};
use std::io::Write;
use xml::writer::{EmitterConfig, EventWriter, XmlEvent};

const MARCXML_NS: &[(&str, &str)] = &[
    ("xmlns:marc", "http://www.loc.gov/MARC21/slim"),
    ("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"),
    (
        "xsi:schemaLocation",
        "http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd",
    ),
];

pub trait XmlElement {
    fn xml_element<W: Write>(&self, w: &mut EventWriter<W>) -> Result<()>;
}

pub trait XmlRootElement<T: XmlElement> {
    fn xml_root_element<W: Write>(&self, w: &mut EventWriter<W>) -> Result<()>;
}

/// Output a single record or a collection of records as MARC XML.
///
/// ## Examples
///
/// ### Outputting a single record
///
/// ```rust
/// # use marc::*;
/// # fn main() -> Result<()> {
/// let mut builder = RecordBuilder::new();
/// let record = builder
///     .add_fields(fields!(
///         control fields: [
///             b"001" => "000000002",
///             b"003" => "RuMoRGB",
///         ];
///         data fields: [
///             b"979", b"  ", [
///                 b'a' => "autoref",
///                 b'a' => "dlopen",
///             ],
///         ];
///     ))?
///     .get_record()?;
/// assert_eq!(String::from_utf8(record.xml_minified()?).unwrap(), "<?xml version=\"1.0\" encoding=\
/// \"utf-8\"?><marc:record xmlns:marc=\"http://www.loc.gov/MARC21/slim\" xmlns:xsi=\"http://www.w3\
/// .org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.loc.gov/MARC21/slim http://www.l\
/// oc.gov/standards/marcxml/schema/MARC21slim.xsd\"><marc:leader>00100nam  2200061 i 4500</marc:le\
/// ader><marc:controlfield tag=\"001\">000000002</marc:controlfield><marc:controlfield tag=\"003\"\
/// >RuMoRGB</marc:controlfield><marc:datafield tag=\"979\" ind1=\" \" ind2=\" \"><marc:subfield co\
/// de=\"a\">autoref</marc:subfield><marc:subfield code=\"a\">dlopen</marc:subfield></marc:datafiel\
/// d></marc:record>".to_string());
/// # Ok(())
/// # }
/// ```
///
/// ### Outputting a collection of records
///
/// ```rust
/// # use marc::*;
/// # fn main() -> Result<()> {
/// let mut builder = RecordBuilder::new();
/// let records = vec![builder
///     .add_fields(fields!(
///         control fields: [
///             b"001" => "000000002",
///             b"003" => "RuMoRGB",
///         ];
///         data fields: [
///             b"979", b"  ", [
///                 b'a' => "autoref",
///                 b'a' => "dlopen",
///             ],
///         ];
///     ))?
///     .get_record()?];
/// assert_eq!(String::from_utf8(records.xml_minified()?).unwrap(), "<?xml version=\"1.0\" enc\
/// oding=\"utf-8\"?><marc:collection xmlns:marc=\"http://www.loc.gov/MARC21/slim\" xmlns:xsi=\"htt\
/// p://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.loc.gov/MARC21/slim ht\
/// tp://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd\"><marc:record><marc:leader>00100nam  \
/// 2200061 i 4500</marc:leader><marc:controlfield tag=\"001\">000000002</marc:controlfield><marc:c\
/// ontrolfield tag=\"003\">RuMoRGB</marc:controlfield><marc:datafield tag=\"979\" ind1=\" \" ind2=\
/// \" \"><marc:subfield code=\"a\">autoref</marc:subfield><marc:subfield code=\"a\">dlopen</marc:s\
/// ubfield></marc:datafield></marc:record></marc:collection>".to_string());
/// # Ok(())
/// # }
/// ```
#[cfg_attr(docsrs, doc(cfg(feature = "xml")))]
pub trait MarcXml<'a>
where
    Self: XmlRootElement<Record<'a>>,
{
    /// Output MARC XML
    fn xml(&self, pretty_print: bool) -> Result<Vec<u8>> {
        let mut buffer = Vec::new();
        let mut writer = EmitterConfig::new()
            .perform_indent(pretty_print)
            .create_writer(&mut buffer);

        self.xml_root_element(&mut writer)?;
        Ok(buffer)
    }

    /// Output minified (outdented) MARC XML
    fn xml_minified(&self) -> Result<Vec<u8>> {
        Self::xml(self, false)
    }

    /// Output pretty-print (indented) MARC XML
    fn xml_pretty(&self) -> Result<Vec<u8>> {
        Self::xml(self, true)
    }
}

impl<'a> MarcXml<'a> for Vec<Record<'a>> {}
impl<'a> MarcXml<'a> for Record<'a> {}

impl XmlRootElement<Record<'_>> for Vec<Record<'_>> {
    fn xml_root_element<W: Write>(&self, w: &mut EventWriter<W>) -> Result<()> {
        write_element("marc:collection", MARCXML_NS.to_vec(), w, |w| {
            for record in self {
                record.xml_element(w)?;
            }
            Ok(())
        })?;
        Ok(())
    }
}

impl XmlRootElement<Record<'_>> for Record<'_> {
    fn xml_root_element<W: Write>(&self, w: &mut EventWriter<W>) -> Result<()> {
        write_element("marc:record", MARCXML_NS.to_vec(), w, |w| {
            write_element("marc:leader", vec![], w, |w| {
                w.write(XmlEvent::Characters(&String::from_utf8_lossy(
                    &self.as_ref()[0..24],
                )))
                .map_err(Into::into)
            })?;
            for field in self.fields() {
                field.xml_element(w)?;
            }
            Ok(())
        })
    }
}

impl XmlElement for Record<'_> {
    fn xml_element<W: Write>(&self, w: &mut EventWriter<W>) -> Result<()> {
        write_element("marc:record", vec![], w, |w| {
            write_element("marc:leader", vec![], w, |w| {
                w.write(XmlEvent::Characters(&String::from_utf8_lossy(
                    &self.as_ref()[0..24],
                )))
                .map_err(Into::into)
            })?;
            for field in self.fields() {
                field.xml_element(w)?;
            }
            Ok(())
        })
    }
}

impl XmlElement for Field<'_> {
    fn xml_element<W: Write>(&self, w: &mut EventWriter<W>) -> Result<()> {
        let tag = self.get_tag();
        match tag.0 {
            [b'0', b'0', ..] => {
                let attributes = vec![("tag", tag.as_str())];
                write_element("marc:controlfield", attributes, w, |w| {
                    w.write(XmlEvent::Characters(self.get_data::<str>()))
                        .map_err(Into::into)
                })?;
            }
            _ => {
                let indicator = self.get_indicator();
                let attributes = vec![
                    ("tag", tag.as_str()),
                    ("ind1", indicator.first()),
                    ("ind2", indicator.second()),
                ];
                write_element("marc:datafield", attributes, w, |w| {
                    for subfield in self.subfields() {
                        subfield.xml_element(w)?;
                    }
                    Ok(())
                })?;
            }
        }
        Ok(())
    }
}

impl XmlElement for Subfield<'_> {
    fn xml_element<W: Write>(&self, w: &mut EventWriter<W>) -> Result<()> {
        let code: &str = &self.get_identifier().as_char().to_string();
        let attributes = vec![("code", code)];
        write_element("marc:subfield", attributes, w, |w| {
            w.write(XmlEvent::Characters(self.get_data::<str>()))
                .map_err(Into::into)
        })?;
        Ok(())
    }
}

fn write_element<W: Write, F: Fn(&mut EventWriter<W>) -> Result<()>>(
    element: &str,
    attr: Vec<(&str, &str)>,
    w: &mut EventWriter<W>,
    f: F,
) -> Result<()> {
    let mut event_builder = XmlEvent::start_element(element);

    for &(k, v) in attr.iter() {
        event_builder = event_builder.attr(k, v);
    }

    let mut event: XmlEvent<'_> = event_builder.into();
    w.write(event)?;
    f(w)?;
    event = XmlEvent::end_element().into();
    w.write(event).map_err(Into::into)
}

impl From<xml::writer::Error> for Error {
    fn from(error: xml::writer::Error) -> Error {
        Error::XmlError(error)
    }
}

#[cfg(test)]
mod tests {
    use crate::{fields, MarcXml, Record, RecordBuilder};
    use xml::writer::XmlEvent;
    use xml::EmitterConfig;

    fn test_record() -> Record<'static> {
        let mut builder = RecordBuilder::new();
        builder
            .add_fields(fields!(
                data fields: [
                    b"264", b" 1", [
                        b'a' => "León, Spain",
                    ],
                    b"245", b"00", [
                        b'a' => "Book title",
                        b'b' => "Book Subtitle",
                    ],
                    b"100", b"1 ", [
                        b'a' => "Author Name",
                    ],
                    b"041", b"0 ", [
                        b'a' => "eng",
                    ],
                ];
                control fields: [
                    b"008" => "210128t20212021enka    sb    000 0 eng d",
                    b"001" => "000000001",
                ];
            ))
            .unwrap();
        builder.get_record().unwrap()
    }

    fn test_records() -> Vec<Record<'static>> {
        vec![test_record()]
    }

    #[test]
    fn should_output_minified_xml_record() {
        let minified_xml = test_record()
            .xml_minified()
            .map(String::from_utf8)
            .unwrap()
            .unwrap();

        let expected = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\
                               <marc:record xmlns:marc=\"http://www.loc.gov/MARC21/slim\" \
                               xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \
                               xsi:schemaLocation=\"http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd\">\
                               <marc:leader>00220nam  2200097 i 4500</marc:leader>\
                               <marc:controlfield tag=\"001\">000000001</marc:controlfield>\
                               <marc:controlfield tag=\"008\">210128t20212021enka    sb    000 0 eng d</marc:controlfield>\
                               <marc:datafield tag=\"041\" ind1=\"0\" ind2=\" \">\
                               <marc:subfield code=\"a\">eng</marc:subfield>\
                               </marc:datafield>\
                               <marc:datafield tag=\"100\" ind1=\"1\" ind2=\" \">\
                               <marc:subfield code=\"a\">Author Name</marc:subfield>\
                               </marc:datafield>\
                               <marc:datafield tag=\"245\" ind1=\"0\" ind2=\"0\">\
                               <marc:subfield code=\"a\">Book title</marc:subfield>\
                               <marc:subfield code=\"b\">Book Subtitle</marc:subfield>\
                               </marc:datafield>\
                               <marc:datafield tag=\"264\" ind1=\" \" ind2=\"1\">\
                               <marc:subfield code=\"a\">León, Spain</marc:subfield>\
                               </marc:datafield>\
                               </marc:record>".to_string();
        assert_eq!(minified_xml, expected);
    }

    #[test]
    fn should_output_pretty_xml_record() {
        let minified_xml = test_record()
            .xml_pretty()
            .map(String::from_utf8)
            .unwrap()
            .unwrap();

        let expected = "\
            <?xml version=\"1.0\" encoding=\"utf-8\"?>\n\
            <marc:record \
                xmlns:marc=\"http://www.loc.gov/MARC21/slim\" \
                xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \
                xsi:schemaLocation=\"http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd\"\
            >\n  \
                <marc:leader>00220nam  2200097 i 4500</marc:leader>\n  \
                <marc:controlfield tag=\"001\">000000001</marc:controlfield>\n  \
                <marc:controlfield tag=\"008\">210128t20212021enka    sb    000 0 eng d</marc:controlfield>\n  \
                <marc:datafield tag=\"041\" ind1=\"0\" ind2=\" \">\n    \
                    <marc:subfield code=\"a\">eng</marc:subfield>\n  \
                </marc:datafield>\n  <marc:datafield tag=\"100\" ind1=\"1\" ind2=\" \">\n    \
                    <marc:subfield code=\"a\">Author Name</marc:subfield>\n  \
                </marc:datafield>\n  \
                <marc:datafield tag=\"245\" ind1=\"0\" ind2=\"0\">\n    \
                    <marc:subfield code=\"a\">Book title</marc:subfield>\n    \
                    <marc:subfield code=\"b\">Book Subtitle</marc:subfield>\n  \
                </marc:datafield>\n  \
                <marc:datafield tag=\"264\" ind1=\" \" ind2=\"1\">\n    \
                    <marc:subfield code=\"a\">León, Spain</marc:subfield>\n  \
                </marc:datafield>\n\
            </marc:record>".to_string();

        assert_eq!(minified_xml, expected);
    }

    #[test]
    fn should_output_minified_xml_collection() {
        let minified_xml = test_records()
            .xml_minified()
            .map(String::from_utf8)
            .unwrap()
            .unwrap();

        let expected = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\
                               <marc:collection xmlns:marc=\"http://www.loc.gov/MARC21/slim\" \
                               xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \
                               xsi:schemaLocation=\"http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd\">\
                               <marc:record>\
                               <marc:leader>00220nam  2200097 i 4500</marc:leader>\
                               <marc:controlfield tag=\"001\">000000001</marc:controlfield>\
                               <marc:controlfield tag=\"008\">210128t20212021enka    sb    000 0 eng d</marc:controlfield>\
                               <marc:datafield tag=\"041\" ind1=\"0\" ind2=\" \">\
                               <marc:subfield code=\"a\">eng</marc:subfield>\
                               </marc:datafield>\
                               <marc:datafield tag=\"100\" ind1=\"1\" ind2=\" \">\
                               <marc:subfield code=\"a\">Author Name</marc:subfield>\
                               </marc:datafield>\
                               <marc:datafield tag=\"245\" ind1=\"0\" ind2=\"0\">\
                               <marc:subfield code=\"a\">Book title</marc:subfield>\
                               <marc:subfield code=\"b\">Book Subtitle</marc:subfield>\
                               </marc:datafield>\
                               <marc:datafield tag=\"264\" ind1=\" \" ind2=\"1\">\
                               <marc:subfield code=\"a\">León, Spain</marc:subfield>\
                               </marc:datafield>\
                               </marc:record>\
                               </marc:collection>".to_string();
        assert_eq!(minified_xml, expected);
    }

    #[test]
    fn should_output_pretty_xml_collection() {
        let minified_xml = test_records()
            .xml_pretty()
            .map(String::from_utf8)
            .unwrap()
            .unwrap();

        let expected = "\
            <?xml version=\"1.0\" encoding=\"utf-8\"?>\n\
            <marc:collection \
                xmlns:marc=\"http://www.loc.gov/MARC21/slim\" \
                xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \
                xsi:schemaLocation=\"http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd\"\
            >\n  \
                <marc:record>\n    \
                    <marc:leader>00220nam  2200097 i 4500</marc:leader>\n    \
                    <marc:controlfield tag=\"001\">000000001</marc:controlfield>\n    \
                    <marc:controlfield tag=\"008\">210128t20212021enka    sb    000 0 eng d</marc:controlfield>\n    \
                    <marc:datafield tag=\"041\" ind1=\"0\" ind2=\" \">\n      \
                        <marc:subfield code=\"a\">eng</marc:subfield>\n    \
                    </marc:datafield>\n    \
                    <marc:datafield tag=\"100\" ind1=\"1\" ind2=\" \">\n      \
                        <marc:subfield code=\"a\">Author Name</marc:subfield>\n    \
                    </marc:datafield>\n    \
                    <marc:datafield tag=\"245\" ind1=\"0\" ind2=\"0\">\n      \
                        <marc:subfield code=\"a\">Book title</marc:subfield>\n      \
                        <marc:subfield code=\"b\">Book Subtitle</marc:subfield>\n    </marc:datafield>\n    \
                    <marc:datafield tag=\"264\" ind1=\" \" ind2=\"1\">\n      \
                        <marc:subfield code=\"a\">León, Spain</marc:subfield>\n    \
                    </marc:datafield>\n  \
                </marc:record>\n\
            </marc:collection>".to_string();

        assert_eq!(minified_xml, expected);
    }

    #[test]
    fn should_write_element() {
        let mut buffer = Vec::new();
        let mut writer = EmitterConfig::new()
            .write_document_declaration(false)
            .create_writer(&mut buffer);

        super::write_element(
            "test_element",
            vec![("attr1", "value1"), ("attr2", "value2")],
            &mut writer,
            |w| {
                // Write some content to the element
                let event = XmlEvent::characters("test content");
                w.write(event).map_err(Into::into)
            },
        )
        .ok();
        let xml_str = String::from_utf8(buffer).unwrap();

        let expected_xml =
            r#"<test_element attr1="value1" attr2="value2">test content</test_element>"#;
        assert_eq!(xml_str, expected_xml);
    }
}