Skip to main content

word_ooxml/
error.rs

1//! Error type for `word-ooxml`.
2
3use thiserror::Error;
4
5/// Errors that can occur while reading or writing a `.docx` document.
6#[derive(Debug, Error)]
7pub enum Error {
8    /// An error occurred at the OPC package level (ZIP container, parts,
9    /// relationships).
10    #[error("OPC package error: {0}")]
11    Opc(#[from] opc::Error),
12
13    /// An error occurred while reading or writing XML.
14    #[error("XML error: {0}")]
15    Xml(#[from] xml_core::Error),
16
17    /// The package has no relationship of type
18    /// `.../relationships/officeDocument`, so the main document part could
19    /// not be located.
20    #[error("the package has no main document part (missing officeDocument relationship)")]
21    MissingMainDocument,
22
23    /// The main document part is not valid UTF-8.
24    #[error("document.xml is not valid UTF-8: {0}")]
25    InvalidUtf8(#[from] std::str::Utf8Error),
26
27    /// A `<w:drawing>` referenced a relationship id (`r:embed`) that has
28    /// no matching entry in `word/_rels/document.xml.rels`.
29    #[error("image relationship '{0}' is not declared in word/_rels/document.xml.rels")]
30    MissingImageRelationship(String),
31
32    /// A `<w:drawing>`'s relationship resolved to a part name that isn't
33    /// actually present in the package.
34    #[error("image part '{0}' is missing from the package")]
35    MissingImagePart(String),
36
37    /// An image part's extension isn't one of the formats we support
38    /// reading back (see [`crate::ImageFormat`]).
39    #[error("unsupported image format for part '{0}'")]
40    UnsupportedImageFormat(String),
41
42    /// A `<w:headerReference>`/`<w:footerReference>` referenced a
43    /// relationship id (`r:id`) that has no matching entry in
44    /// `word/_rels/document.xml.rels`.
45    #[error("header/footer relationship '{0}' is not declared in word/_rels/document.xml.rels")]
46    MissingHeaderFooterRelationship(String),
47
48    /// A `<w:headerReference>`/`<w:footerReference>`'s relationship
49    /// resolved to a part name that isn't actually present in the package.
50    #[error("header/footer part '{0}' is missing from the package")]
51    MissingHeaderFooterPart(String),
52
53    /// `word/document.xml` declares a relationship to `word/styles.xml`,
54    /// but that part isn't actually present in the package.
55    #[error("styles part '{0}' is missing from the package")]
56    MissingStylesPart(String),
57
58    /// `word/document.xml` declares a relationship to `word/numbering.xml`,
59    /// but that part isn't actually present in the package.
60    #[error("numbering part '{0}' is missing from the package")]
61    MissingNumberingPart(String),
62
63    /// `word/document.xml` declares a relationship to `word/footnotes.xml`,
64    /// but that part isn't actually present in the package.
65    #[error("footnotes part '{0}' is missing from the package")]
66    MissingFootnotesPart(String),
67
68    /// `word/document.xml` declares a relationship to `word/endnotes.xml`,
69    /// but that part isn't actually present in the package.
70    #[error("endnotes part '{0}' is missing from the package")]
71    MissingEndnotesPart(String),
72
73    /// `word/document.xml` declares a relationship to `word/comments.xml`,
74    /// but that part isn't actually present in the package.
75    #[error("comments part '{0}' is missing from the package")]
76    MissingCommentsPart(String),
77
78    /// `word/document.xml` declares a relationship to
79    /// `word/theme/theme1.xml`, but that part isn't actually present in the
80    /// package.
81    #[error("theme part '{0}' is missing from the package")]
82    MissingThemePart(String),
83
84    /// An error occurred while reading or writing a shape's DrawingML
85    /// content (fill/line/text/image reference) inside an inline drawing.
86    #[error("drawing error: {0}")]
87    Drawing(#[from] drawing::Error),
88
89    /// An error occurred while reading or writing an embedded chart's
90    /// content.
91    #[error("chart error: {0}")]
92    Chart(#[from] chart::Error),
93
94    /// A `<w:drawing>` referenced a chart relationship id (`r:id` on
95    /// `<c:chart>`) that has no matching entry in the owning part's
96    /// `.rels`.
97    #[error("chart relationship '{0}' is not declared in the owning part's .rels")]
98    MissingChartRelationship(String),
99
100    /// A `<w:drawing>`'s chart relationship resolved to a part name that
101    /// isn't actually present in the package.
102    #[error("chart part '{0}' is missing from the package")]
103    MissingChartPart(String),
104}
105
106/// A [`Result`](std::result::Result) alias using [`Error`] as the error type.
107pub type Result<T> = std::result::Result<T, Error>;