plugx_input/
input_from_impls.rs

1use crate::Input;
2use std::collections::HashMap;
3
4impl From<&Input> for Input {
5    fn from(value: &Input) -> Self {
6        value.clone()
7    }
8}
9
10impl From<bool> for Input {
11    fn from(value: bool) -> Self {
12        Self::Bool(value)
13    }
14}
15
16impl From<Option<bool>> for Input {
17    fn from(value: Option<bool>) -> Self {
18        Self::Bool(value.unwrap_or_default())
19    }
20}
21
22impl From<isize> for Input {
23    fn from(value: isize) -> Self {
24        Self::Int(value)
25    }
26}
27
28impl From<Option<isize>> for Input {
29    fn from(value: Option<isize>) -> Self {
30        Self::Int(value.unwrap_or_default())
31    }
32}
33
34impl From<i8> for Input {
35    fn from(value: i8) -> Self {
36        Self::Int(value as isize)
37    }
38}
39
40impl From<i16> for Input {
41    fn from(value: i16) -> Self {
42        Self::Int(value as isize)
43    }
44}
45
46impl From<i32> for Input {
47    fn from(value: i32) -> Self {
48        Self::Int(value as isize)
49    }
50}
51
52impl From<u8> for Input {
53    fn from(value: u8) -> Self {
54        Self::Int(value as isize)
55    }
56}
57
58impl From<u16> for Input {
59    fn from(value: u16) -> Self {
60        Self::Int(value as isize)
61    }
62}
63
64impl From<u32> for Input {
65    fn from(value: u32) -> Self {
66        Self::Int(value as isize)
67    }
68}
69
70impl From<f64> for Input {
71    fn from(value: f64) -> Self {
72        Self::Float(value)
73    }
74}
75
76impl From<Option<f64>> for Input {
77    fn from(value: Option<f64>) -> Self {
78        Self::Float(value.unwrap_or_default())
79    }
80}
81
82impl From<f32> for Input {
83    fn from(value: f32) -> Self {
84        Self::Float(value as f64)
85    }
86}
87
88impl From<&str> for Input {
89    fn from(value: &str) -> Self {
90        Self::Str(value.to_string())
91    }
92}
93
94impl From<Option<&str>> for Input {
95    fn from(value: Option<&str>) -> Self {
96        Self::Str(value.unwrap_or_default().to_string())
97    }
98}
99
100impl From<String> for Input {
101    fn from(value: String) -> Self {
102        Self::Str(value)
103    }
104}
105
106impl From<Option<String>> for Input {
107    fn from(value: Option<String>) -> Self {
108        Self::Str(value.unwrap_or_default())
109    }
110}
111
112impl From<&String> for Input {
113    fn from(value: &String) -> Self {
114        Self::Str(value.to_string())
115    }
116}
117
118impl From<Option<&String>> for Input {
119    fn from(value: Option<&String>) -> Self {
120        Self::Str(value.cloned().unwrap_or_default())
121    }
122}
123
124impl<T: Into<Input>> From<Vec<T>> for Input {
125    fn from(value: Vec<T>) -> Self {
126        Self::List(value.into_iter().map(|i| i.into()).collect())
127    }
128}
129
130impl<T: Into<Input>> From<Option<Vec<T>>> for Input {
131    fn from(value: Option<Vec<T>>) -> Self {
132        Self::List(
133            value
134                .unwrap_or_default()
135                .into_iter()
136                .map(|i| i.into())
137                .collect(),
138        )
139    }
140}
141
142impl<T: Into<Input>, const SIZE: usize> From<[T; SIZE]> for Input {
143    fn from(value: [T; SIZE]) -> Self {
144        Self::List(value.into_iter().map(|i| i.into()).collect())
145    }
146}
147
148impl<T: Into<Input> + Clone> From<&[T]> for Input {
149    fn from(value: &[T]) -> Self {
150        Self::List(value.iter().cloned().map(|i| i.into()).collect())
151    }
152}
153
154impl<T: Into<Input> + Clone> From<Option<&[T]>> for Input {
155    fn from(value: Option<&[T]>) -> Self {
156        Self::List(
157            value
158                .unwrap_or_default()
159                .iter()
160                .cloned()
161                .map(|i| i.into())
162                .collect(),
163        )
164    }
165}
166
167impl<I: Into<Input>> FromIterator<I> for Input {
168    fn from_iter<T: IntoIterator<Item = I>>(iter: T) -> Self {
169        Self::List(iter.into_iter().map(|i| i.into()).collect())
170    }
171}
172
173impl<K: Into<String>, V: Into<Input>> From<HashMap<K, V>> for Input {
174    fn from(value: HashMap<K, V>) -> Self {
175        Self::Map(
176            value
177                .into_iter()
178                .map(|(k, v)| (k.into(), v.into()))
179                .collect(),
180        )
181    }
182}
183
184impl<K: Into<String>, V: Into<Input>> From<Option<HashMap<K, V>>> for Input {
185    fn from(value: Option<HashMap<K, V>>) -> Self {
186        Self::Map(
187            value
188                .unwrap_or_default()
189                .into_iter()
190                .map(|(k, v)| (k.into(), v.into()))
191                .collect(),
192        )
193    }
194}
195
196impl<K: Into<String>, V: Into<Input>> FromIterator<(K, V)> for Input {
197    fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self {
198        Self::Map(
199            iter.into_iter()
200                .map(|(k, v)| (k.into(), v.into()))
201                .collect(),
202        )
203    }
204}