Skip to content

Commit

Permalink
improve "no image loaders" error message
Browse files Browse the repository at this point in the history
  • Loading branch information
jprochazk committed Sep 12, 2023
1 parent e6c8e61 commit 1686b59
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
10 changes: 9 additions & 1 deletion crates/egui/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2038,17 +2038,25 @@ impl Context {
///
/// # Errors
/// This may fail with:
/// - [`LoadError::NoImageLoaders`][no_image_loaders] if tbere are no registered image loaders.
/// - [`LoadError::NotSupported`][not_supported] if none of the registered loaders support loading the given `uri`.
/// - [`LoadError::Custom`][custom] if one of the loaders _does_ support loading the `uri`, but the loading process failed.
///
/// ⚠ May deadlock if called from within an `ImageLoader`!
///
/// [no_image_loaders]: crate::load::LoadError::NoImageLoaders
/// [not_supported]: crate::load::LoadError::NotSupported
/// [custom]: crate::load::LoadError::Custom
pub fn try_load_image(&self, uri: &str, size_hint: load::SizeHint) -> load::ImageLoadResult {
crate::profile_function!();

for loader in self.loaders().image.lock().iter() {
let loaders = self.loaders();
let loaders = loaders.image.lock();
if loaders.is_empty() {
return Err(load::LoadError::NoImageLoaders);
}

for loader in loaders.iter() {
match loader.load(self, uri, size_hint) {
Err(load::LoadError::NotSupported) => continue,
result => return result,
Expand Down
4 changes: 4 additions & 0 deletions crates/egui/src/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ use std::{error::Error as StdError, fmt::Display, sync::Arc};
/// Represents a failed attempt at loading an image.
#[derive(Clone, Debug)]
pub enum LoadError {
/// There are no image loaders installed.
NoImageLoaders,

/// This loader does not support this protocol or image format.
NotSupported,

Expand All @@ -76,6 +79,7 @@ pub enum LoadError {
impl Display for LoadError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
LoadError::NoImageLoaders => f.write_str("no image loaders are installed"),
LoadError::NotSupported => f.write_str("not supported"),
LoadError::Custom(message) => f.write_str(message),
}
Expand Down

0 comments on commit 1686b59

Please sign in to comment.