Skip to content

Commit

Permalink
implement Display trait for most error types
Browse files Browse the repository at this point in the history
  • Loading branch information
bbogdan-ov authored and not-fl3 committed Jan 13, 2025
1 parent c09662d commit 0d40431
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 14 deletions.
20 changes: 13 additions & 7 deletions src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,25 @@ pub enum Error {
IOSAssetNoData,
}

impl From<std::io::Error> 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<std::io::Error> for Error {
fn from(e: std::io::Error) -> Error {
Error::IOError(e)
}
}
impl std::error::Error for Error {}

pub type Response = Result<Vec<u8>, Error>;

Expand Down
24 changes: 18 additions & 6 deletions src/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -297,15 +306,18 @@ impl From<std::ffi::NulError> 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.
Expand Down
13 changes: 13 additions & 0 deletions src/native/egl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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(
Expand Down
5 changes: 4 additions & 1 deletion src/native/linux_x11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<module::Error> for X11Error {
Expand Down
11 changes: 11 additions & 0 deletions src/native/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -79,6 +88,8 @@ mod windows {
}
}

use std::fmt::Display;

#[cfg(any(target_os = "linux", target_os = "android"))]
pub use linux::*;

Expand Down

0 comments on commit 0d40431

Please sign in to comment.