Skip to main content

gust_render/
gl_error.rs

1//
2//  Rust file | 2018
3//  Author: Alexandre Fourcat
4//  gl_error.rs
5//  module:
6//! gl error system
7
8use gl;
9use std::error::Error;
10
11#[derive(Debug)]
12pub enum GlError {
13    NoError,
14    InvalidEnum,
15    InvalidValue,
16    InvalidOperation,
17    InvalidFramebufferOperation,
18    OutOfMemory,
19}
20
21impl GlError {
22    pub fn new() -> Result<(), GlError> {
23        unsafe {
24            match gl::GetError() {
25                gl::NO_ERROR => Ok(()),
26                gl::INVALID_ENUM => Err(GlError::InvalidEnum),
27                gl::INVALID_VALUE => Err(GlError::InvalidValue),
28                gl::INVALID_OPERATION => Err(GlError::InvalidOperation),
29                gl::INVALID_FRAMEBUFFER_OPERATION => Err(GlError::InvalidFramebufferOperation),
30                gl::OUT_OF_MEMORY => Err(GlError::OutOfMemory),
31                _ => Ok(()),
32            }
33        }
34    }
35}
36
37impl Error for GlError {
38    fn cause(&self) -> Option<&Error> {
39        None
40    }
41}
42
43use std::fmt;
44
45impl fmt::Display for GlError {
46    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
47        match self {
48            GlError::NoError => write!(f, "No Error"),
49            GlError::InvalidEnum => write!(f, "Bad enum argument"),
50            GlError::InvalidValue => write!(f, "Bad value argument"),
51            GlError::InvalidOperation => write!(f, "Bad operation"),
52            GlError::InvalidFramebufferOperation => write!(f, "Bad framebuffer operation"),
53            GlError::OutOfMemory => write!(f, "No more gl memory"),
54        }
55    }
56}