teo_runtime/value/convert/into/
range.rs

1use teo_result::Error;
2use crate::value::range::Range;
3use crate::value::Value;
4
5impl TryFrom<Value> for Range {
6    type Error = Error;
7
8    fn try_from(value: Value) -> Result<Self, Self::Error> {
9        match value {
10            Value::Range(s) => Ok(s),
11            _ => Err(Error::new(format!("Cannot convert {} into Range", value.type_hint()))),
12        }
13    }
14}
15
16impl TryFrom<&Value> for Range {
17    type Error = Error;
18
19    fn try_from(value: &Value) -> Result<Self, Self::Error> {
20        match value {
21            Value::Range(s) => Ok(s.clone()),
22            _ => Err(Error::new(format!("Cannot convert {} into Range", value.type_hint()))),
23        }
24    }
25}
26
27impl<'a> TryFrom<&'a Value> for &'a Range {
28    type Error = Error;
29
30    fn try_from(value: &'a Value) -> Result<Self, Self::Error> {
31        match value {
32            Value::Range(s) => Ok(s),
33            _ => Err(Error::new(format!("Cannot convert {} into &Range", value.type_hint()))),
34        }
35    }
36}
37
38impl TryFrom<Value> for Option<Range> {
39    type Error = Error;
40
41    fn try_from(value: Value) -> Result<Self, Self::Error> {
42        match value {
43            Value::Null => Ok(None),
44            Value::Range(s) => Ok(Some(s)),
45            _ => Err(Error::new(format!("Cannot convert {} into Option<Range>", value.type_hint()))),
46        }
47    }
48}
49
50impl TryFrom<&Value> for Option<Range> {
51    type Error = Error;
52
53    fn try_from(value: &Value) -> Result<Self, Self::Error> {
54        value.clone().try_into()
55    }
56}
57
58impl<'a> TryFrom<&'a Value> for Option<&'a Range> {
59    type Error = Error;
60
61    fn try_from(value: &'a Value) -> Result<Self, Self::Error> {
62        match value {
63            Value::Null => Ok(None),
64            Value::Range(s) => Ok(Some(s)),
65            _ => Err(Error::new(format!("Cannot convert {} into Option<&Range>", value.type_hint()))),
66        }
67    }
68}