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

use mysql::Value;

pub use mysql; // Reexport for derive produced code

#[derive(Debug)]
pub struct EnumIr<T> {
	pub string: String,
	pub value: T,
}

pub use mysql_enum_derive::MysqlEnum;

pub fn convert_enum<T: std::str::FromStr>(v: Value) -> Result<EnumIr<T>, mysql::FromValueError> {
	match v {
		Value::Bytes(bytes) => match String::from_utf8(bytes) {
			Ok(string) => match string.parse() {
				Ok(value) => Ok(EnumIr { value, string }),
				Err(_) => Err(mysql::FromValueError(Value::Bytes(string.into_bytes()))),
			},
			Err(e) => Err(mysql::FromValueError(Value::Bytes(e.into_bytes()))),
		},
		v => Err(mysql::FromValueError(v)),
	}
}