f-office-opc 0.3.0

Open Packaging Conventions container: ZIP package and relationships, with input limits.
Documentation
//! Why a package could not be read.

use std::fmt;

/// Why a package could not be read.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Error {
    /// The bytes are not a readable ZIP file.
    Zip(ZipFault),
    /// A limit in [`crate::Limits`] was broken.
    Limit(Limit),
    /// A relationships part is not valid XML.
    Xml(f_office_xml::Error),
    /// The package has no main document relationship.
    NoMainDocument,
}

/// The step of reading the ZIP file that failed. The ZIP reader's own error stays inside the
/// crate, so that no type of another crate reaches the caller.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum ZipFault {
    /// The end of the archive or its central directory does not read.
    Archive,
    /// An entry of the central directory does not read.
    Entry,
    /// The name of an entry does not read.
    Name,
    /// The data of an entry does not read or does not decompress.
    Read,
}

/// A limit of [`crate::Limits`].
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Limit {
    /// The number of entries, [`crate::Limits::max_entries`].
    Entries,
    /// The size of a part, [`crate::Limits::max_part_size`].
    PartSize,
    /// The total size of the parts, [`crate::Limits::max_total_size`].
    TotalSize,
    /// The compression ratio of a large part, [`crate::Limits::max_ratio`].
    Ratio,
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Zip(fault) => write!(f, "unreadable ZIP package: {fault}"),
            Self::Limit(limit) => write!(f, "package breaks the limit on {limit}"),
            Self::Xml(_) => f.write_str("bad relationships part"),
            Self::NoMainDocument => f.write_str("package has no main document"),
        }
    }
}

impl fmt::Display for ZipFault {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(match self {
            Self::Archive => "its central directory does not read",
            Self::Entry => "an entry of its central directory does not read",
            Self::Name => "the name of an entry does not read",
            Self::Read => "the data of an entry does not read",
        })
    }
}

impl fmt::Display for Limit {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(match self {
            Self::Entries => "the number of entries",
            Self::PartSize => "the size of a part",
            Self::TotalSize => "the total size",
            Self::Ratio => "the compression ratio",
        })
    }
}

impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Xml(error) => Some(error),
            Self::Zip(_) | Self::Limit(_) | Self::NoMainDocument => None,
        }
    }
}