papermake 0.2.0

Fast PDF generation library using Typst with a virtual file system for templates, images, and fonts
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
//! Error types for the papermake library
//!
//! This module provides a comprehensive error hierarchy for all papermake operations.
//! Errors are organized by domain to provide clear context and actionable information.

use std::fmt;
use thiserror::Error;
use typst::diag::{FileError, SourceDiagnostic};

/// Main error type for the papermake library
///
/// This is the root error type that encompasses all possible errors that can occur
/// during papermake operations. Each variant represents a different domain of errors.
#[derive(Error, Debug)]
pub enum PapermakeError {
    /// Template-related errors (structure, validation, loading)
    #[error("Template error: {0}")]
    Template(#[from] TemplateError),

    /// Typst compilation errors with rich diagnostics
    #[error("Compilation error: {0}")]
    Compilation(#[from] CompilationError),

    /// File system operations (reading, writing, permissions)
    #[error("File system error: {0}")]
    FileSystem(#[from] FileSystemError),

    /// Data serialization and validation errors
    #[error("Data error: {0}")]
    Data(#[from] DataError),

    /// Configuration and initialization errors
    #[error("Configuration error: {0}")]
    Config(#[from] ConfigError),
}

/// Template-related errors
///
/// These errors occur during template loading, validation, or structure analysis.
#[derive(Error, Debug)]
pub enum TemplateError {
    #[error("Template not found: {path}")]
    NotFound { path: String },

    #[error("Invalid template structure: {message}")]
    InvalidStructure { message: String },

    #[error("Missing required file: {file}")]
    MissingFile { file: String },

    #[error("Invalid template content: {reason}")]
    InvalidContent { reason: String },

    #[error("Template dependency error: {dependency} - {reason}")]
    DependencyError { dependency: String, reason: String },
}

/// Typst compilation errors with rich diagnostics
///
/// These errors occur during the Typst compilation process and include
/// detailed diagnostic information when available.
#[derive(Error, Debug)]
pub enum CompilationError {
    #[error("Typst compilation failed with {error_count} error(s)")]
    TypstError {
        error_count: usize,
        diagnostics: Vec<DiagnosticInfo>,
    },

    #[error("Template compilation failed: {message}")]
    TemplateCompilation { message: String },

    #[error("Data injection failed: {reason}")]
    DataInjection { reason: String },

    #[error("Syntax error in template: {message}")]
    SyntaxError { message: String },

    #[error("Import resolution failed: {import_path} - {reason}")]
    ImportResolution { import_path: String, reason: String },
}

/// File system related errors
///
/// These errors occur during file operations and provide context about
/// what operation failed and why.
#[derive(Error, Debug)]
pub enum FileSystemError {
    #[error("File not found: {path}")]
    NotFound { path: String },

    #[error("Permission denied: {path}")]
    PermissionDenied { path: String },

    #[error("Invalid file path: {path}")]
    InvalidPath { path: String },

    #[error("File read error: {path} - {reason}")]
    ReadError { path: String, reason: String },

    #[error("File write error: {path} - {reason}")]
    WriteError { path: String, reason: String },

    #[error("Invalid UTF-8 content in file: {path}")]
    InvalidUtf8 { path: String },
}

/// Data serialization and validation errors
///
/// These errors occur during JSON serialization/deserialization and
/// schema validation operations.
#[derive(Error, Debug)]
pub enum DataError {
    #[error("JSON serialization failed: {reason}")]
    Serialization { reason: String },

    #[error("JSON deserialization failed: {reason}")]
    Deserialization { reason: String },

    #[error("Schema validation failed: {message}")]
    SchemaValidation { message: String },

    #[error("Invalid data format: {expected}, got {actual}")]
    InvalidFormat { expected: String, actual: String },

    #[error("Missing required field: {field}")]
    MissingField { field: String },

    #[error("Invalid field value: {field} - {reason}")]
    InvalidFieldValue { field: String, reason: String },
}

/// Configuration and initialization errors
///
/// These errors occur during system configuration, font loading,
/// and other initialization tasks.
#[derive(Error, Debug)]
pub enum ConfigError {
    #[error("Font loading failed: {reason}")]
    FontLoading { reason: String },

    #[error("Cache initialization failed: {reason}")]
    CacheInit { reason: String },

    #[error("Invalid configuration: {setting} - {reason}")]
    InvalidConfig { setting: String, reason: String },

    #[error("Environment variable error: {var} - {reason}")]
    Environment { var: String, reason: String },

    #[error("Runtime error: {message}")]
    Runtime { message: String },
}

/// Rich diagnostic information from Typst compilation
///
/// This struct captures detailed information about compilation errors
/// including source location, severity, and helpful hints.
#[derive(Debug, Clone)]
pub struct DiagnosticInfo {
    /// The error message
    pub message: String,
    /// Severity level (error, warning, info)
    pub severity: DiagnosticSeverity,
    /// Source location information
    pub location: Option<SourceLocation>,
    /// Helpful hints for fixing the error
    pub hints: Vec<String>,
}

/// Diagnostic severity levels
#[derive(Debug, Clone, PartialEq)]
pub enum DiagnosticSeverity {
    Error,
    Warning,
    Info,
}

/// Source location information for diagnostics
#[derive(Debug, Clone)]
pub struct SourceLocation {
    /// File path or identifier
    pub file: String,
    /// Line number (1-based)
    pub line: usize,
    /// Column number (1-based)
    pub column: usize,
    /// Character range in the source
    pub range: Option<(usize, usize)>,
}

// Implement Display for DiagnosticInfo to provide user-friendly error messages
impl fmt::Display for DiagnosticInfo {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self.location {
            Some(loc) => write!(f, "{}:{}: {}", loc.file, loc.line, self.message),
            None => write!(f, "{}", self.message),
        }
    }
}

// Implement Display for DiagnosticSeverity
impl fmt::Display for DiagnosticSeverity {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            DiagnosticSeverity::Error => write!(f, "error"),
            DiagnosticSeverity::Warning => write!(f, "warning"),
            DiagnosticSeverity::Info => write!(f, "info"),
        }
    }
}

/// Shorthand result type for papermake operations
pub type Result<T> = std::result::Result<T, PapermakeError>;

/// Utility function to convert Typst diagnostics to our diagnostic format
pub fn convert_typst_diagnostic(diagnostic: SourceDiagnostic) -> DiagnosticInfo {
    DiagnosticInfo {
        message: diagnostic.message.to_string(),
        severity: DiagnosticSeverity::Error, // Typst diagnostics are typically errors
        location: None, // Will be filled in by the caller with file context
        hints: diagnostic.hints.into_iter().map(|h| h.to_string()).collect(),
    }
}

/// Create a template error for missing files
pub fn template_missing_file<S: Into<String>>(file: S) -> PapermakeError {
    PapermakeError::Template(TemplateError::MissingFile { file: file.into() })
}

/// Create a compilation error from Typst diagnostics
pub fn compilation_error_from_diagnostics(diagnostics: Vec<SourceDiagnostic>) -> PapermakeError {
    let diagnostic_infos: Vec<DiagnosticInfo> = diagnostics
        .into_iter()
        .map(convert_typst_diagnostic)
        .collect();
    
    let error_count = diagnostic_infos.len();
    
    PapermakeError::Compilation(CompilationError::TypstError {
        error_count,
        diagnostics: diagnostic_infos,
    })
}

// ============================================================================
// From Implementations for External Error Types
// ============================================================================

/// Convert std::io::Error to PapermakeError
impl From<std::io::Error> for PapermakeError {
    fn from(error: std::io::Error) -> Self {
        let reason = error.to_string();
        match error.kind() {
            std::io::ErrorKind::NotFound => {
                PapermakeError::FileSystem(FileSystemError::NotFound {
                    path: "<unknown>".to_string(),
                })
            }
            std::io::ErrorKind::PermissionDenied => {
                PapermakeError::FileSystem(FileSystemError::PermissionDenied {
                    path: "<unknown>".to_string(),
                })
            }
            _ => PapermakeError::FileSystem(FileSystemError::ReadError {
                path: "<unknown>".to_string(),
                reason,
            }),
        }
    }
}

/// Convert serde_json::Error to PapermakeError
impl From<serde_json::Error> for PapermakeError {
    fn from(error: serde_json::Error) -> Self {
        let reason = error.to_string();
        if error.is_syntax() || error.is_data() {
            PapermakeError::Data(DataError::Deserialization { reason })
        } else {
            PapermakeError::Data(DataError::Serialization { reason })
        }
    }
}

/// Convert typst::diag::FileError to PapermakeError
impl From<FileError> for PapermakeError {
    fn from(error: FileError) -> Self {
        match error {
            FileError::NotFound(path) => {
                PapermakeError::FileSystem(FileSystemError::NotFound {
                    path: path.display().to_string(),
                })
            }
            FileError::AccessDenied => {
                PapermakeError::FileSystem(FileSystemError::PermissionDenied {
                    path: "<unknown>".to_string(),
                })
            }
            FileError::InvalidUtf8 => {
                PapermakeError::FileSystem(FileSystemError::InvalidUtf8 {
                    path: "<unknown>".to_string(),
                })
            }
            FileError::Other(msg) => {
                PapermakeError::FileSystem(FileSystemError::ReadError {
                    path: "<unknown>".to_string(),
                    reason: msg.map(|m| m.to_string()).unwrap_or_else(|| "Unknown error".to_string()),
                })
            }
            FileError::IsDirectory => {
                PapermakeError::FileSystem(FileSystemError::InvalidPath {
                    path: "<directory>".to_string(),
                })
            }
            FileError::NotSource => {
                PapermakeError::FileSystem(FileSystemError::ReadError {
                    path: "<unknown>".to_string(),
                    reason: "File is not a Typst source file".to_string(),
                })
            }
            FileError::Package(pkg_error) => {
                PapermakeError::FileSystem(FileSystemError::ReadError {
                    path: "<package>".to_string(),
                    reason: format!("Package error: {:?}", pkg_error),
                })
            }
        }
    }
}

/// Convert std::string::FromUtf8Error to PapermakeError
impl From<std::string::FromUtf8Error> for PapermakeError {
    fn from(_error: std::string::FromUtf8Error) -> Self {
        PapermakeError::FileSystem(FileSystemError::InvalidUtf8 {
            path: "<unknown>".to_string(),
        })
    }
}

// ============================================================================
// Error Helper Functions
// ============================================================================

impl PapermakeError {
    /// Get a user-friendly error message
    pub fn user_message(&self) -> String {
        match self {
            PapermakeError::Template(e) => match e {
                TemplateError::NotFound { path } => {
                    format!("Template not found: {}", path)
                }
                TemplateError::InvalidStructure { message } => {
                    format!("Invalid template structure: {}", message)
                }
                TemplateError::MissingFile { file } => {
                    format!("Template is missing required file: {}", file)
                }
                _ => format!("Template error: {}", e),
            },
            PapermakeError::Compilation(e) => match e {
                CompilationError::TypstError { error_count, .. } => {
                    format!("Template compilation failed with {} error(s)", error_count)
                }
                _ => format!("Compilation error: {}", e),
            },
            PapermakeError::FileSystem(e) => match e {
                FileSystemError::NotFound { path } => {
                    format!("File not found: {}", path)
                }
                FileSystemError::PermissionDenied { path } => {
                    format!("Permission denied accessing: {}", path)
                }
                _ => format!("File system error: {}", e),
            },
            PapermakeError::Data(e) => match e {
                DataError::Serialization { .. } => {
                    "Failed to serialize data. Please check your data format.".to_string()
                }
                DataError::Deserialization { .. } => {
                    "Failed to parse data. Please check your JSON format.".to_string()
                }
                _ => format!("Data error: {}", e),
            },
            PapermakeError::Config(e) => {
                format!("Configuration error: {}", e)
            }
        }
    }

    /// Check if this error is recoverable
    pub fn is_recoverable(&self) -> bool {
        match self {
            PapermakeError::Template(TemplateError::NotFound { .. }) => false,
            PapermakeError::FileSystem(FileSystemError::NotFound { .. }) => false,
            PapermakeError::FileSystem(FileSystemError::PermissionDenied { .. }) => false,
            PapermakeError::Config(_) => false,
            _ => true,
        }
    }

    /// Get error suggestions for common problems
    pub fn suggestions(&self) -> Vec<String> {
        match self {
            PapermakeError::Template(TemplateError::NotFound { .. }) => {
                vec![
                    "Check if the template path is correct".to_string(),
                    "Verify the template exists in the expected location".to_string(),
                ]
            }
            PapermakeError::Data(DataError::Deserialization { .. }) => {
                vec![
                    "Verify your JSON syntax is valid".to_string(),
                    "Check for missing quotes or trailing commas".to_string(),
                    "Validate your data against the template schema".to_string(),
                ]
            }
            PapermakeError::Compilation(CompilationError::DataInjection { .. }) => {
                vec![
                    "Ensure your data matches the expected structure".to_string(),
                    "Check if required fields are present".to_string(),
                ]
            }
            _ => vec![],
        }
    }
}