From 0d4043157531b1972e58d96bcc4d4827d853d036 Mon Sep 17 00:00:00 2001 From: bogdanov Date: Mon, 13 Jan 2025 16:37:47 +0000 Subject: [PATCH] implement `Display` trait for most error types --- src/fs.rs | 20 +++++++++++++------- src/graphics.rs | 24 ++++++++++++++++++------ src/native/egl.rs | 13 +++++++++++++ src/native/linux_x11.rs | 5 ++++- src/native/module.rs | 11 +++++++++++ 5 files changed, 59 insertions(+), 14 deletions(-) diff --git a/src/fs.rs b/src/fs.rs index 46abe2d5..faff64a6 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -12,19 +12,25 @@ pub enum Error { IOSAssetNoData, } +impl From for Error { + fn from(e: std::io::Error) -> Error { + Error::IOError(e) + } +} + impl std::fmt::Display for Error { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - match *self { - _ => write!(f, "Error: {:?}", self), + match self { + Self::IOError(e) => write!(f, "I/O error: {e}"), + Self::DownloadFailed => write!(f, "Download failed"), + Self::AndroidAssetLoadingError => write!(f, "[android] Failed to load asset"), + Self::IOSAssetNoSuchFile => write!(f, "[ios] No such asset file"), + Self::IOSAssetNoData => write!(f, "[ios] No data in asset file"), } } } -impl From for Error { - fn from(e: std::io::Error) -> Error { - Error::IOError(e) - } -} +impl std::error::Error for Error {} pub type Response = Result, Error>; diff --git a/src/graphics.rs b/src/graphics.rs index c289f89c..dde3edb6 100644 --- a/src/graphics.rs +++ b/src/graphics.rs @@ -278,6 +278,15 @@ pub enum ShaderType { Fragment, } +impl Display for ShaderType { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::Vertex => write!(f, "Vertex"), + Self::Fragment => write!(f, "Fragment"), + } + } +} + #[derive(Clone, Debug)] pub enum ShaderError { CompilationError { @@ -297,15 +306,18 @@ impl From for ShaderError { impl Display for ShaderError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{:?}", self) // Display the same way as Debug + match self { + Self::CompilationError { + shader_type, + error_message, + } => write!(f, "{shader_type} shader error:\n{error_message}"), + Self::LinkError(msg) => write!(f, "Link shader error:\n{msg}"), + Self::FFINulError(e) => write!(f, "{e}"), + } } } -impl Error for ShaderError { - fn source(&self) -> Option<&(dyn Error + 'static)> { - None - } -} +impl Error for ShaderError {} /// List of all the possible formats of input data when uploading to texture. /// The list is built by intersection of texture formats supported by 3.3 core profile and webgl1. diff --git a/src/native/egl.rs b/src/native/egl.rs index 4d87439a..6aefbb0c 100644 --- a/src/native/egl.rs +++ b/src/native/egl.rs @@ -17,6 +17,7 @@ pub type EGLNativePixmapType = ::core::ffi::c_ulong; pub type EGLNativeWindowType = ::core::ffi::c_ulong; pub use core::ptr::null_mut; +use std::fmt::Display; pub const EGL_SUCCESS: u32 = 12288; @@ -256,6 +257,18 @@ pub enum EglError { CreateContextFailed, } +impl Display for EglError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::NoDisplay => write!(f, "No display"), + Self::InitializeFailed => write!(f, "Failed to initialize context"), + Self::CreateContextFailed => write!(f, "Faild to create context"), + } + } +} + +impl std::error::Error for EglError {} + pub struct Egl {} pub unsafe fn create_egl_context( diff --git a/src/native/linux_x11.rs b/src/native/linux_x11.rs index 923bbc55..5ddb9eef 100644 --- a/src/native/linux_x11.rs +++ b/src/native/linux_x11.rs @@ -25,7 +25,10 @@ pub enum X11Error { } impl std::fmt::Display for X11Error { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{:?}", self) + match self { + Self::LibraryNotFound(e) => write!(f, "Library not found error: {e}"), + Self::GLXError(msg) => write!(f, "GLX error:\n{msg}"), + } } } impl From for X11Error { diff --git a/src/native/module.rs b/src/native/module.rs index ddd91f56..bcad5075 100644 --- a/src/native/module.rs +++ b/src/native/module.rs @@ -4,6 +4,15 @@ pub enum Error { DlSymError(String), } +impl Display for Error { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::DlOpenError(msg) => write!(f, "Shared library open error:\n{msg}"), + Self::DlSymError(msg) => write!(f, "Shared library symlink error:\n{msg}"), + } + } +} + #[cfg(any(target_os = "linux", target_os = "android"))] pub mod linux { use super::Error; @@ -79,6 +88,8 @@ mod windows { } } +use std::fmt::Display; + #[cfg(any(target_os = "linux", target_os = "android"))] pub use linux::*;