office-toolkit 1.0.0

A Rust library to create, read, and modify Microsoft Office documents (Word/Excel/PowerPoint) in the modern Open XML (OOXML) format.
Documentation
//! Error type for `office-toolkit`'s own file-level convenience layer
//! ([`crate::open`], [`crate::OpenFile`], [`crate::SaveToFile`]).
//!
//! The `word`/`excel`/`powerpoint` modules' own re-exported types keep
//! using their own crate's error type directly (`word_ooxml::Error`, and
//! so on) for their own `read_from`/`write_to` — this type only comes into
//! play where this crate's own code needs to return a single error type
//! spanning more than one format, which format-specific auto-detection
//! (`open`) inherently does: it can fail for reasons no single format's
//! own error type can express at all, like "this isn't a recognized
//! Office file in the first place".

use thiserror::Error;

/// Errors from `office-toolkit`'s own file-level convenience layer.
#[derive(Debug, Error)]
pub enum Error {
    /// An I/O error occurred opening/reading/writing the file itself.
    #[error("I/O error: {0}")]
    Io(#[from] std::io::Error),

    /// An error occurred at the OPC package level while inspecting a file
    /// to detect its format ([`crate::open`]) — before it's even clear
    /// which format-specific reader should take over.
    #[error("OPC package error: {0}")]
    Opc(#[from] opc::Error),

    /// The file is a well-formed OPC/ZIP package, but carries none of the
    /// three main parts (`word/document.xml`/`xl/workbook.xml`/
    /// `ppt/presentation.xml`) [`crate::open`] knows how to recognize —
    /// either a format outside this crate's scope, or a corrupt/foreign
    /// package. Detection never looks at the file's name/extension, only
    /// at the package's own parts, so this is never a false negative
    /// caused by a missing/wrong extension.
    #[error(
        "unrecognized Office format: no word/document.xml, xl/workbook.xml or ppt/presentation.xml part found"
    )]
    UnrecognizedFormat,

    /// [`crate::open`] recognized the file's format from its parts, but
    /// the matching Cargo feature (`word`/`excel`/`powerpoint`) isn't
    /// enabled in this build, so no reader is available for it. Distinct
    /// from [`Error::UnrecognizedFormat`] — the file is a perfectly valid
    /// Office document, this build just wasn't compiled to handle it.
    #[error(
        "recognized this file as a '{0}' document, but the '{0}' feature isn't enabled in this build"
    )]
    FormatNotEnabled(&'static str),

    /// A `.docx` was recognized (or explicitly requested via
    /// [`crate::OpenFile`]/[`crate::SaveToFile`]), but `word-ooxml` itself
    /// failed to read or write it.
    #[cfg(feature = "word")]
    #[error("Word error: {0}")]
    Word(#[from] word_ooxml::Error),

    /// A `.xlsx` was recognized (or explicitly requested via
    /// [`crate::OpenFile`]/[`crate::SaveToFile`]), but `excel-ooxml` itself
    /// failed to read or write it.
    #[cfg(feature = "excel")]
    #[error("Excel error: {0}")]
    Excel(#[from] excel_ooxml::Error),

    /// A `.pptx` was recognized (or explicitly requested via
    /// [`crate::OpenFile`]/[`crate::SaveToFile`]), but `powerpoint-ooxml`
    /// itself failed to read or write it.
    #[cfg(feature = "powerpoint")]
    #[error("PowerPoint error: {0}")]
    Powerpoint(#[from] powerpoint_ooxml::Error),
}

/// A [`Result`](std::result::Result) alias using [`Error`] as the error type.
pub type Result<T> = std::result::Result<T, Error>;