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
//! Core File Properties part
//!
//! The corresponding ZIP item is `/docProps/core.xml`.

use std::borrow::Cow;
use std::io::Write;
use strong_xml::{XmlRead, XmlResult, XmlWrite, XmlWriter};

use crate::schema::{SCHEMA_CORE_2, SCHEMA_DC, SCHEMA_XML};

#[derive(Debug, Default, XmlRead, Clone)]
#[xml(tag = "cp:coreProperties")]
pub struct Core<'a> {
    #[xml(flatten_text = "dc:title")]
    pub title: Option<Cow<'a, str>>,
    #[xml(flatten_text = "dc:subject")]
    pub subject: Option<Cow<'a, str>>,
    #[xml(flatten_text = "dc:creator")]
    pub creator: Option<Cow<'a, str>>,
    #[xml(flatten_text = "cp:keywords")]
    pub keywords: Option<Cow<'a, str>>,
    #[xml(flatten_text = "dc:description")]
    pub description: Option<Cow<'a, str>>,
    #[xml(flatten_text = "cp:lastModifiedBy")]
    pub last_modified_by: Option<Cow<'a, str>>,
    #[xml(flatten_text = "cp:revision")]
    pub revision: Option<Cow<'a, str>>,
}

impl<'a> XmlWrite for Core<'a> {
    fn to_writer<W: Write>(&self, writer: &mut XmlWriter<W>) -> XmlResult<()> {
        let Core {
            title,
            subject,
            creator,
            keywords,
            description,
            last_modified_by,
            revision,
        } = self;

        log::debug!("[Core] Started writing.");
        let _ = write!(writer.inner, "{}", SCHEMA_XML);

        writer.write_element_start("cp:coreProperties")?;

        writer.write_attribute("xmlns:cp", SCHEMA_CORE_2)?;

        writer.write_attribute("xmlns:dc", SCHEMA_DC)?;

        if title.is_none()
            && subject.is_none()
            && creator.is_none()
            && keywords.is_none()
            && description.is_none()
            && last_modified_by.is_none()
            && revision.is_none()
        {
            writer.write_element_end_empty()?;
        } else {
            writer.write_element_end_open()?;
            if let Some(val) = title {
                writer.write_flatten_text("dc:title", val, false)?;
            }
            if let Some(val) = subject {
                writer.write_flatten_text("dc:subject", val, false)?;
            }
            if let Some(val) = creator {
                writer.write_flatten_text("dc:creator", val, false)?;
            }
            if let Some(val) = keywords {
                writer.write_flatten_text("cp:keywords", val, false)?;
            }
            if let Some(val) = description {
                writer.write_flatten_text("dc:description", val, false)?;
            }
            if let Some(val) = last_modified_by {
                writer.write_flatten_text("cp:lastModifiedBy", val, false)?;
            }
            if let Some(val) = revision {
                writer.write_flatten_text("cp:revision", val, false)?;
            }
            writer.write_element_end_close("cp:coreProperties")?;
        }

        log::debug!("[Core] Finished writing.");

        Ok(())
    }
}