docx_rust/
content_type.rs

1//! Content-type item
2//!
3//! The corresponding ZIP item is `/[Content_Types].xml`.
4
5use hard_xml::{XmlRead, XmlResult, XmlWrite, XmlWriter};
6use std::borrow::Cow;
7use std::io::Write;
8
9use crate::schema::SCHEMA_CONTENT_TYPES;
10
11const CONTENT_TYPE_XML: &str = "application/xml";
12const CONTENT_TYPE_CORE: &str = "application/vnd.openxmlformats-package.core-properties+xml";
13const CONTENT_TYPE_RELATIONSHIP: &str = "application/vnd.openxmlformats-package.relationships+xml";
14const CONTENT_TYPE_EXTENDED: &str =
15    "application/vnd.openxmlformats-officedocument.extended-properties+xml";
16const CONTENT_TYPE_DOCUMENT: &str =
17    "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml";
18const CONTENT_TYPE_STYLES: &str =
19    "application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml";
20
21#[derive(Debug, XmlRead, Clone)]
22#[xml(tag = "Types")]
23pub struct ContentTypes<'a> {
24    #[xml(child = "Default")]
25    pub defaults: Vec<DefaultContentType<'a>>,
26    #[xml(child = "Override")]
27    pub overrides: Vec<OverrideContentType<'a>>,
28}
29
30impl Default for ContentTypes<'static> {
31    fn default() -> ContentTypes<'static> {
32        ContentTypes {
33            defaults: vec![
34                DefaultContentType {
35                    ext: "rels".into(),
36                    ty: CONTENT_TYPE_RELATIONSHIP.into(),
37                },
38                DefaultContentType {
39                    ext: "xml".into(),
40                    ty: CONTENT_TYPE_XML.into(),
41                },
42            ],
43            overrides: vec![
44                OverrideContentType {
45                    part: "/docProps/app.xml".into(),
46                    ty: CONTENT_TYPE_EXTENDED.into(),
47                },
48                OverrideContentType {
49                    part: "/docProps/core.xml".into(),
50                    ty: CONTENT_TYPE_CORE.into(),
51                },
52                OverrideContentType {
53                    part: "/word/document.xml".into(),
54                    ty: CONTENT_TYPE_DOCUMENT.into(),
55                },
56                OverrideContentType {
57                    part: "/word/styles.xml".into(),
58                    ty: CONTENT_TYPE_STYLES.into(),
59                },
60            ],
61        }
62    }
63}
64
65impl<'a> XmlWrite for ContentTypes<'a> {
66    fn to_writer<W: Write>(&self, writer: &mut XmlWriter<W>) -> XmlResult<()> {
67        let ContentTypes {
68            defaults,
69            overrides,
70        } = self;
71
72        log::debug!("[ContentTypes] Started writing.");
73        let _ = write!(writer.inner, "{}", crate::schema::SCHEMA_XML);
74
75        writer.write_element_start("Types")?;
76
77        writer.write_attribute("xmlns", SCHEMA_CONTENT_TYPES)?;
78
79        if defaults.is_empty() && overrides.is_empty() {
80            writer.write_element_end_empty()?;
81        } else {
82            writer.write_element_end_open()?;
83            for ele in defaults {
84                ele.to_writer(writer)?;
85            }
86            for ele in overrides {
87                ele.to_writer(writer)?;
88            }
89            writer.write_element_end_close("Types")?;
90        }
91
92        log::debug!("[ContentTypes] Finished writing.");
93
94        Ok(())
95    }
96}
97
98#[derive(Debug, Default, XmlRead, XmlWrite, Clone)]
99#[xml(tag = "Default")]
100pub struct DefaultContentType<'a> {
101    #[xml(attr = "Extension")]
102    pub ext: Cow<'a, str>,
103    #[xml(attr = "ContentType")]
104    pub ty: Cow<'a, str>,
105}
106
107#[derive(Debug, Default, XmlRead, XmlWrite, Clone)]
108#[xml(tag = "Override")]
109pub struct OverrideContentType<'a> {
110    #[xml(attr = "PartName")]
111    pub part: Cow<'a, str>,
112    #[xml(attr = "ContentType")]
113    pub ty: Cow<'a, str>,
114}