xacli_core/application/
value.rs

1use crate::Error;
2
3#[derive(Debug, Clone, PartialEq)]
4pub enum InputValue {
5    Int(i64),
6    Float(f64),
7    Bool(bool),
8    String(String),
9    Array(Vec<Box<InputValue>>),
10}
11
12impl TryFrom<InputValue> for String {
13    type Error = Error;
14
15    fn try_from(value: InputValue) -> std::result::Result<Self, Error> {
16        Ok(match value {
17            InputValue::String(s) => s,
18            _ => {
19                return Err(Error::TypeError(format!(
20                    "Cannot convert to String, got {:?}",
21                    value
22                )))
23            }
24        })
25    }
26}
27
28impl TryFrom<InputValue> for bool {
29    type Error = Error;
30
31    fn try_from(value: InputValue) -> std::result::Result<Self, Error> {
32        Ok(match value {
33            InputValue::Bool(b) => b,
34            _ => {
35                return Err(Error::TypeError(format!(
36                    "Cannot convert to bool, got {:?}",
37                    value
38                )))
39            }
40        })
41    }
42}
43
44impl TryFrom<InputValue> for i64 {
45    type Error = Error;
46
47    fn try_from(value: InputValue) -> std::result::Result<Self, Error> {
48        match value {
49            InputValue::Int(i) => Ok(i),
50            _ => Err(Error::TypeError(format!(
51                "Cannot convert to i64, got {:?}",
52                value
53            ))),
54        }
55    }
56}
57
58impl TryFrom<InputValue> for f64 {
59    type Error = Error;
60
61    fn try_from(value: InputValue) -> std::result::Result<Self, Error> {
62        match value {
63            InputValue::Float(f) => Ok(f),
64            _ => Err(Error::TypeError(format!(
65                "Cannot convert to f64, got {:?}",
66                value
67            ))),
68        }
69    }
70}
71
72impl TryFrom<InputValue> for Vec<String> {
73    type Error = Error;
74
75    fn try_from(value: InputValue) -> std::result::Result<Self, Error> {
76        match value {
77            InputValue::Array(arr) => {
78                let mut result = Vec::new();
79                for item in arr {
80                    let s: String = (*item).try_into()?;
81                    result.push(s);
82                }
83                Ok(result)
84            }
85            _ => Err(Error::TypeError(format!(
86                "Cannot convert to Vec<String>, got {:?}",
87                value
88            ))),
89        }
90    }
91}
92
93impl TryFrom<InputValue> for Vec<i64> {
94    type Error = Error;
95
96    fn try_from(value: InputValue) -> std::result::Result<Self, Error> {
97        match value {
98            InputValue::Array(arr) => {
99                let mut result = Vec::new();
100                for item in arr {
101                    let i: i64 = (*item).try_into()?;
102                    result.push(i);
103                }
104                Ok(result)
105            }
106            _ => Err(Error::TypeError(format!(
107                "Cannot convert to Vec<i64>, got {:?}",
108                value
109            ))),
110        }
111    }
112}
113
114impl TryFrom<InputValue> for Vec<f64> {
115    type Error = Error;
116
117    fn try_from(value: InputValue) -> std::result::Result<Self, Error> {
118        match value {
119            InputValue::Array(arr) => {
120                let mut result = Vec::new();
121                for item in arr {
122                    let f: f64 = (*item).try_into()?;
123                    result.push(f);
124                }
125                Ok(result)
126            }
127            _ => Err(Error::TypeError(format!(
128                "Cannot convert to Vec<f64>, got {:?}",
129                value
130            ))),
131        }
132    }
133}
134
135impl TryFrom<InputValue> for Vec<bool> {
136    type Error = Error;
137
138    fn try_from(value: InputValue) -> std::result::Result<Self, Error> {
139        match value {
140            InputValue::Array(arr) => {
141                let mut result = Vec::new();
142                for item in arr {
143                    let b: bool = (*item).try_into()?;
144                    result.push(b);
145                }
146                Ok(result)
147            }
148            _ => Err(Error::TypeError(format!(
149                "Cannot convert to Vec<bool>, got {:?}",
150                value
151            ))),
152        }
153    }
154}