qubit_value/value/
value_getter.rs1use std::collections::HashMap;
14use std::time::Duration;
15
16use bigdecimal::BigDecimal;
17use chrono::{
18 DateTime,
19 NaiveDate,
20 NaiveDateTime,
21 NaiveTime,
22 Utc,
23};
24use num_bigint::BigInt;
25use url::Url;
26
27use super::value::Value;
28use crate::value_error::{
29 ValueError,
30 ValueResult,
31};
32
33macro_rules! impl_value_try_from_ref {
34 ($type:ty, $method:ident) => {
35 impl TryFrom<&Value> for $type {
36 type Error = ValueError;
37
38 #[inline]
39 fn try_from(value: &Value) -> ValueResult<$type> {
40 value.$method()
41 }
42 }
43 };
44}
45
46impl_value_try_from_ref!(bool, get_bool);
48impl_value_try_from_ref!(char, get_char);
49impl_value_try_from_ref!(i8, get_int8);
50impl_value_try_from_ref!(i16, get_int16);
51impl_value_try_from_ref!(i32, get_int32);
52impl_value_try_from_ref!(i64, get_int64);
53impl_value_try_from_ref!(i128, get_int128);
54impl_value_try_from_ref!(u8, get_uint8);
55impl_value_try_from_ref!(u16, get_uint16);
56impl_value_try_from_ref!(u32, get_uint32);
57impl_value_try_from_ref!(u64, get_uint64);
58impl_value_try_from_ref!(u128, get_uint128);
59impl_value_try_from_ref!(f32, get_float32);
60impl_value_try_from_ref!(f64, get_float64);
61impl_value_try_from_ref!(NaiveDate, get_date);
62impl_value_try_from_ref!(NaiveTime, get_time);
63impl_value_try_from_ref!(NaiveDateTime, get_datetime);
64impl_value_try_from_ref!(DateTime<Utc>, get_instant);
65impl_value_try_from_ref!(BigInt, get_biginteger);
66impl_value_try_from_ref!(BigDecimal, get_bigdecimal);
67impl_value_try_from_ref!(isize, get_intsize);
68impl_value_try_from_ref!(usize, get_uintsize);
69impl_value_try_from_ref!(Duration, get_duration);
70impl_value_try_from_ref!(Url, get_url);
71impl_value_try_from_ref!(HashMap<String, String>, get_string_map);
72impl_value_try_from_ref!(serde_json::Value, get_json);
73
74impl TryFrom<&Value> for String {
76 type Error = ValueError;
77
78 #[inline]
79 fn try_from(value: &Value) -> ValueResult<String> {
80 value.get_string().map(|s| s.to_string())
81 }
82}