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
//! Methods to convert to or from FFI types that don't have an easy
//! one to one conversion

use std::error::Error;
use std::fmt::{Formatter, Display};
use std::fmt;

use types::Boolean;

/// Extract bool from a Boolean
pub fn from_boolean(x: Boolean) -> Result<bool, ConversionError> {
	match x {
		0 => Ok(false),
		1 => Ok(true),
		_ => Err(ConversionError::Boolean),
	}
}

/// Possible errors when converting to Rust Types from FFI
/// or to FFI from Rust
#[derive(Debug, Copy, Clone)]
pub enum ConversionError {
	/// Thrown if the conversion is on a number that is not 0 or 1.
	/// Since 1 is being used as true and 0 as false any other number
	/// is considered invalid.
	Boolean
}

/// Display implementation for ConversionErrors
impl Display for ConversionError {
	fn fmt(&self, f: &mut Formatter) -> fmt::Result {
		write!(f, "{}", self)
	}
}

/// Error implementation for ConversionErrors
impl Error for ConversionError {
	fn description(&self) -> &str {
		use convert::ConversionError::*;
		match *self {
			Boolean => "Failed to convert to or from a Boolean"
		}
	}
}