Skip to content

Commit c9a3786

Browse files
authored
Merge pull request #570 from kstrohbeck/master
Implement Display + Error for error types.
2 parents 74ea280 + f630e25 commit c9a3786

File tree

5 files changed

+194
-0
lines changed

5 files changed

+194
-0
lines changed

src/sdl2/controller.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use libc::c_char;
2+
use std::error;
23
use std::ffi::{CString, CStr, NulError};
4+
use std::fmt;
35
use std::path::Path;
46
use rwops::RWops;
57

@@ -18,6 +20,30 @@ pub enum AddMappingError {
1820
SdlError(String),
1921
}
2022

23+
impl fmt::Display for AddMappingError {
24+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
25+
use self::AddMappingError::*;
26+
27+
match *self {
28+
InvalidMapping(ref e) => write!(f, "Null error: {}", e),
29+
InvalidFilePath(ref value) => write!(f, "Invalid file path ({})", value),
30+
SdlError(ref e) => write!(f, "SDL error: {}", e)
31+
}
32+
}
33+
}
34+
35+
impl error::Error for AddMappingError {
36+
fn description(&self) -> &str {
37+
use self::AddMappingError::*;
38+
39+
match *self {
40+
InvalidMapping(_) => "invalid mapping",
41+
InvalidFilePath(_) => "invalid file path",
42+
SdlError(ref e) => e,
43+
}
44+
}
45+
}
46+
2147
impl GameControllerSubsystem {
2248
/// Retreive the total number of attached joysticks *and* controllers identified by SDL.
2349
pub fn num_joysticks(&self) -> Result<u32, String> {

src/sdl2/filesystem.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
use std::error;
12
use std::ffi::{CStr, CString, NulError};
3+
use std::fmt;
24
use get_error;
35
use libc::c_char;
46

@@ -24,6 +26,30 @@ pub enum PrefPathError {
2426
SdlError(String),
2527
}
2628

29+
impl fmt::Display for PrefPathError {
30+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
31+
use self::PrefPathError::*;
32+
33+
match *self {
34+
InvalidOrganizationName(ref e) => write!(f, "Invalid organization name: {}", e),
35+
InvalidApplicationName(ref e) => write!(f, "Invalid application name: {}", e),
36+
SdlError(ref e) => write!(f, "SDL error: {}", e)
37+
}
38+
}
39+
}
40+
41+
impl error::Error for PrefPathError {
42+
fn description(&self) -> &str {
43+
use self::PrefPathError::*;
44+
45+
match *self {
46+
InvalidOrganizationName(_) => "invalid organization name",
47+
InvalidApplicationName(_) => "invalid application name",
48+
SdlError(ref e) => e,
49+
}
50+
}
51+
}
52+
2753
// TODO: Change to OsStr or something?
2854
/// Return the preferred directory for the application to write files on this
2955
/// system, based on the given organization and application name.

src/sdl2/messagebox.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
use std::error;
12
use std::ffi::{CString, NulError};
3+
use std::fmt;
24
use std::ptr;
35
use std::os::raw::{c_char,c_int};
46

@@ -81,6 +83,33 @@ pub enum ShowMessageError {
8183
SdlError(String),
8284
}
8385

86+
impl fmt::Display for ShowMessageError {
87+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
88+
use self::ShowMessageError::*;
89+
90+
match *self {
91+
InvalidTitle(ref e) => write!(f, "Invalid title: {}", e),
92+
InvalidMessage(ref e) => write!(f, "Invalid message: {}", e),
93+
InvalidButton(ref e, value) => write!(f,
94+
"Invalid button ({}): {}", value, e),
95+
SdlError(ref e) => write!(f, "SDL error: {}", e)
96+
}
97+
}
98+
}
99+
100+
impl error::Error for ShowMessageError {
101+
fn description(&self) -> &str {
102+
use self::ShowMessageError::*;
103+
104+
match *self {
105+
InvalidTitle(_) => "invalid title",
106+
InvalidMessage(_) => "invalid message",
107+
InvalidButton(..) => "invalid button",
108+
SdlError(ref e) => e
109+
}
110+
}
111+
}
112+
84113
/// Show a simple message box, meant to be informative only.
85114
///
86115
/// There is no way to know if the user clicked "Ok" or closed the message box,

src/sdl2/render.rs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,6 +1043,48 @@ pub enum UpdateTextureError {
10431043
SdlError(String),
10441044
}
10451045

1046+
impl fmt::Display for UpdateTextureError {
1047+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1048+
use self::UpdateTextureError::*;
1049+
1050+
match *self {
1051+
PitchOverflows(value) => write!(f, "Pitch overflows ({})", value),
1052+
PitchMustBeMultipleOfTwoForFormat(value, format) => write!(f,
1053+
"Pitch must be multiple of two for pixel format '{:?}' ({})",
1054+
format, value),
1055+
XMustBeMultipleOfTwoForFormat(value, format) => write!(f,
1056+
"X must be multiple of two for pixel format '{:?}' ({})",
1057+
format, value),
1058+
YMustBeMultipleOfTwoForFormat(value, format) => write!(f,
1059+
"Y must be multiple of two for pixel format '{:?}' ({})",
1060+
format, value),
1061+
WidthMustBeMultipleOfTwoForFormat(value, format) => write!(f,
1062+
"Width must be multiple of two for pixel format '{:?}' ({})",
1063+
format, value),
1064+
HeightMustBeMultipleOfTwoForFormat(value, format) => write!(f,
1065+
"Height must be multiple of two for pixel format '{:?}' ({})",
1066+
format, value),
1067+
SdlError(ref e) => write!(f, "SDL error: {}", e)
1068+
}
1069+
}
1070+
}
1071+
1072+
impl Error for UpdateTextureError {
1073+
fn description(&self) -> &str {
1074+
use self::UpdateTextureError::*;
1075+
1076+
match *self {
1077+
PitchOverflows(_) => "pitch overflow",
1078+
PitchMustBeMultipleOfTwoForFormat(..) => "pitch must be multiple of two",
1079+
XMustBeMultipleOfTwoForFormat(..) => "x must be multiple of two",
1080+
YMustBeMultipleOfTwoForFormat(..) => "y must be multiple of two",
1081+
WidthMustBeMultipleOfTwoForFormat(..) => "width must be multiple of two",
1082+
HeightMustBeMultipleOfTwoForFormat(..) => "height must be multiple of two",
1083+
SdlError(ref e) => e,
1084+
}
1085+
}
1086+
}
1087+
10461088
#[derive(Debug)]
10471089
pub enum UpdateTextureYUVError {
10481090
PitchOverflows { plane: &'static str, value: usize },
@@ -1055,6 +1097,47 @@ pub enum UpdateTextureYUVError {
10551097
SdlError(String),
10561098
}
10571099

1100+
impl fmt::Display for UpdateTextureYUVError {
1101+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1102+
use self::UpdateTextureYUVError::*;
1103+
1104+
match *self {
1105+
PitchOverflows { plane, value } => write!(f,
1106+
"Pitch overflows on {} plane ({})", plane, value),
1107+
InvalidPlaneLength { plane, length, pitch, height } => write!(f,
1108+
"The {} plane is wrong length ({}, should be {} * {})",
1109+
plane, length, pitch, height),
1110+
XMustBeMultipleOfTwoForFormat(value) => write!(f,
1111+
"X must be multiple of two ({})", value),
1112+
YMustBeMultipleOfTwoForFormat(value) => write!(f,
1113+
"Y must be multiple of two ({})", value),
1114+
WidthMustBeMultipleOfTwoForFormat(value) => write!(f,
1115+
"Width must be multiple of two ({})", value),
1116+
HeightMustBeMultipleOfTwoForFormat(value) => write!(f,
1117+
"Height must be multiple of two ({})", value),
1118+
RectNotInsideTexture(_) => write!(f, "Rect must be inside texture"),
1119+
SdlError(ref e) => write!(f, "SDL error: {}", e)
1120+
}
1121+
}
1122+
}
1123+
1124+
impl Error for UpdateTextureYUVError {
1125+
fn description(&self) -> &str {
1126+
use self::UpdateTextureYUVError::*;
1127+
1128+
match *self {
1129+
PitchOverflows {..} => "pitch overflow",
1130+
InvalidPlaneLength {..} => "invalid plane length",
1131+
XMustBeMultipleOfTwoForFormat(_) => "x must be multiple of two",
1132+
YMustBeMultipleOfTwoForFormat(_) => "y must be multiple of two",
1133+
WidthMustBeMultipleOfTwoForFormat(_) => "width must be multiple of two",
1134+
HeightMustBeMultipleOfTwoForFormat(_) => "height must be multiple of two",
1135+
RectNotInsideTexture(_) => "rect must be inside texture",
1136+
SdlError(ref e) => e,
1137+
}
1138+
}
1139+
}
1140+
10581141
impl Texture {
10591142
#[inline]
10601143
fn check_renderer(&self) {

src/sdl2/sdl.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
use std::error;
12
use std::ffi::{CStr, CString, NulError};
3+
use std::fmt;
24
use std::rc::Rc;
35
use libc::c_char;
46

@@ -13,6 +15,34 @@ pub enum Error {
1315
UnsupportedError = ll::SDL_UNSUPPORTED as isize
1416
}
1517

18+
impl fmt::Display for Error {
19+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
20+
use self::Error::*;
21+
22+
match *self {
23+
NoMemError => write!(f, "Out of memory"),
24+
ReadError => write!(f, "Error reading from datastream"),
25+
WriteError => write!(f, "Error writing to datastream"),
26+
SeekError => write!(f, "Error seeking in datastream"),
27+
UnsupportedError => write!(f, "Unknown SDL error")
28+
}
29+
}
30+
}
31+
32+
impl error::Error for Error {
33+
fn description(&self) -> &str {
34+
use self::Error::*;
35+
36+
match *self {
37+
NoMemError => "out of memory",
38+
ReadError => "error reading from datastream",
39+
WriteError => "error writing to datastream",
40+
SeekError => "error seeking in datastream",
41+
UnsupportedError => "unknown SDL error"
42+
}
43+
}
44+
}
45+
1646
use std::sync::atomic::{AtomicBool, ATOMIC_BOOL_INIT};
1747
/// Only one Sdl context can be alive at a time.
1848
/// Set to false by default (not alive).

0 commit comments

Comments
 (0)