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
135
136
137
138
139
140
141
142
143
use std::error;
use std::fmt;

use ffi::*;
use ::traits::Device;

/// OpenAL error type.
#[derive(Copy, Clone)]
pub enum Error {
	/// There is no current error.
	None,

	/// The device handle or specifier names an inaccessible driver/server.
	InvalidDevice,

	/// The Context argument does not name a valid context.
	InvalidContext,

	/// Invalid name parameter.
	InvalidName,

	/// Illegal call.
	InvalidOperation,

	/// Invalid parameter.
	InvalidEnum,

	/// Invalid enum parameter value.
	InvalidValue,

	/// Unable to allocate memory.
	OutOfMemory,
}

#[doc(hidden)]
#[derive(Copy, Clone)]
pub struct AL(pub ALenum);

#[doc(hidden)]
#[derive(Copy, Clone)]
pub struct ALC(pub ALCenum);

impl Error {
	/// Check if there was an error on the last operation.
	pub fn last() -> Option<Self> {
		unsafe {
			match Error::from(AL(alGetError())) {
				Error::None =>
					None,

				error =>
					Some(error)
			}
		}
	}

	/// Check if there was an error on the last operation of the given device.
	pub fn last_for<T: Device>(device: &T) -> Option<Self> {
		unsafe {
			match Error::from(ALC(alcGetError(device.as_ptr()))) {
				Error::None =>
					None,

				error =>
					Some(error)
			}
		}
	}
}

impl From<AL> for Error {
	fn from(value: AL) -> Error {
		match value.0 {
			AL_NO_ERROR          => Error::None,
			AL_INVALID_NAME      => Error::InvalidName,
			AL_INVALID_ENUM      => Error::InvalidEnum,
			AL_INVALID_VALUE     => Error::InvalidValue,
			AL_INVALID_OPERATION => Error::InvalidOperation,
			AL_OUT_OF_MEMORY     => Error::OutOfMemory,

			_ => Error::None,
		}
	}
}

impl From<ALC> for Error {
	fn from(value: ALC) -> Error {
		match value.0 {
			ALC_NO_ERROR        => Error::None,
			ALC_INVALID_CONTEXT => Error::InvalidContext,
			ALC_INVALID_DEVICE  => Error::InvalidDevice,
			ALC_INVALID_ENUM    => Error::InvalidEnum,
			ALC_INVALID_VALUE   => Error::InvalidValue,
			ALC_OUT_OF_MEMORY   => Error::OutOfMemory,

			_ => Error::None,
		}
	}
}

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

impl fmt::Debug for Error {
	fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
		try!(f.write_str("openal::Error("));
		try!(fmt::Display::fmt(self, f));
		f.write_str(")")
	}
}

impl error::Error for Error {
	fn description(&self) -> &str {
		match self {
			&Error::None =>
				"There is no current error.",

			&Error::InvalidDevice =>
				"The device handle or specifier names an inaccessible driver/server.",

			&Error::InvalidContext =>
				"The Context argument does not name a valid context.",

			&Error::InvalidEnum =>
				"Invalid parameter.",

			&Error::InvalidName =>
				"Invalid name parameter.",

			&Error::InvalidValue =>
				"Invalid enum parameter value.",

			&Error::InvalidOperation =>
				"Illegal call.",

			&Error::OutOfMemory =>
				"Unable to allocate memory.",
		}
	}
}