tf_bindgen/value/
prelude.rs

1use std::collections::{HashMap, HashSet};
2use std::hash::Hash;
3use std::rc::Rc;
4
5use super::Cell;
6pub use super::Value;
7
8pub trait IntoValue<T> {
9    fn into_value(self) -> Value<T>;
10}
11
12pub trait IntoValueList<T> {
13    fn into_value_list(self) -> Vec<Value<T>>;
14}
15
16pub trait IntoValueSet<T> {
17    fn into_value_set(self) -> HashSet<Value<T>>;
18}
19
20pub trait IntoValueMap<T> {
21    fn into_value_map(self) -> HashMap<String, Value<T>>;
22}
23
24macro_rules! impl_into_value {
25    ($t:ty) => {
26        impl IntoValue<$t> for $t {
27            fn into_value(self) -> Value<$t> {
28                Value::Value {
29                    value: Rc::new(self.into()),
30                }
31            }
32        }
33        impl IntoValue<$t> for &$t {
34            fn into_value(self) -> Value<$t> {
35                Value::Value {
36                    value: Rc::new(self.clone()),
37                }
38            }
39        }
40    };
41}
42
43impl_into_value!(bool);
44impl_into_value!(i64);
45impl_into_value!(String);
46impl_into_value!(serde_json::value::Value);
47
48impl IntoValue<String> for &str {
49    fn into_value(self) -> Value<String> {
50        self.to_string().into_value()
51    }
52}
53
54impl<T> IntoValue<T> for Value<T> {
55    fn into_value(self) -> Value<T> {
56        self
57    }
58}
59
60impl<T: Clone> IntoValue<T> for &Value<T> {
61    fn into_value(self) -> Value<T> {
62        self.clone()
63    }
64}
65
66impl<T: Clone> IntoValue<T> for &Cell<Value<T>> {
67    fn into_value(self) -> Value<T> {
68        Value::Ref {
69            path: self.path().to_string(),
70            value: Some(Box::new(self.value().clone())),
71        }
72    }
73}
74
75impl<T: Clone> IntoValue<T> for &Cell<Option<Value<T>>> {
76    fn into_value(self) -> Value<T> {
77        Value::Ref {
78            path: self.path().to_string(),
79            value: self.value().as_ref().cloned().map(Box::new),
80        }
81    }
82}
83
84impl<T, U> IntoValueList<T> for Vec<U>
85where
86    U: IntoValue<T>,
87{
88    fn into_value_list(self) -> Vec<Value<T>> {
89        self.into_iter().map(IntoValue::into_value).collect()
90    }
91}
92
93impl<T, U, const S: usize> IntoValueList<T> for [U; S]
94where
95    U: IntoValue<T>,
96{
97    fn into_value_list(self) -> Vec<Value<T>> {
98        self.into_iter().map(IntoValue::into_value).collect()
99    }
100}
101
102impl<'a, T, U> IntoValueList<T> for &'a [U]
103where
104    U: IntoValue<T> + Clone,
105{
106    fn into_value_list(self) -> Vec<Value<T>> {
107        self.iter().cloned().map(IntoValue::into_value).collect()
108    }
109}
110
111impl<T, U> IntoValueSet<T> for HashSet<U>
112where
113    T: Hash + Eq,
114    U: IntoValue<T>,
115{
116    fn into_value_set(self) -> HashSet<Value<T>> {
117        self.into_iter().map(IntoValue::into_value).collect()
118    }
119}
120
121impl<T, U, const S: usize> IntoValueSet<T> for [U; S]
122where
123    T: Hash + Eq,
124    U: IntoValue<T>,
125{
126    fn into_value_set(self) -> HashSet<Value<T>> {
127        self.into_iter().map(IntoValue::into_value).collect()
128    }
129}
130
131impl<'a, T, U> IntoValueSet<T> for &'a [U]
132where
133    T: Hash + Eq,
134    U: IntoValue<T> + Clone,
135{
136    fn into_value_set(self) -> HashSet<Value<T>> {
137        self.iter().cloned().map(IntoValue::into_value).collect()
138    }
139}
140
141impl<T, U> IntoValueMap<T> for HashMap<String, U>
142where
143    U: IntoValue<T>,
144{
145    fn into_value_map(self) -> HashMap<String, Value<T>> {
146        self.into_iter()
147            .map(|(key, value)| (key, value.into_value()))
148            .collect()
149    }
150}
151
152impl<T, U> IntoValueMap<T> for &HashMap<String, U>
153where
154    U: IntoValue<T> + Clone,
155{
156    fn into_value_map(self) -> HashMap<String, Value<T>> {
157        self.iter()
158            .map(|(key, value)| (key.clone(), value.clone()))
159            .map(|(key, value)| (key, value.into_value()))
160            .collect()
161    }
162}