1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//! Error Handling

use crate::__gl;

use crate::device::Device;
use crate::pipeline::{Pipeline, Shader};
use std::{error, fmt, result};

/// Error return codes
///
/// Error handling in `grr` only deals with runtime-only detectable errors.
///
/// Other error codes returned by OpenGL are either treated as API miss use (see `Valid Usage` sections),
/// or indicate driver or implementation issues.
///
/// API validation is provided by the debug functionality on device creation.
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum Error {
    OutOfMemory,

    /// Shader compilation failure.
    CompileError(Shader),

    /// Link pipeline failure.
    LinkError(Pipeline),
}

/// A specialized Result type for `grr` operations.
pub type Result<T> = result::Result<T, Error>;

impl Device {
    pub(crate) unsafe fn get_error(&self) -> Result<()> {
        let err = self.0.GetError();
        match err {
            __gl::OUT_OF_MEMORY => Err(Error::OutOfMemory),
            _ => Ok(()),
        }
    }
}

impl error::Error for Error {}

impl fmt::Display for Error {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> result::Result<(), fmt::Error> {
        match *self {
            Error::OutOfMemory => write!(fmt, "OutOfMemory"),
            Error::CompileError(_) => write!(fmt, "CompileError"),
            Error::LinkError(_) => write!(fmt, "LinkError"),
        }
    }
}