Skip to content

Commit

Permalink
use TryFrom
Browse files Browse the repository at this point in the history
  • Loading branch information
robtfm committed Feb 22, 2024
1 parent 3805f6c commit 8808782
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
30 changes: 16 additions & 14 deletions crates/bevy_render/src/texture/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<image::ImageFormat> {
Some(match self {
impl TryFrom<ImageFormat> for image::ImageFormat {
type Error = ();

fn try_from(value: ImageFormat) -> Result<Self, Self::Error> {
Ok(match value {
ImageFormat::Avif => image::ImageFormat::Avif,
ImageFormat::Bmp => image::ImageFormat::Bmp,
ImageFormat::Dds => image::ImageFormat::Dds,
Expand All @@ -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<Self> {
Some(match format {
impl TryFrom<image::ImageFormat> for ImageFormat {
type Error = ();

fn try_from(value: image::ImageFormat) -> Result<Self, Self::Error> {
Ok(match value {
image::ImageFormat::Avif => ImageFormat::Avif,
image::ImageFormat::Bmp => ImageFormat::Bmp,
image::ImageFormat::Dds => ImageFormat::Dds,
Expand All @@ -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(()),
})
}
}
Expand Down Expand Up @@ -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();
Expand Down
5 changes: 3 additions & 2 deletions crates/bevy_render/src/texture/image_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
})?;
Expand Down

0 comments on commit 8808782

Please sign in to comment.