Skip to main content

typf_core/
error.rs

1//! When things go wrong in the pipeline
2
3use thiserror::Error;
4
5pub type Result<T, E = TypfError> = std::result::Result<T, E>;
6
7/// Every failure in Typf has a story to tell
8#[derive(Debug, Error)]
9pub enum TypfError {
10    #[error("Feature not implemented: {0}")]
11    NotImplemented(String),
12
13    #[error("Feature not compiled: {0}")]
14    FeatureNotCompiled(String),
15
16    #[error("Invalid backend combination: shaping={0}, render={1}")]
17    UnsupportedBackendCombination(String, String),
18
19    #[error("Font loading failed: {0}")]
20    FontLoad(#[from] FontLoadError),
21
22    #[error("Shaping failed: {0}")]
23    ShapingFailed(#[from] ShapingError),
24
25    #[error("Rendering failed: {0}")]
26    RenderingFailed(#[from] RenderError),
27
28    #[error("Export failed: {0}")]
29    ExportFailed(#[from] ExportError),
30
31    #[error("Pipeline error: {0}")]
32    Pipeline(String),
33
34    #[error("Configuration error: {0}")]
35    ConfigError(String),
36
37    #[error("IO error: {0}")]
38    Io(#[from] std::io::Error),
39
40    #[error("Other error: {0}")]
41    Other(String),
42}
43
44/// When fonts refuse to load
45#[derive(Debug, Error)]
46pub enum FontLoadError {
47    #[error("Font file not found: {0}")]
48    FileNotFound(String),
49
50    #[error("Invalid font data")]
51    InvalidData,
52
53    #[error("Font not supported: {0}")]
54    NotSupported(String),
55
56    #[error("System font not found: {0}")]
57    SystemFontNotFound(String),
58}
59
60/// When shaping goes wrong
61#[derive(Debug, Error)]
62pub enum ShapingError {
63    #[error("Invalid text input")]
64    InvalidText,
65
66    #[error("Font size {0} exceeds maximum allowed ({1}). Use a smaller font size for security.")]
67    FontSizeTooLarge(f32, f32),
68
69    #[error("Script not supported: {0}")]
70    ScriptNotSupported(String),
71
72    #[error("Language not supported: {0}")]
73    LanguageNotSupported(String),
74
75    #[error("Feature not supported: {0}")]
76    FeatureNotSupported(String),
77
78    #[error("Backend error: {0}")]
79    BackendError(String),
80}
81
82/// When rendering fails
83#[derive(Debug, Error)]
84pub enum RenderError {
85    #[error("Zero bitmap dimensions: {width}x{height}. This usually means empty text or all whitespace. Check that your text contains renderable characters.")]
86    ZeroDimensions { width: u32, height: u32 },
87
88    #[error("Bitmap dimensions {width}x{height} exceed limits (max {max_width}x{max_height}). Use smaller font sizes, implement line wrapping, or use SVG export for large renders.")]
89    DimensionsTooLarge {
90        width: u32,
91        height: u32,
92        max_width: u32,
93        max_height: u32,
94    },
95
96    #[error("Total bitmap pixels {total} exceed maximum ({max}). Requested {width}x{height}. Use smaller font sizes or SVG export.")]
97    TotalPixelsTooLarge {
98        width: u32,
99        height: u32,
100        total: u64,
101        max: u64,
102    },
103
104    /// Legacy variant for backwards compatibility - prefer ZeroDimensions or DimensionsTooLarge
105    #[error("Invalid bitmap dimensions: {width}x{height} (max 65,535 pixels per dimension). For long texts, use smaller font sizes, implement line wrapping, or use SVG export instead of bitmap rendering.")]
106    InvalidDimensions { width: u32, height: u32 },
107
108    #[error("Glyph count {0} exceeds maximum allowed ({1}). Split text into smaller chunks for security.")]
109    GlyphCountTooLarge(usize, usize),
110
111    #[error("Out of memory")]
112    OutOfMemory,
113
114    #[error("Format not supported: {0}")]
115    FormatNotSupported(String),
116
117    #[error("Backend error: {0}")]
118    BackendError(String),
119
120    #[error("Invalid font")]
121    InvalidFont,
122
123    #[error("Glyph not found: {0}")]
124    GlyphNotFound(u32),
125
126    #[error("Outline extraction failed")]
127    OutlineExtractionFailed,
128
129    #[error("Path building failed")]
130    PathBuildingFailed,
131
132    #[error("Pixmap creation failed")]
133    PixmapCreationFailed,
134}
135
136/// When export can't finish
137#[derive(Debug, Error)]
138pub enum ExportError {
139    #[error("Format not supported: {0}")]
140    FormatNotSupported(String),
141
142    #[error("Encoding failed: {0}")]
143    EncodingFailed(String),
144
145    #[error("Write failed: {0}")]
146    WriteFailed(String),
147}