diff --git a/crates/bevy_render/src/texture/image.rs b/crates/bevy_render/src/texture/image.rs index 698c6159a714d..3654af26699de 100644 --- a/crates/bevy_render/src/texture/image.rs +++ b/crates/bevy_render/src/texture/image.rs @@ -85,12 +85,13 @@ impl ImageFormat { _ => return None, }) } +} - /// Attempts to convert to [`image::ImageFormat`]. - /// - /// Unsupported formats will become [`None`]. - pub fn as_image_crate_format(&self) -> Option { - Some(match self { +impl TryFrom for image::ImageFormat { + type Error = (); + + fn try_from(value: ImageFormat) -> Result { + Ok(match value { ImageFormat::Avif => image::ImageFormat::Avif, ImageFormat::Bmp => image::ImageFormat::Bmp, ImageFormat::Dds => image::ImageFormat::Dds, @@ -105,15 +106,16 @@ impl ImageFormat { ImageFormat::Tga => image::ImageFormat::Tga, ImageFormat::Tiff => image::ImageFormat::Tiff, ImageFormat::WebP => image::ImageFormat::WebP, - ImageFormat::Basis | ImageFormat::Ktx2 => return None, + ImageFormat::Basis | ImageFormat::Ktx2 => return Err(()), }) } +} - /// Attempts to convert from [`image::ImageFormat`]. - /// - /// Unsupported formats will become [`None`]. - pub fn from_image_crate_format(format: image::ImageFormat) -> Option { - Some(match format { +impl TryFrom for ImageFormat { + type Error = (); + + fn try_from(value: image::ImageFormat) -> Result { + Ok(match value { image::ImageFormat::Avif => ImageFormat::Avif, image::ImageFormat::Bmp => ImageFormat::Bmp, image::ImageFormat::Dds => ImageFormat::Dds, @@ -128,7 +130,7 @@ impl ImageFormat { image::ImageFormat::Tga => ImageFormat::Tga, image::ImageFormat::Tiff => ImageFormat::Tiff, image::ImageFormat::WebP => ImageFormat::WebP, - _ => return None, + _ => return Err(()), }) } } @@ -710,8 +712,8 @@ impl Image { } _ => { let image_crate_format = format - .as_image_crate_format() - .ok_or_else(|| TextureError::UnsupportedTextureFormat(format!("{format:?}")))?; + .try_into() + .map_err(|_| TextureError::UnsupportedTextureFormat(format!("{format:?}")))?; let mut reader = image::io::Reader::new(std::io::Cursor::new(buffer)); reader.set_format(image_crate_format); reader.no_limits(); diff --git a/crates/bevy_render/src/texture/image_loader.rs b/crates/bevy_render/src/texture/image_loader.rs index 5e22467fdfb2a..da46cbe195972 100644 --- a/crates/bevy_render/src/texture/image_loader.rs +++ b/crates/bevy_render/src/texture/image_loader.rs @@ -115,8 +115,9 @@ impl AssetLoader for ImageLoader { path: load_context.path().display().to_string(), })?; - let format = ImageFormat::from_image_crate_format(image_crate_format) - .ok_or_else(|| FileTextureError { + let format = image_crate_format + .try_into() + .map_err(|_| FileTextureError { error: TextureError::InvalidImageContentType(image_crate_format), path: load_context.path().display().to_string(), })?;