Skip to main content

pptxboss_core/
error.rs

1//! The crate's error type.
2
3use std::fmt;
4
5/// Result alias used throughout the crate.
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// Everything that can go wrong while reading a package.
9///
10/// Reading is lenient, so most defects never become errors: they are
11/// recorded in a report next to the result. An `Error` means the caller
12/// received nothing.
13#[derive(Debug, thiserror::Error)]
14#[non_exhaustive]
15pub enum Error {
16    #[error("io: {0}")]
17    Io(#[from] std::io::Error),
18    /// No end of central directory record: the bytes are not a ZIP archive.
19    #[error("not a zip archive")]
20    NotZip,
21    /// An OLE compound file where an ECMA-376 package was required.
22    #[error("compound file: a legacy .ppt or an encrypted package, not an ECMA-376 package")]
23    CompoundFile,
24    /// An OLE compound file with no presentation inside.
25    #[error("compound file holds no PowerPoint Document stream")]
26    NoPresentationStream,
27    /// A password-protected document; the reader does not decrypt it.
28    #[error("encrypted: {0}")]
29    Encrypted(String),
30    /// A structurally broken ZIP record at the given byte offset.
31    #[error("zip: {msg} at offset {offset}")]
32    Zip { offset: u64, msg: String },
33    /// A ZIP feature the reader does not implement.
34    #[error("unsupported: {0}")]
35    Unsupported(String),
36    /// The compressed data of an entry could not be inflated.
37    #[error("inflate: {0}")]
38    Inflate(String),
39    /// Malformed XML in a part.
40    #[error("xml in {part} at byte {offset}: {msg}")]
41    Xml {
42        part: String,
43        offset: usize,
44        msg: String,
45    },
46    /// A part the document model requires is absent.
47    #[error("missing part: {0}")]
48    MissingPart(String),
49    /// A relationship id referenced from a part has no entry in its rels.
50    #[error("missing relationship {id} in {part}")]
51    MissingRelationship { part: String, id: String },
52    /// The package has no presentation part reachable from the package rels.
53    #[error("not a presentation package")]
54    NotAPresentation,
55    #[error("slide {0} not found")]
56    SlideNotFound(usize),
57    #[error("{0}")]
58    Other(String),
59}
60
61impl Error {
62    pub(crate) fn zip(offset: u64, msg: impl fmt::Display) -> Self {
63        Error::Zip {
64            offset,
65            msg: msg.to_string(),
66        }
67    }
68}