Skip to main content

edgefirst_gl/
error.rs

1use crate::{
2    gl::{
3        GetError, INVALID_ENUM, INVALID_FRAMEBUFFER_OPERATION, INVALID_OPERATION, INVALID_VALUE,
4        NO_ERROR, OUT_OF_MEMORY,
5    },
6    GLenum,
7};
8
9/// Error Object for OpenGL.
10#[derive(Clone, Copy, Default, PartialEq, Eq)]
11pub struct Error(GLenum);
12
13impl Error {
14    /// Create an new error with glGetError.
15    pub fn new() -> Self {
16        Self(unsafe { GetError() })
17    }
18
19    /// Return human readable text of the error code.
20    pub fn to_str(self) -> &'static str {
21        match self.0 {
22            NO_ERROR => "No error",
23            INVALID_ENUM => "An unacceptable value is specified for an enumerated argument",
24            INVALID_VALUE => "A numeric argument is out of range",
25            INVALID_OPERATION => "The specified operation is not allowed in the current state",
26            INVALID_FRAMEBUFFER_OPERATION => "The framebuffer object is not complete",
27            OUT_OF_MEMORY => "There is not enough memory left to execute the command",
28            _ => "Unknown error",
29        }
30    }
31}
32
33impl std::fmt::Debug for Error {
34    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
35        write!(f, "Error({}, \"{}\")", self.0, self.to_str())
36    }
37}
38
39impl std::fmt::Display for Error {
40    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
41        write!(f, "Error({}, \"{}\")", self.0, self.to_str())
42    }
43}
44
45impl std::error::Error for Error {}