Skip to main content

ass_renderer/utils/
errors.rs

1//! Error types for rendering
2
3#[cfg(feature = "nostd")]
4use alloc::string::String;
5#[cfg(feature = "nostd")]
6use core::fmt;
7#[cfg(not(feature = "nostd"))]
8use std::string::String;
9
10#[cfg(not(feature = "nostd"))]
11use thiserror::Error;
12
13/// Rendering error types
14#[cfg_attr(not(feature = "nostd"), derive(Error))]
15#[derive(Debug)]
16pub enum RenderError {
17    /// Invalid dimensions provided
18    #[cfg_attr(
19        not(feature = "nostd"),
20        error("Invalid dimensions: dimensions must be positive and non-zero")
21    )]
22    InvalidDimensions,
23
24    /// Invalid buffer size
25    #[cfg_attr(
26        not(feature = "nostd"),
27        error("Invalid buffer size: expected {expected} bytes, got {actual}")
28    )]
29    InvalidBufferSize {
30        /// Expected size
31        expected: usize,
32        /// Actual size
33        actual: usize,
34    },
35
36    /// Invalid pixmap creation
37    #[cfg_attr(not(feature = "nostd"), error("Failed to create pixmap"))]
38    InvalidPixmap,
39
40    /// No backend available
41    #[cfg_attr(not(feature = "nostd"), error("No rendering backend available"))]
42    NoBackendAvailable,
43
44    /// Unsupported backend
45    #[cfg_attr(not(feature = "nostd"), error("Unsupported backend: {0}"))]
46    UnsupportedBackend(&'static str),
47
48    /// Backend initialization failed
49    #[cfg_attr(not(feature = "nostd"), error("Backend initialization failed: {0}"))]
50    BackendInitFailed(String),
51
52    /// Generic backend error
53    #[cfg_attr(not(feature = "nostd"), error("Backend error: {0}"))]
54    BackendError(String),
55
56    /// Pipeline error
57    #[cfg_attr(not(feature = "nostd"), error("Pipeline error: {0}"))]
58    PipelineError(String),
59
60    /// Shaping error
61    #[cfg_attr(not(feature = "nostd"), error("Text shaping failed: {0}"))]
62    ShapingError(String),
63
64    /// Drawing error
65    #[cfg_attr(not(feature = "nostd"), error("Drawing failed: {0}"))]
66    DrawingError(String),
67
68    /// Invalid draw command
69    #[cfg_attr(not(feature = "nostd"), error("Invalid draw command: {0}"))]
70    InvalidDrawCommand(String),
71
72    /// Effect error
73    #[cfg_attr(not(feature = "nostd"), error("Effect application failed: {0}"))]
74    EffectError(String),
75
76    /// Compositing error
77    #[cfg_attr(not(feature = "nostd"), error("Compositing failed: {0}"))]
78    CompositingError(String),
79
80    /// Font error
81    #[cfg_attr(not(feature = "nostd"), error("Font error: {0}"))]
82    FontError(String),
83
84    /// GPU error
85    #[cfg_attr(not(feature = "nostd"), error("GPU error: {0}"))]
86    GpuError(String),
87
88    /// WASM error
89    #[cfg(target_arch = "wasm32")]
90    #[cfg_attr(not(feature = "nostd"), error("WASM error: {0}"))]
91    WasmError(String),
92
93    /// Resource limit exceeded
94    #[cfg_attr(not(feature = "nostd"), error("Resource limit exceeded: {0}"))]
95    ResourceLimitExceeded(String),
96
97    /// Invalid script
98    #[cfg_attr(not(feature = "nostd"), error("Invalid script: {0}"))]
99    InvalidScript(String),
100
101    /// Parse error
102    #[cfg_attr(not(feature = "nostd"), error("Parse error: {0}"))]
103    ParseError(String),
104
105    /// Invalid input
106    #[cfg_attr(not(feature = "nostd"), error("Invalid input: {0}"))]
107    InvalidInput(String),
108
109    /// Core error from ass-core
110    #[cfg_attr(not(feature = "nostd"), error("Core error: {0}"))]
111    CoreError(#[cfg_attr(not(feature = "nostd"), from)] ass_core::utils::CoreError),
112
113    /// Initialization error
114    #[cfg_attr(not(feature = "nostd"), error("Initialization error: {0}"))]
115    InitializationError(String),
116
117    /// IO error
118    #[cfg_attr(not(feature = "nostd"), error("IO error: {0}"))]
119    IOError(String),
120
121    /// Invalid state
122    #[cfg_attr(not(feature = "nostd"), error("Invalid state: {0}"))]
123    InvalidState(String),
124
125    /// Unsupported operation
126    #[cfg_attr(not(feature = "nostd"), error("Unsupported operation: {0}"))]
127    UnsupportedOperation(String),
128}
129
130impl RenderError {
131    /// Check if error is recoverable
132    pub fn is_recoverable(&self) -> bool {
133        matches!(
134            self,
135            Self::ShapingError(_)
136                | Self::DrawingError(_)
137                | Self::EffectError(_)
138                | Self::FontError(_)
139        )
140    }
141
142    /// Check if error indicates missing resources
143    pub fn is_resource_error(&self) -> bool {
144        matches!(self, Self::FontError(_) | Self::ResourceLimitExceeded(_))
145    }
146}
147
148// Manual Display implementation for nostd
149#[cfg(feature = "nostd")]
150impl fmt::Display for RenderError {
151    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
152        match self {
153            Self::InvalidDimensions => write!(
154                f,
155                "Invalid dimensions: dimensions must be positive and non-zero"
156            ),
157            Self::InvalidBufferSize { expected, actual } => {
158                write!(
159                    f,
160                    "Invalid buffer size: expected {expected} bytes, got {actual}"
161                )
162            }
163            Self::InvalidPixmap => write!(f, "Failed to create pixmap"),
164            Self::NoBackendAvailable => write!(f, "No rendering backend available"),
165            Self::UnsupportedBackend(s) => write!(f, "Unsupported backend: {s}"),
166            Self::BackendInitFailed(s) => write!(f, "Backend initialization failed: {s}"),
167            Self::BackendError(s) => write!(f, "Backend error: {s}"),
168            Self::PipelineError(s) => write!(f, "Pipeline error: {s}"),
169            Self::ShapingError(s) => write!(f, "Text shaping failed: {s}"),
170            Self::DrawingError(s) => write!(f, "Drawing failed: {s}"),
171            Self::InvalidDrawCommand(s) => write!(f, "Invalid draw command: {s}"),
172            Self::EffectError(s) => write!(f, "Effect application failed: {s}"),
173            Self::CompositingError(s) => write!(f, "Compositing failed: {s}"),
174            Self::FontError(s) => write!(f, "Font error: {s}"),
175            Self::GpuError(s) => write!(f, "GPU error: {s}"),
176            #[cfg(target_arch = "wasm32")]
177            Self::WasmError(s) => write!(f, "WASM error: {s}"),
178            Self::ResourceLimitExceeded(s) => write!(f, "Resource limit exceeded: {s}"),
179            Self::InvalidScript(s) => write!(f, "Invalid script: {s}"),
180            Self::ParseError(s) => write!(f, "Parse error: {s}"),
181            Self::InvalidInput(s) => write!(f, "Invalid input: {s}"),
182            Self::CoreError(e) => write!(f, "Core error: {e}"),
183            Self::InitializationError(s) => write!(f, "Initialization error: {s}"),
184            Self::IOError(s) => write!(f, "IO error: {s}"),
185            Self::InvalidState(s) => write!(f, "Invalid state: {s}"),
186            Self::UnsupportedOperation(s) => write!(f, "Unsupported operation: {s}"),
187        }
188    }
189}
190
191// Manual Error implementation for nostd
192#[cfg(feature = "nostd")]
193impl core::error::Error for RenderError {}