rust-and-opengl-lessons icon indicating copy to clipboard operation
rust-and-opengl-lessons copied to clipboard

Failure is deprecated

Open Nercury opened this issue 4 years ago • 2 comments

https://github.com/rust-lang-nursery/failure/pull/347

Nercury avatar May 03 '20 07:05 Nercury

Hi, I'm going through the tutorial and I've stumbled upon this so I used the proposed replacements, thiserror for the auto Display trait functionality and anyhow in main file for "anyerror" type handling.

e.g shader.rs

use thiserror::Error;

#[derive(Error, Debug)]
pub enum Error {
    #[error("Failed to load resource {}", name)]
    ResourceLoad {
        name: String,
        #[source]
        inner: resources::Error,
    },

    #[error("Can not determine shader type for resource {}", name)]
    CanNotDetermineShaderTypeForResource { name: String },

    #[error("Failed to compile shader {}: {}", name, message)]
    CompileError { name: String, message: String },

    #[error("Failed to link program {}: {}", name, message)]
    LinkError { name: String, message: String },
}

and main.rs (Truncated for brevity, showing mainly error handling)

use anyhow::{Error, Result};

fn run() -> Result<()> {


    let res = Resources::from_relative_exe_path(Path::new("assets")).unwrap();

    let sdl = sdl2::init().map_err(|message| Error::msg(message))?;
    let video = sdl.video().map_err(|message| Error::msg(message))?;
    
    .....

    let window = video
        .window("Game", 900, 700)
        .opengl()
        .resizable()
        .build()?;

    let _gl_context = window
        .gl_create_context()
        .map_err(|message| Error::msg(message))?;

    ....

    let shader_program = render_gl::Program::from_res(&gl, &res, "shaders/triangle")?;

     
    ...
   

    // Main loop
    let mut event_pump = sdl.event_pump().map_err(|message| Error::msg(message))?;

    'main: loop {
       ...
    }

    Ok(())
}


SpirosMakris avatar Aug 10 '20 13:08 SpirosMakris

Thanks for the update! It's good to know what is the current fashionable error handling crate is :).

Nercury avatar Aug 10 '20 20:08 Nercury