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
//! Content-type item
//!
//! The corresponding ZIP item is `/[Content_Types].xml`.

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

use crate::schema::SCHEMA_CONTENT_TYPES;

const CONTENT_TYPE_XML: &str = "application/xml";
const CONTENT_TYPE_CORE: &str = "application/vnd.openxmlformats-package.core-properties+xml";
const CONTENT_TYPE_RELATIONSHIP: &str = "application/vnd.openxmlformats-package.relationships+xml";
const CONTENT_TYPE_EXTENDED: &str =
    "application/vnd.openxmlformats-officedocument.extended-properties+xml";
const CONTENT_TYPE_DOCUMENT: &str =
    "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml";
const CONTENT_TYPE_STYLES: &str =
    "application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml";

#[derive(Debug, XmlRead, Clone)]
#[xml(tag = "Types")]
pub struct ContentTypes<'a> {
    #[xml(child = "Default")]
    pub defaults: Vec<DefaultContentType<'a>>,
    #[xml(child = "Override")]
    pub overrides: Vec<OverrideContentType<'a>>,
}

impl Default for ContentTypes<'static> {
    fn default() -> ContentTypes<'static> {
        ContentTypes {
            defaults: vec![
                DefaultContentType {
                    ext: "rels".into(),
                    ty: CONTENT_TYPE_RELATIONSHIP.into(),
                },
                DefaultContentType {
                    ext: "xml".into(),
                    ty: CONTENT_TYPE_XML.into(),
                },
            ],
            overrides: vec![
                OverrideContentType {
                    part: "/docProps/app.xml".into(),
                    ty: CONTENT_TYPE_EXTENDED.into(),
                },
                OverrideContentType {
                    part: "/docProps/core.xml".into(),
                    ty: CONTENT_TYPE_CORE.into(),
                },
                OverrideContentType {
                    part: "/word/document.xml".into(),
                    ty: CONTENT_TYPE_DOCUMENT.into(),
                },
                OverrideContentType {
                    part: "/word/styles.xml".into(),
                    ty: CONTENT_TYPE_STYLES.into(),
                },
            ],
        }
    }
}

impl<'a> XmlWrite for ContentTypes<'a> {
    fn to_writer<W: Write>(&self, writer: &mut XmlWriter<W>) -> XmlResult<()> {
        let ContentTypes {
            defaults,
            overrides,
        } = self;

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

        writer.write_element_start("Types")?;

        writer.write_attribute("xmlns", SCHEMA_CONTENT_TYPES)?;

        if defaults.is_empty() && overrides.is_empty() {
            writer.write_element_end_empty()?;
        } else {
            writer.write_element_end_open()?;
            for ele in defaults {
                ele.to_writer(writer)?;
            }
            for ele in overrides {
                ele.to_writer(writer)?;
            }
            writer.write_element_end_close("Types")?;
        }

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

        Ok(())
    }
}

#[derive(Debug, Default, XmlRead, XmlWrite, Clone)]
#[xml(tag = "Default")]
pub struct DefaultContentType<'a> {
    #[xml(attr = "Extension")]
    pub ext: Cow<'a, str>,
    #[xml(attr = "ContentType")]
    pub ty: Cow<'a, str>,
}

#[derive(Debug, Default, XmlRead, XmlWrite, Clone)]
#[xml(tag = "Override")]
pub struct OverrideContentType<'a> {
    #[xml(attr = "PartName")]
    pub part: Cow<'a, str>,
    #[xml(attr = "ContentType")]
    pub ty: Cow<'a, str>,
}