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
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
pub use image::ImageError;

use std::error::Error;
use std::io::Error as IoError;
use std::convert::From;
use std;

use glium;
use image_atlas;
use image_atlas::image_atlas_bincode as bincode;

/// A result returning either a value or a lux-generated error.
pub type LuxResult<A> = Result<A, LuxError>;

/// Any error that Lux might run into.
#[derive(Debug)]
pub enum LuxError {
    /// An error that can occur when creating a window.
    WindowError(String),
    /// An error that can occur when creating an opengl context.
    OpenGlError(String),
    /// An error related to image decoding.
    ImageError(ImageError),
    /// An error that can occur when compiling or linking shaders.
    ShaderError(glium::ProgramCreationError),
    /// An error that can occur when required I/O fails.
    IoError(IoError),
    /// An error that can occur when attempting to use a font that hasn't
    /// been loaded yet.
    FontNotLoaded(String),
    /// An error that was produced while submitting a draw call
    DrawError(glium::DrawError),
    /// An error occured while creating a texture
    TextureCreationError(glium::texture::TextureCreationError),
    /// An error creating an index buffer occured
    IndexBufferCreationError,
    /// An error creating an vertex buffer occured
    VertexBufferCreationError,
    /// An error occurred when reading the metadata of a font
    FontMetadataError(bincode::DecodingError)
}

impl Error for LuxError {
    fn description(&self) -> &str {
        match self {
            &LuxError::WindowError(ref s) => &s[..],
            &LuxError::OpenGlError(ref s) => &s[..],
            &LuxError::ShaderError(ref e) => e.description(),
            &LuxError::IoError(ref ioe) => Error::description(ioe),
            &LuxError::FontNotLoaded(ref s) => &s[..],
            // TODO: implement this when glium/959 is finished.
            &LuxError::ImageError(ref e) => e.description(),
            &LuxError::DrawError(_) => "",
            &LuxError::TextureCreationError(_) => "",
            &LuxError::IndexBufferCreationError => "An index buffer could not be created",
            &LuxError::VertexBufferCreationError => "A vertex buffer could not be created",
            &LuxError::FontMetadataError(ref e) => e.description()
        }
    }
}

impl From<image_atlas::DecodingError> for LuxError {
    fn from(e: image_atlas::DecodingError) -> LuxError {
        use image_atlas::DecodingError::*;
        match e {
            ImageDecodingError(e) => LuxError::ImageError(e),
            BincodeDecodingError(e) => LuxError::FontMetadataError(e),
            IoError(e) => LuxError::IoError(e)
        }
    }
}

impl From<glium::ProgramCreationError> for LuxError {
    fn from(e: glium::ProgramCreationError) -> LuxError {
        LuxError::ShaderError(e)
    }
}

impl From<glium::texture::TextureCreationError> for LuxError {
    fn from(e: glium::texture::TextureCreationError) -> LuxError {
        LuxError::TextureCreationError(e)
    }
}

impl From<IoError> for LuxError {
    fn from(ioe: IoError) -> LuxError {
        LuxError::IoError(ioe)
    }
}

impl From<glium::DrawError> for LuxError {
    fn from(e: glium::DrawError) -> LuxError {
        LuxError::DrawError(e)
    }
}

impl From<ImageError> for LuxError {
    fn from(e: ImageError) -> LuxError {
        LuxError::ImageError(e)
    }
}


impl From<glium::vertex::BufferCreationError> for LuxError {
    fn from(_: glium::vertex::BufferCreationError) -> LuxError {
        LuxError::VertexBufferCreationError
    }
}

impl From<glium::index::BufferCreationError> for LuxError {
    fn from(_: glium::index::BufferCreationError) -> LuxError {
        LuxError::IndexBufferCreationError
    }
}

impl std::fmt::Display for LuxError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
        match self {
            &LuxError::WindowError(ref s) => s.fmt(f),
            &LuxError::OpenGlError(ref s) => s.fmt(f),
            &LuxError::ShaderError(ref e) => e.fmt(f),
            &LuxError::IoError(ref e) => e.fmt(f),
            &LuxError::FontNotLoaded(ref s) => s.fmt(f),
            &LuxError::DrawError(ref e) => e.fmt(f),
            &LuxError::ImageError(ref e) => e.fmt(f),
            &LuxError::TextureCreationError(ref e) => std::fmt::Debug::fmt(&e, f),
            &LuxError::IndexBufferCreationError => "An index buffer could not be created".fmt(f),
            &LuxError::VertexBufferCreationError => "A vertex buffer could not be created".fmt(f),
            &LuxError::FontMetadataError(ref e) => e.fmt(f)
        }
    }
}