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
//! A module containing the error structures for this crate.
extern crate core;
extern crate glium;
extern crate std;

use std::error::Error;

/// The error object that is returned when a shape fails to build.
#[derive(Debug, Copy, Clone)]
pub enum ShapeCreationError {
    /// The shape failed to build because vertex buffer could not be created.
    VertexBufferCreationError(glium::vertex::BufferCreationError),

    /// The shape failed to build because the number of divisions in the u axis
    /// is too small.
    NotEnoughDivisionsInU,

    /// The shape failed to build because the number of divisions in the v axis
    /// is too small.
    NotEnoughDivisionsInV,
}

impl std::error::Error for ShapeCreationError {
    fn description(&self) -> &str {
        match &self {
            ShapeCreationError::VertexBufferCreationError(ref err) => err.description(),
            ShapeCreationError::NotEnoughDivisionsInU => "Not enough divisions in the u axis",
            ShapeCreationError::NotEnoughDivisionsInV => "Not enough divisions in the v axis",
        }
    }

    fn cause(&self) -> Option<&dyn Error> {
        match &self {
            ShapeCreationError::VertexBufferCreationError(ref error) => Some(error),
            _ => None,
        }
    }
}

impl From<glium::vertex::BufferCreationError> for ShapeCreationError {
    fn from(error: glium::vertex::BufferCreationError) -> Self {
        ShapeCreationError::VertexBufferCreationError(error)
    }
}

impl core::fmt::Display for ShapeCreationError {
    fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::fmt::Result {
        write!(fmt, "{}", self.description())
    }
}