Skip to main content

odox_core/doc/
mod.rs

1//! The three kinds of document, over one shared reader.
2//!
3//! Everything a package carries that is not the body — its styles, its
4//! metadata, its pictures, its manifest — is read the same way for all three
5//! formats, and [`Document`] is that. Each format then adds one thing: the way
6//! its body is addressed. A text document is a flow, a spreadsheet is a set of
7//! sheets indexed by row and column, a presentation is a sequence of pages.
8//
9// Author: David M. Anderson
10// Built with AI assistance (Claude, Anthropic)
11
12mod pres;
13mod sheet;
14mod text;
15
16pub use pres::{Presentation, Slide};
17pub use sheet::{Cell, Column, Sheet, SheetDocument, Value};
18pub use text::TextDocument;
19
20use crate::xml::{Element, Name, Ns};
21use crate::{Error, Meta, Package, Styles};
22
23/// A document read from a package: everything the three formats share.
24pub struct Document {
25    /// The package itself, which keeps every entry whether or not it was read
26    /// here, so that writing gives back a document and not a subset of one.
27    pub package: Package,
28    /// The parsed `content.xml`.
29    pub content: Element,
30    /// The parsed `styles.xml`, if the package has one.
31    pub styles_part: Option<Element>,
32    /// Every style in the document, from both parts.
33    pub styles: Styles,
34    /// What the document says about itself.
35    pub meta: Meta,
36}
37
38impl Document {
39    /// Read a package and check its media type.
40    ///
41    /// # Errors
42    ///
43    /// The bytes are not a package, the package declares a different media type,
44    /// or `content.xml` is absent or not well formed.
45    pub fn read(bytes: &[u8], wanted: &'static [&'static str]) -> Result<Self, Error> {
46        let package = Package::read(bytes)?;
47        package.expect_media_type(wanted)?;
48        let content = package.xml("content.xml")?;
49        let styles_part = package.optional_xml("styles.xml")?;
50        let styles = Styles::collect(Some(&content), styles_part.as_ref());
51        let meta = match package.optional_xml("meta.xml")? {
52            Some(part) => Meta::read(&part),
53            None => Meta::default(),
54        };
55        Ok(Self {
56            package,
57            content,
58            styles_part,
59            styles,
60            meta,
61        })
62    }
63
64    /// The `office:body` element, which every format has exactly one of.
65    pub fn body(&self) -> Option<&Element> {
66        self.content.child(&Ns::Office, "body")
67    }
68
69    /// The part of the body this format keeps its content in: `office:text`,
70    /// `office:spreadsheet` or `office:presentation`.
71    pub fn body_of(&self, local: &str) -> Option<&Element> {
72        self.body()?.child(&Ns::Office, local)
73    }
74
75    /// Where [`Self::body_of`] is under the content root, as a path of child
76    /// indices: what an editor addresses the body's paragraphs from.
77    pub fn body_path(&self, local: &str) -> Option<Vec<usize>> {
78        let (at_body, body) = self
79            .content
80            .elements_indexed()
81            .find(|(_, e)| e.is(&Ns::Office, "body"))?;
82        let (at_part, _) = body
83            .elements_indexed()
84            .find(|(_, e)| e.is(&Ns::Office, local))?;
85        Some(vec![at_body, at_part])
86    }
87
88    /// A name for writing into the document, in the prefix the document
89    /// declares for the namespace on its content root, or the conventional one
90    /// where it declares none.
91    pub fn name(&self, ns: &Ns, local: &str) -> Name {
92        self.content.name_for(ns, local)
93    }
94
95    /// Whether the content root declares a namespace, which is what decides
96    /// whether an attribute in it may be written at all.
97    pub fn declares(&self, ns: &Ns) -> bool {
98        self.content.declares(ns)
99    }
100
101    /// The version of the format the document declares, as `office:version`.
102    pub fn version(&self) -> Option<&str> {
103        self.content.attr(&Ns::Office, "version")
104    }
105
106    /// The bytes of a picture, by the `xlink:href` that referred to it.
107    ///
108    /// A href that names an entry of the package gives those bytes. A href that
109    /// is a URL to somewhere else gives `None`: reaching it would be a network
110    /// request, which nothing in this suite makes.
111    pub fn picture(&self, href: &str) -> Option<&[u8]> {
112        // A href inside a package is a relative path, sometimes written with a
113        // leading `./`.
114        let path = href.strip_prefix("./").unwrap_or(href);
115        if path.contains("://") {
116            return None;
117        }
118        self.package.part(path).map(|p| p.data.as_slice())
119    }
120
121    /// Write the document back out, with the parsed parts re-serialized over the
122    /// ones they were read from.
123    ///
124    /// # Errors
125    ///
126    /// The package could not be assembled.
127    pub fn write(&mut self) -> Result<Vec<u8>, Error> {
128        let content = crate::xml::serialize(&self.content);
129        self.package.set_part("content.xml", content);
130        if let Some(styles) = &self.styles_part {
131            let bytes = crate::xml::serialize(styles);
132            self.package.set_part("styles.xml", bytes);
133        }
134        self.package.write()
135    }
136
137    /// Write the document and prove the bytes read back as the tree they were
138    /// written from, which is what a save has to know before it touches the
139    /// file.
140    ///
141    /// The round-trip tests make the same claim over the corpus, and a person's
142    /// document is not in the corpus. One re-parse per save is the price, and
143    /// the alternative is an editor that finds out it damaged a document after
144    /// it has.
145    ///
146    /// # Errors
147    ///
148    /// The package could not be assembled, or a part came back different, in
149    /// which case nothing should be written.
150    pub fn write_verified(&mut self) -> Result<Vec<u8>, Error> {
151        let bytes = self.write()?;
152        let written = Package::read(&bytes)?;
153        if written.xml("content.xml")? != self.content {
154            return Err(Error::Unfaithful("content.xml"));
155        }
156        if let Some(styles) = &self.styles_part
157            && written.optional_xml("styles.xml")?.as_ref() != Some(styles)
158        {
159            return Err(Error::Unfaithful("styles.xml"));
160        }
161        Ok(bytes)
162    }
163}