mirror of
https://github.com/openai/codex.git
synced 2026-05-02 12:21:26 +03:00
Cleanup image semantics in code mode.
`view_image` now returns `{image_url:string, details?: string}`
`image()` now allows both string parameter and `{image_url:string,
details?: string}`
56 lines
1.5 KiB
Rust
56 lines
1.5 KiB
Rust
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 },
|
|
}
|
|
|
|
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(_),
|
|
..
|
|
}
|
|
)
|
|
}
|
|
}
|