use gtype::GType;
use types::gint;
use std::error::Error as ErrorTrait;
use std::fmt;
pub trait IntrospectedEnum : Sized {
fn from_int(v: gint) -> Result<Self, UnknownValue>;
fn to_int(&self) -> gint;
fn name(&self) -> &'static str;
}
pub trait EnumType : IntrospectedEnum {
fn get_type() -> GType;
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct UnknownValue(pub gint);
impl ErrorTrait for UnknownValue {
fn description(&self) -> &str {
"unknown enumeration value"
}
}
impl fmt::Display for UnknownValue {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "unknown enumeration value {}", self.0)
}
}
pub fn from_int<E>(v: gint) -> Result<E, UnknownValue>
where E: IntrospectedEnum
{
IntrospectedEnum::from_int(v)
}
pub fn type_of<E>() -> GType where E: EnumType {
<E as EnumType>::get_type()
}