1use std::fmt;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7#[non_exhaustive]
8pub enum Location {
9 File(usize),
11 ObjectStream {
13 stream: u32,
15 offset: usize,
17 },
18 Object(u32),
20}
21
22impl fmt::Display for Location {
23 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24 match self {
25 Self::File(offset) => write!(f, "at byte {offset}"),
26 Self::ObjectStream { stream, offset } => {
27 write!(f, "at byte {offset} of object stream {stream}")
28 }
29 Self::Object(number) => write!(f, "in object {number}"),
30 }
31 }
32}
33
34#[derive(Debug, Clone, PartialEq, Eq)]
39#[non_exhaustive]
40pub enum Error {
41 NotPdf,
43 Encrypted,
45 Malformed {
47 location: Option<Location>,
49 message: String,
51 },
52 Unsupported {
54 location: Option<Location>,
56 feature: String,
58 },
59 LimitExceeded {
61 limit: &'static str,
63 value: usize,
65 },
66 NoInvoice {
68 attachments: Vec<String>,
70 },
71 Ambiguous {
73 names: Vec<String>,
75 },
76 Metadata(String),
79}
80
81impl Error {
82 pub(crate) fn malformed(location: Option<Location>, message: impl Into<String>) -> Self {
83 Self::Malformed {
84 location,
85 message: message.into(),
86 }
87 }
88
89 pub(crate) fn unsupported(location: Option<Location>, feature: impl Into<String>) -> Self {
90 Self::Unsupported {
91 location,
92 feature: feature.into(),
93 }
94 }
95
96 pub(crate) fn at(self, at: Location) -> Self {
98 match self {
99 Self::Malformed {
100 location: None,
101 message,
102 } => Self::Malformed {
103 location: Some(at),
104 message,
105 },
106 Self::Unsupported {
107 location: None,
108 feature,
109 } => Self::Unsupported {
110 location: Some(at),
111 feature,
112 },
113 other => other,
114 }
115 }
116}
117
118fn suffix(location: Option<&Location>) -> String {
119 location.map_or_else(String::new, |l| format!(" ({l})"))
120}
121
122impl fmt::Display for Error {
123 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
124 match self {
125 Self::NotPdf => f.write_str("not a PDF file: no %PDF-1.x or %PDF-2.0 header at byte 0"),
126 Self::Encrypted => f.write_str("encrypted PDF (PDF/A forbids encryption)"),
127 Self::Malformed { location, message } => {
128 write!(f, "malformed PDF: {message}{}", suffix(location.as_ref()))
129 }
130 Self::Unsupported { location, feature } => {
131 write!(
132 f,
133 "unsupported PDF feature: {feature}{}",
134 suffix(location.as_ref())
135 )
136 }
137 Self::LimitExceeded { limit, value } => {
138 write!(f, "limit exceeded: {limit} ({value})")
139 }
140 Self::NoInvoice { attachments } => {
141 if attachments.is_empty() {
142 f.write_str("no embedded invoice: the PDF has no embedded file")
143 } else {
144 write!(
145 f,
146 "no embedded invoice: no embedded file has an invoice name; embedded files: {}",
147 attachments.join(", ")
148 )
149 }
150 }
151 Self::Ambiguous { names } => write!(
152 f,
153 "ambiguous embedded invoice: several embedded files have an invoice name: {}",
154 names.join(", ")
155 ),
156 Self::Metadata(message) => write!(f, "XMP metadata: {message}"),
157 }
158 }
159}
160
161impl std::error::Error for Error {}
162
163pub(crate) type Result<T> = std::result::Result<T, Error>;