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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use crate::{
error::{ Error, Result },
typing::{ AnyValue, utf8_string::Utf8String }
};
use std::convert::TryFrom;
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Enumeration {
variant: Utf8String,
value: Box<AnyValue>
}
impl Enumeration {
pub fn new<T>(variant: T) -> Self where T: ToString {
Self { variant: Utf8String::new(variant), value: Box::new(AnyValue::None) }
}
pub fn with_value<T, V>(variant: T, value: V) -> Self where T: ToString, V: Into<AnyValue> {
Self { variant: Utf8String::new(variant), value: Box::new(value.into()) }
}
pub fn variant(&self) -> &String {
&self.variant
}
pub fn variant_mut(&mut self) -> &mut String {
&mut self.variant
}
pub fn value(&self) -> &AnyValue {
&self.value
}
pub fn value_mut(&mut self) -> &mut AnyValue {
&mut self.value
}
pub fn into_inner(self) -> (Utf8String, AnyValue) {
(self.variant, *self.value)
}
}
impl From<String> for Enumeration {
fn from(value: String) -> Self {
Self::new(value)
}
}
impl From<&str> for Enumeration {
fn from(value: &str) -> Self {
Self::new(value)
}
}
impl TryFrom<AnyValue> for Enumeration {
type Error = Error;
fn try_from(value: AnyValue) -> Result<Self> {
match value {
AnyValue::Enum(value) => Ok(value),
value => Err(etype!("Cannot convert {:?} to enum", value))
}
}
}
impl From<Enumeration> for AnyValue {
fn from(value: Enumeration) -> Self {
AnyValue::Enum(value)
}
}