aKode fase 5: rinomina interna (crate akode-*, akode-rs/, akode-cli/, binario akode, AKODE_*, ~/.akode, snapshot)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
63
akode-rs/utils/image/src/error.rs
Normal file
63
akode-rs/utils/image/src/error.rs
Normal file
@@ -0,0 +1,63 @@
|
||||
use image::ImageError;
|
||||
use image::ImageFormat;
|
||||
use std::path::PathBuf;
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum ImageProcessingError {
|
||||
#[error("failed to read image at {path}: {source}")]
|
||||
Read {
|
||||
path: PathBuf,
|
||||
#[source]
|
||||
source: std::io::Error,
|
||||
},
|
||||
#[error("failed to decode image at {path}: {source}")]
|
||||
Decode {
|
||||
path: PathBuf,
|
||||
#[source]
|
||||
source: image::ImageError,
|
||||
},
|
||||
#[error("failed to encode image as {format:?}: {source}")]
|
||||
Encode {
|
||||
format: ImageFormat,
|
||||
#[source]
|
||||
source: image::ImageError,
|
||||
},
|
||||
#[error("unsupported image `{mime}`")]
|
||||
UnsupportedImageFormat { mime: String },
|
||||
#[error("invalid image data URL: {reason}")]
|
||||
InvalidDataUrl { reason: String },
|
||||
#[error("image {representation} is too large ({size} bytes; max {max} bytes)")]
|
||||
ImageTooLarge {
|
||||
representation: &'static str,
|
||||
size: usize,
|
||||
max: usize,
|
||||
},
|
||||
}
|
||||
|
||||
impl ImageProcessingError {
|
||||
pub fn decode_error(path: &std::path::Path, source: image::ImageError) -> Self {
|
||||
if matches!(source, ImageError::Decoding(_)) {
|
||||
return ImageProcessingError::Decode {
|
||||
path: path.to_path_buf(),
|
||||
source,
|
||||
};
|
||||
}
|
||||
|
||||
let mime = mime_guess::from_path(path)
|
||||
.first()
|
||||
.map(|mime_guess| mime_guess.essence_str().to_owned())
|
||||
.unwrap_or_else(|| "unknown".to_string());
|
||||
ImageProcessingError::UnsupportedImageFormat { mime }
|
||||
}
|
||||
|
||||
pub fn is_invalid_image(&self) -> bool {
|
||||
matches!(
|
||||
self,
|
||||
ImageProcessingError::Decode {
|
||||
source: ImageError::Decoding(_),
|
||||
..
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user