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
use crate::{InputType, InputValueError, InputValueResult, Name, Value};
pub struct EnumItem<T> {
pub name: &'static str,
pub value: T,
}
pub trait EnumType: Sized + Eq + Send + Copy + Sized + 'static {
fn items() -> &'static [EnumItem<Self>];
}
pub fn parse_enum<T: EnumType + InputType>(value: Value) -> InputValueResult<T> {
let value = match &value {
Value::Enum(s) => s,
Value::String(s) => s.as_str(),
_ => return Err(InputValueError::expected_type(value)),
};
T::items()
.iter()
.find(|item| item.name == value)
.map(|item| item.value)
.ok_or_else(|| {
InputValueError::custom(format_args!(
r#"Enumeration type does not contain value "{}"."#,
value,
))
})
}
pub fn enum_value<T: EnumType>(value: T) -> Value {
let item = T::items().iter().find(|item| item.value == value).unwrap();
Value::Enum(Name::new(item.name))
}