1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-30 13:20:05 -04:00

Support loading images with weird urls and improve error message (#5431)

* Closes #5341
* [x] I have followed the instructions in the PR template
This commit is contained in:
lucasmerlin
2024-12-05 07:33:02 +01:00
committed by GitHub
parent f687b27efc
commit 291b83b7be
4 changed files with 70 additions and 29 deletions

View File

@@ -3455,15 +3455,23 @@ impl Context {
return Err(load::LoadError::NoImageLoaders);
}
let mut format = None;
// Try most recently added loaders first (hence `.rev()`)
for loader in image_loaders.iter().rev() {
match loader.load(self, uri, size_hint) {
Err(load::LoadError::NotSupported) => continue,
Err(load::LoadError::FormatNotSupported { detected_format }) => {
format = format.or(detected_format);
continue;
}
result => return result,
}
}
Err(load::LoadError::NoMatchingImageLoader)
Err(load::LoadError::NoMatchingImageLoader {
detected_format: format,
})
}
/// Try loading the texture from the given uri using any available texture loaders.

View File

@@ -77,16 +77,19 @@ pub enum LoadError {
/// Programmer error: There are no image loaders installed.
NoImageLoaders,
/// A specific loader does not support this scheme, protocol or image format.
/// A specific loader does not support this scheme or protocol.
NotSupported,
/// A specific loader does not support the format of the image.
FormatNotSupported { detected_format: Option<String> },
/// Programmer error: Failed to find the bytes for this image because
/// there was no [`BytesLoader`] supporting the scheme.
NoMatchingBytesLoader,
/// Programmer error: Failed to parse the bytes as an image because
/// there was no [`ImageLoader`] supporting the scheme.
NoMatchingImageLoader,
/// there was no [`ImageLoader`] supporting the format.
NoMatchingImageLoader { detected_format: Option<String> },
/// Programmer error: no matching [`TextureLoader`].
/// Because of the [`DefaultTextureLoader`], this error should never happen.
@@ -96,6 +99,20 @@ pub enum LoadError {
Loading(String),
}
impl LoadError {
/// Returns the (approximate) size of the error message in bytes.
pub fn byte_size(&self) -> usize {
match self {
Self::FormatNotSupported { detected_format }
| Self::NoMatchingImageLoader { detected_format } => {
detected_format.as_ref().map_or(0, |s| s.len())
}
Self::Loading(message) => message.len(),
_ => std::mem::size_of::<Self>(),
}
}
}
impl Display for LoadError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
@@ -105,12 +122,15 @@ impl Display for LoadError {
Self::NoMatchingBytesLoader => f.write_str("No matching BytesLoader. Either you need to call Context::include_bytes, or install some more bytes loaders, e.g. using egui_extras."),
Self::NoMatchingImageLoader => f.write_str("No matching ImageLoader. Either you need to call Context::include_bytes, or install some more bytes loaders, e.g. using egui_extras."),
Self::NoMatchingImageLoader { detected_format: None } => f.write_str("No matching ImageLoader. Either no ImageLoader is installed or the image is corrupted / has an unsupported format."),
Self::NoMatchingImageLoader { detected_format: Some(detected_format) } => write!(f, "No matching ImageLoader for format: {detected_format:?}. Make sure you enabled the necessary features on the image crate."),
Self::NoMatchingTextureLoader => f.write_str("No matching TextureLoader. Did you remove the default one?"),
Self::NotSupported => f.write_str("Image scheme or URI not supported by this loader"),
Self::FormatNotSupported { detected_format } => write!(f, "Image format not supported by this loader: {detected_format:?}"),
Self::Loading(message) => f.write_str(message),
}
}