use thiserror::Error;
use crate::resources::{font::FontError, image::ImageResourceError};
pub use takumi_css::error::{
StyleDeclarationBlockParseError, StyleSheetParseError, StyleSheetParseErrorKind,
};
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum WebPError {
#[error("WebP encoder setup failed")]
EncoderSetupFailed,
#[error("WebP encode failed")]
EncodeFailed,
#[error("WebP encode failed ({error_code})")]
EncodeFailedWithCode {
error_code: String,
},
#[error("{name} must be in 1..={max}, got {value}")]
InvalidDimension {
name: &'static str,
value: u32,
max: u32,
},
#[error("WebP animation frame dimensions must be in 1..={max}, got {width}x{height}")]
InvalidFrameDimensions {
width: u32,
height: u32,
max: u32,
},
#[error("animation must contain at least one frame")]
EmptyAnimation,
#[error(
"frame {index} dimensions {frame_width}x{frame_height} exceed canvas {canvas_width}x{canvas_height}"
)]
FrameExceedsCanvas {
index: usize,
frame_width: u32,
frame_height: u32,
canvas_width: u32,
canvas_height: u32,
},
#[error("all animation frames must have the same dimensions")]
MixedFrameDimensions,
#[error("WebP encoded data is invalid or unsupported")]
InvalidEncodedData,
#[error("WebP container size exceeds supported limits")]
ContainerSizeOverflow,
}
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum Error {
#[error("Image resolution error: {0}")]
ImageResolveError(#[from] ImageResourceError),
#[error("IO error: {0}")]
IoError(#[from] std::io::Error),
#[error("PNG encoding error: {0}")]
PngError(#[from] png::EncodingError),
#[error("WebP encoding error: {0}")]
#[cfg(target_arch = "wasm32")]
WebPEncodingError(#[from] image_webp::EncodingError),
#[error("WebP error: {0}")]
WebPError(#[from] WebPError),
#[error("GIF encoding error: {0}")]
GifEncodingError(#[from] gif::EncodingError),
#[error("Image error: {0}")]
ImageError(#[from] image::ImageError),
#[error("Invalid viewport: width or height cannot be 0")]
InvalidViewport,
#[error("invalid RGBA buffer length: expected {expected} bytes, got {actual}")]
InvalidRgbaBufferLength {
actual: usize,
expected: usize,
},
#[error("{format} animation must contain at least one frame")]
EmptyAnimationFrames {
format: &'static str,
},
#[error("all {format} animation frames must share the same dimensions")]
MixedAnimationFrameDimensions {
format: &'static str,
},
#[error("GIF frame dimensions must be <= {max}x{max}, got {width}x{height}")]
GifFrameDimensionsTooLarge {
width: u32,
height: u32,
max: u16,
},
#[error("Font error: {0}")]
FontError(#[from] FontError),
#[error("Layout error: {0}")]
LayoutError(taffy::TaffyError),
}
impl From<taffy::TaffyError> for Error {
fn from(err: taffy::TaffyError) -> Self {
Self::LayoutError(err)
}
}
pub type Result<T> = std::result::Result<T, Error>;