1use thiserror::Error;
4
5#[derive(Debug, Error)]
7pub enum Error {
8 #[error("Form error: {0}")]
10 Form(#[from] FormError),
11 #[error("Signing error: {0}")]
13 Signing(#[from] SigningError),
14 #[error("Encryption error: {0}")]
16 Encryption(#[from] EncryptionError),
17 #[error("Watermark error: {0}")]
19 Watermark(#[from] WatermarkError),
20 #[error("Bookmark error: {0}")]
22 Bookmark(#[from] BookmarkError),
23 #[error("Annotation error: {0}")]
25 Annotation(#[from] AnnotationError),
26 #[error("PDF/A error: {0}")]
28 PdfA(#[from] PdfAError),
29 #[error("PDF/UA error: {0}")]
31 PdfUA(#[from] PdfUAError),
32 #[error("Invalid PDF: {0}")]
34 InvalidPdf(String),
35 #[error("Invalid parameter: {0}")]
37 InvalidParameter(String),
38 #[error("PDF operation error: {0}")]
40 PdfOperation(String),
41 #[error("IO error: {0}")]
43 Io(#[from] std::io::Error),
44 #[error("FFI error: {0}")]
46 Ffi(#[from] cxx::Exception),
47}
48
49#[derive(Debug, Error)]
51pub enum FormError {
52 #[error(
54 "PDF form manipulation requires a commercial license. Purchase at: https://printwell.dev/pricing"
55 )]
56 RequiresLicense,
57 #[error("failed to initialize form builder: {0}")]
59 InitFailed(String),
60 #[error("failed to add {field_type} field '{name}': {reason}")]
62 AddFieldFailed {
63 field_type: &'static str,
65 name: String,
67 reason: String,
69 },
70 #[error("failed to build form: {0}")]
72 BuildFailed(String),
73 #[error("validation failed for field '{field}': {reason}")]
75 ValidationFailed {
76 field: String,
78 reason: String,
80 },
81 #[error("invalid field specification: {0}")]
83 InvalidSpec(String),
84 #[error("{0}")]
86 Operation(String),
87}
88
89#[derive(Debug, Error)]
91pub enum SigningError {
92 #[error(
94 "Digital signing requires a commercial license. Purchase at: https://printwell.dev/pricing"
95 )]
96 RequiresLicense,
97 #[error("failed to load certificate: {0}")]
99 CertificateLoadFailed(String),
100 #[error("invalid certificate format: {0}")]
102 InvalidCertificate(String),
103 #[error("failed to prepare PDF for signing: {0}")]
105 PrepareFailed(String),
106 #[error("failed to create signature: {0}")]
108 SignatureFailed(String),
109 #[error("failed to embed signature in PDF: {0}")]
111 EmbedFailed(String),
112 #[error("no signer information found in signature")]
114 NoSignerInfo,
115 #[error("signature verification failed: {0}")]
117 VerificationFailed(String),
118 #[error("timestamp operation failed: {0}")]
120 TimestampFailed(String),
121 #[error("CMS operation failed: {0}")]
123 CmsFailed(String),
124 #[error("failed to extract signatures: {0}")]
126 ExtractionFailed(String),
127 #[error("{0}")]
129 Operation(String),
130}
131
132#[derive(Debug, Error)]
134pub enum EncryptionError {
135 #[error(
137 "PDF encryption requires a commercial license. Purchase at: https://printwell.dev/pricing"
138 )]
139 RequiresLicense,
140 #[error("invalid password")]
142 InvalidPassword,
143 #[error("password too short (minimum {min} characters)")]
145 PasswordTooShort {
146 min: usize,
148 },
149 #[error("unsupported encryption algorithm: {0}")]
151 UnsupportedAlgorithm(String),
152 #[error("failed to encrypt PDF: {0}")]
154 EncryptFailed(String),
155 #[error("failed to decrypt PDF: {0}")]
157 DecryptFailed(String),
158 #[error("PDF is already encrypted")]
160 AlreadyEncrypted,
161 #[error("PDF is not encrypted")]
163 NotEncrypted,
164 #[error("{0}")]
166 Operation(String),
167}
168
169#[derive(Debug, Error)]
171pub enum WatermarkError {
172 #[error("either text or image must be specified for watermark")]
174 NoContent,
175 #[error("cannot specify both text and image for watermark")]
177 BothContentTypes,
178 #[error("invalid image data: {0}")]
180 InvalidImage(String),
181 #[error("invalid opacity value {value}: must be between 0.0 and 1.0")]
183 InvalidOpacity {
184 value: f32,
186 },
187 #[error("invalid position: {0}")]
189 InvalidPosition(String),
190 #[error("failed to add watermark: {0}")]
192 AddFailed(String),
193 #[error("{0}")]
195 Operation(String),
196}
197
198#[derive(Debug, Error)]
200pub enum BookmarkError {
201 #[error("invalid bookmark specification: {0}")]
203 InvalidSpec(String),
204 #[error("invalid page number {page}: document has {total} pages")]
206 InvalidPage {
207 page: u32,
209 total: u32,
211 },
212 #[error("failed to add bookmarks: {0}")]
214 AddFailed(String),
215 #[error("failed to extract bookmarks: {0}")]
217 ExtractFailed(String),
218 #[error("{0}")]
220 Operation(String),
221}
222
223#[derive(Debug, Error)]
225pub enum AnnotationError {
226 #[error("invalid annotation type: {0}")]
228 InvalidType(String),
229 #[error("invalid rectangle bounds: {reason}")]
231 InvalidRect {
232 reason: String,
234 },
235 #[error("invalid color: {0}")]
237 InvalidColor(String),
238 #[error("failed to add annotations: {0}")]
240 AddFailed(String),
241 #[error("failed to list annotations: {0}")]
243 ListFailed(String),
244 #[error("failed to remove annotations: {0}")]
246 RemoveFailed(String),
247 #[error("{0}")]
249 Operation(String),
250}
251
252#[derive(Debug, Error)]
254pub enum PdfAError {
255 #[error(
257 "PDF/A compliance features require a commercial license. Purchase at: https://printwell.dev/pricing"
258 )]
259 RequiresLicense,
260 #[error("invalid PDF/A level: {0}")]
262 InvalidLevel(String),
263 #[error("PDF/A validation failed: {error_count} errors, {warning_count} warnings")]
265 ValidationFailed {
266 error_count: usize,
268 warning_count: usize,
270 },
271 #[error("failed to add PDF/A metadata: {0}")]
273 MetadataFailed(String),
274 #[error("document has {count} compliance issues")]
276 ComplianceIssues {
277 count: usize,
279 },
280 #[error("{0}")]
282 Operation(String),
283}
284
285#[derive(Debug, Error)]
287pub enum PdfUAError {
288 #[error(
290 "PDF/UA accessibility features require a commercial license. Purchase at: https://printwell.dev/pricing"
291 )]
292 RequiresLicense,
293 #[error("invalid PDF/UA level: {0}")]
295 InvalidLevel(String),
296 #[error("document missing required structure: {0}")]
298 MissingStructure(String),
299 #[error("accessibility issue: {category} - {description}")]
301 AccessibilityIssue {
302 category: String,
304 description: String,
306 },
307 #[error("failed to add PDF/UA metadata: {0}")]
309 MetadataFailed(String),
310 #[error("{0}")]
312 Operation(String),
313}
314
315impl From<String> for FormError {
317 fn from(s: String) -> Self {
318 Self::Operation(s)
319 }
320}
321
322impl From<String> for SigningError {
323 fn from(s: String) -> Self {
324 Self::Operation(s)
325 }
326}
327
328impl From<String> for EncryptionError {
329 fn from(s: String) -> Self {
330 Self::Operation(s)
331 }
332}
333
334impl From<String> for WatermarkError {
335 fn from(s: String) -> Self {
336 Self::Operation(s)
337 }
338}
339
340impl From<String> for BookmarkError {
341 fn from(s: String) -> Self {
342 Self::Operation(s)
343 }
344}
345
346impl From<String> for AnnotationError {
347 fn from(s: String) -> Self {
348 Self::Operation(s)
349 }
350}
351
352impl From<String> for PdfAError {
353 fn from(s: String) -> Self {
354 Self::Operation(s)
355 }
356}
357
358impl From<String> for PdfUAError {
359 fn from(s: String) -> Self {
360 Self::Operation(s)
361 }
362}
363
364pub type Result<T> = std::result::Result<T, Error>;