disint_model/
option_value.rs1#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
2#[serde(untagged)]
3pub enum OptionValue {
4 String(String),
5 Int(i32),
6}
7
8impl std::fmt::Display for OptionValue {
9 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
10 match self {
11 OptionValue::String(v) => v.fmt(f),
12 OptionValue::Int(v) => v.fmt(f),
13 }
14 }
15}
16
17impl PartialEq<str> for OptionValue {
18 fn eq(&self, other: &str) -> bool {
19 match self {
20 OptionValue::String(v) => v == other,
21 _ => false,
22 }
23 }
24}
25
26impl PartialEq<OptionValue> for str {
27 fn eq(&self, other: &OptionValue) -> bool {
28 match other {
29 OptionValue::String(v) => v == self,
30 _ => false,
31 }
32 }
33}
34
35impl PartialEq<i32> for OptionValue {
36 fn eq(&self, other: &i32) -> bool {
37 match self {
38 OptionValue::Int(v) => v == other,
39 _ => false,
40 }
41 }
42}
43
44impl PartialEq<OptionValue> for i32 {
45 fn eq(&self, other: &OptionValue) -> bool {
46 match other {
47 OptionValue::Int(v) => v == self,
48 _ => false,
49 }
50 }
51}
52
53impl OptionValue {
54 pub fn try_into_string(self) -> Result<String, Self> {
55 match self {
56 OptionValue::String(v) => Ok(v),
57 other => Err(other),
58 }
59 }
60
61 pub fn try_into_int(self) -> Result<i32, Self> {
62 match self {
63 OptionValue::Int(v) => Ok(v),
64 other => Err(other),
65 }
66 }
67
68 pub fn into_string(self) -> String {
69 self.try_into_string().expect("given OptionValue is not String")
70 }
71
72 pub fn into_int(self) -> i32 {
73 self.try_into_int().expect("given OptionValue is not Int")
74 }
75
76 pub fn try_as_str(&self) -> Option<&str> {
77 match self {
78 OptionValue::String(v) => Some(v),
79 _ => None,
80 }
81 }
82
83 pub fn try_as_int(&self) -> Option<i32> {
84 match self {
85 OptionValue::Int(v) => Some(*v),
86 _ => None,
87 }
88 }
89
90 pub fn as_str(&self) -> &str {
91 self.try_as_str().expect("given OptionValue is not String")
92 }
93}