office_toolkit/error.rs
1//! Error type for `office-toolkit`'s own file-level convenience layer
2//! ([`crate::open`], [`crate::OpenFile`], [`crate::SaveToFile`]).
3//!
4//! The `word`/`excel`/`powerpoint` modules' own re-exported types keep
5//! using their own crate's error type directly (`word_ooxml::Error`, and
6//! so on) for their own `read_from`/`write_to` — this type only comes into
7//! play where this crate's own code needs to return a single error type
8//! spanning more than one format, which format-specific auto-detection
9//! (`open`) inherently does: it can fail for reasons no single format's
10//! own error type can express at all, like "this isn't a recognized
11//! Office file in the first place".
12
13use thiserror::Error;
14
15/// Errors from `office-toolkit`'s own file-level convenience layer.
16#[derive(Debug, Error)]
17pub enum Error {
18 /// An I/O error occurred opening/reading/writing the file itself.
19 #[error("I/O error: {0}")]
20 Io(#[from] std::io::Error),
21
22 /// An error occurred at the OPC package level while inspecting a file
23 /// to detect its format ([`crate::open`]) — before it's even clear
24 /// which format-specific reader should take over.
25 #[error("OPC package error: {0}")]
26 Opc(#[from] opc::Error),
27
28 /// The file is a well-formed OPC/ZIP package, but carries none of the
29 /// three main parts (`word/document.xml`/`xl/workbook.xml`/
30 /// `ppt/presentation.xml`) [`crate::open`] knows how to recognize —
31 /// either a format outside this crate's scope, or a corrupt/foreign
32 /// package. Detection never looks at the file's name/extension, only
33 /// at the package's own parts, so this is never a false negative
34 /// caused by a missing/wrong extension.
35 #[error(
36 "unrecognized Office format: no word/document.xml, xl/workbook.xml or ppt/presentation.xml part found"
37 )]
38 UnrecognizedFormat,
39
40 /// [`crate::open`] recognized the file's format from its parts, but
41 /// the matching Cargo feature (`word`/`excel`/`powerpoint`) isn't
42 /// enabled in this build, so no reader is available for it. Distinct
43 /// from [`Error::UnrecognizedFormat`] — the file is a perfectly valid
44 /// Office document, this build just wasn't compiled to handle it.
45 #[error(
46 "recognized this file as a '{0}' document, but the '{0}' feature isn't enabled in this build"
47 )]
48 FormatNotEnabled(&'static str),
49
50 /// A `.docx` was recognized (or explicitly requested via
51 /// [`crate::OpenFile`]/[`crate::SaveToFile`]), but `word-ooxml` itself
52 /// failed to read or write it.
53 #[cfg(feature = "word")]
54 #[error("Word error: {0}")]
55 Word(#[from] word_ooxml::Error),
56
57 /// A `.xlsx` was recognized (or explicitly requested via
58 /// [`crate::OpenFile`]/[`crate::SaveToFile`]), but `excel-ooxml` itself
59 /// failed to read or write it.
60 #[cfg(feature = "excel")]
61 #[error("Excel error: {0}")]
62 Excel(#[from] excel_ooxml::Error),
63
64 /// A `.pptx` was recognized (or explicitly requested via
65 /// [`crate::OpenFile`]/[`crate::SaveToFile`]), but `powerpoint-ooxml`
66 /// itself failed to read or write it.
67 #[cfg(feature = "powerpoint")]
68 #[error("PowerPoint error: {0}")]
69 Powerpoint(#[from] powerpoint_ooxml::Error),
70}
71
72/// A [`Result`](std::result::Result) alias using [`Error`] as the error type.
73pub type Result<T> = std::result::Result<T, Error>;