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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(untagged)]
pub enum OptionValue {
    String(String),
    Int(i32),
}

impl std::fmt::Display for OptionValue {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            OptionValue::String(v) => v.fmt(f),
            OptionValue::Int(v) => v.fmt(f),
        }
    }
}

impl PartialEq<str> for OptionValue {
    fn eq(&self, other: &str) -> bool {
        match self {
            OptionValue::String(v) => v == other,
            _ => false,
        }
    }
}

impl PartialEq<OptionValue> for str {
    fn eq(&self, other: &OptionValue) -> bool {
        match other {
            OptionValue::String(v) => v == self,
            _ => false,
        }
    }
}

impl PartialEq<i32> for OptionValue {
    fn eq(&self, other: &i32) -> bool {
        match self {
            OptionValue::Int(v) => v == other,
            _ => false,
        }
    }
}

impl PartialEq<OptionValue> for i32 {
    fn eq(&self, other: &OptionValue) -> bool {
        match other {
            OptionValue::Int(v) => v == self,
            _ => false,
        }
    }
}

impl OptionValue {
    pub fn try_into_string(self) -> Result<String, Self> {
        match self {
            OptionValue::String(v) => Ok(v),
            other => Err(other),
        }
    }

    pub fn try_into_int(self) -> Result<i32, Self> {
        match self {
            OptionValue::Int(v) => Ok(v),
            other => Err(other),
        }
    }

    pub fn into_string(self) -> String {
        self.try_into_string().expect("given OptionValue is not String")
    }

    pub fn into_int(self) -> i32 {
        self.try_into_int().expect("given OptionValue is not Int")
    }

    pub fn try_as_str(&self) -> Option<&str> {
        match self {
            OptionValue::String(v) => Some(v),
            _ => None,
        }
    }

    pub fn try_as_int(&self) -> Option<i32> {
        match self {
            OptionValue::Int(v) => Some(*v),
            _ => None,
        }
    }

    pub fn as_str(&self) -> &str {
        self.try_as_str().expect("given OptionValue is not String")
    }
}