Skip to main content

plugx_input/
impls.rs

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