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
//            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
//                    Version 2, December 2004
//
// Copyleft (ↄ) meh. <meh@schizofreni.co> | http://meh.schizofreni.co
//
// Everyone is permitted to copy and distribute verbatim or modified
// copies of this license document, and changing it is allowed as long
// as the name is changed.
//
//            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
//   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
//
//  0. You just DO WHAT THE FUCK YOU WANT TO.

use std::fmt;
use std::error;
use std::io;
use std::env;

#[cfg(feature = "renderer")]
use gl;
use log;

pub type Result<T> = ::std::result::Result<T, Error>;

#[derive(Debug)]
pub enum Error {
	Io(io::Error),
	#[cfg(feature = "renderer")]
	ContextCreation(gl::IncompatibleOpenGl),
	#[cfg(feature = "renderer")]
	SwapBuffers(gl::SwapBuffersError),
	Env(env::VarError),
	Logger(log::SetLoggerError),
	Protocol,
}

#[cfg(feature = "renderer")]
#[derive(Debug)]
pub enum Display {
	NotFound,
	Visual,
	Context,
}

impl From<io::Error> for Error {
	fn from(value: io::Error) -> Self {
		Error::Io(value)
	}
}

impl From<env::VarError> for Error {
	fn from(value: env::VarError) -> Self {
		Error::Env(value)
	}
}

impl From<log::SetLoggerError> for Error {
	fn from(value: log::SetLoggerError) -> Self {
		Error::Logger(value)
	}
}

#[cfg(feature = "renderer")]
impl From<Display> for Error {
	fn from(value: Display) -> Self {
		Error::ContextCreation(gl::IncompatibleOpenGl(match value {
			Display::NotFound => "could not open display",
			Display::Visual => "could not find appropriate visual",
			Display::Context => "could not create context",
		}.into()))
	}
}

#[cfg(feature = "renderer")]
impl From<gl::IncompatibleOpenGl> for Error {
	fn from(value: gl::IncompatibleOpenGl) -> Self {
		Error::ContextCreation(value)
	}
}

#[cfg(feature = "renderer")]
impl From<gl::SwapBuffersError> for Error {
	fn from(value: gl::SwapBuffersError) -> Self {
		Error::SwapBuffers(value)
	}
}

impl fmt::Display for Error {
	fn fmt(&self, f: &mut fmt::Formatter) -> ::std::result::Result<(), fmt::Error> {
		f.write_str(error::Error::description(self))
	}
}

impl error::Error for Error {
	fn description(&self) -> &str {
		match *self {
			Error::Io(ref err) =>
				err.description(),

			#[cfg(feature = "renderer")]
			Error::ContextCreation(..) =>
				"OpenGL error.",

			#[cfg(feature = "renderer")]
			Error::SwapBuffers(ref err) =>
				err.description(),

			Error::Env(ref err) =>
				err.description(),

			Error::Logger(ref err) =>
				err.description(),

			Error::Protocol =>
				"Protocol error.",
		}
	}
}