async_openai/types/
impls.rs

1use std::path::{Path, PathBuf};
2
3use crate::types::{
4    audio::AudioInput,
5    chat::{Prompt, StopConfiguration},
6    embeddings::EmbeddingInput,
7    files::FileInput,
8    images::ImageInput,
9    moderations::ModerationInput,
10    InputSource,
11};
12
13use bytes::Bytes;
14
15/// for `impl_from!(T, Enum)`, implements
16/// - `From<T>`
17/// - `From<Vec<T>>`
18/// - `From<&Vec<T>>`
19/// - `From<[T; N]>`
20/// - `From<&[T; N]>`
21///
22/// for `T: Into<String>` and `Enum` having variants `String(String)` and `StringArray(Vec<String>)`
23macro_rules! impl_from {
24    ($from_typ:ty, $to_typ:ty) => {
25        // From<T> -> String variant
26        impl From<$from_typ> for $to_typ {
27            fn from(value: $from_typ) -> Self {
28                <$to_typ>::String(value.into())
29            }
30        }
31
32        // From<Vec<T>> -> StringArray variant
33        impl From<Vec<$from_typ>> for $to_typ {
34            fn from(value: Vec<$from_typ>) -> Self {
35                <$to_typ>::StringArray(value.iter().map(|v| v.to_string()).collect())
36            }
37        }
38
39        // From<&Vec<T>> -> StringArray variant
40        impl From<&Vec<$from_typ>> for $to_typ {
41            fn from(value: &Vec<$from_typ>) -> Self {
42                <$to_typ>::StringArray(value.iter().map(|v| v.to_string()).collect())
43            }
44        }
45
46        // From<[T; N]> -> StringArray variant
47        impl<const N: usize> From<[$from_typ; N]> for $to_typ {
48            fn from(value: [$from_typ; N]) -> Self {
49                <$to_typ>::StringArray(value.into_iter().map(|v| v.to_string()).collect())
50            }
51        }
52
53        // From<&[T; N]> -> StringArray variatn
54        impl<const N: usize> From<&[$from_typ; N]> for $to_typ {
55            fn from(value: &[$from_typ; N]) -> Self {
56                <$to_typ>::StringArray(value.into_iter().map(|v| v.to_string()).collect())
57            }
58        }
59    };
60}
61
62// From String "family" to Prompt
63impl_from!(&str, Prompt);
64impl_from!(String, Prompt);
65impl_from!(&String, Prompt);
66
67// From String "family" to StopConfiguration
68impl_from!(&str, StopConfiguration);
69impl_from!(String, StopConfiguration);
70impl_from!(&String, StopConfiguration);
71
72// From String "family" to ModerationInput
73impl_from!(&str, ModerationInput);
74impl_from!(String, ModerationInput);
75impl_from!(&String, ModerationInput);
76
77// From String "family" to EmbeddingInput
78impl_from!(&str, EmbeddingInput);
79impl_from!(String, EmbeddingInput);
80impl_from!(&String, EmbeddingInput);
81
82/// for `impl_default!(Enum)`, implements `Default` for `Enum` as `Enum::String("")` where `Enum` has `String` variant
83macro_rules! impl_default {
84    ($for_typ:ty) => {
85        impl Default for $for_typ {
86            fn default() -> Self {
87                Self::String("".into())
88            }
89        }
90    };
91}
92
93impl_default!(Prompt);
94impl_default!(ModerationInput);
95impl_default!(EmbeddingInput);
96
97impl Default for InputSource {
98    fn default() -> Self {
99        InputSource::Path {
100            path: PathBuf::new(),
101        }
102    }
103}
104
105/// for `impl_input!(Struct)` where
106/// ```text
107/// Struct {
108///     source: InputSource
109/// }
110/// ```
111/// implements methods `from_bytes` and `from_vec_u8`,
112/// and `From<P>` for `P: AsRef<Path>`
113macro_rules! impl_input {
114    ($for_typ:ty) => {
115        impl $for_typ {
116            pub fn from_bytes(filename: String, bytes: Bytes) -> Self {
117                Self {
118                    source: InputSource::Bytes { filename, bytes },
119                }
120            }
121
122            pub fn from_vec_u8(filename: String, vec: Vec<u8>) -> Self {
123                Self {
124                    source: InputSource::VecU8 { filename, vec },
125                }
126            }
127        }
128
129        impl<P: AsRef<Path>> From<P> for $for_typ {
130            fn from(path: P) -> Self {
131                let path_buf = path.as_ref().to_path_buf();
132                Self {
133                    source: InputSource::Path { path: path_buf },
134                }
135            }
136        }
137    };
138}
139
140impl_input!(AudioInput);
141impl_input!(FileInput);
142impl_input!(ImageInput);
143
144macro_rules! impl_from_for_integer_array {
145    ($from_typ:ty, $to_typ:ty) => {
146        impl<const N: usize> From<[$from_typ; N]> for $to_typ {
147            fn from(value: [$from_typ; N]) -> Self {
148                Self::IntegerArray(value.to_vec())
149            }
150        }
151
152        impl<const N: usize> From<&[$from_typ; N]> for $to_typ {
153            fn from(value: &[$from_typ; N]) -> Self {
154                Self::IntegerArray(value.to_vec())
155            }
156        }
157
158        impl From<Vec<$from_typ>> for $to_typ {
159            fn from(value: Vec<$from_typ>) -> Self {
160                Self::IntegerArray(value)
161            }
162        }
163
164        impl From<&Vec<$from_typ>> for $to_typ {
165            fn from(value: &Vec<$from_typ>) -> Self {
166                Self::IntegerArray(value.clone())
167            }
168        }
169    };
170}
171
172impl_from_for_integer_array!(u32, EmbeddingInput);
173impl_from_for_integer_array!(u32, Prompt);
174
175macro_rules! impl_from_for_array_of_integer_array {
176    ($from_typ:ty, $to_typ:ty) => {
177        impl From<Vec<Vec<$from_typ>>> for $to_typ {
178            fn from(value: Vec<Vec<$from_typ>>) -> Self {
179                Self::ArrayOfIntegerArray(value)
180            }
181        }
182
183        impl From<&Vec<Vec<$from_typ>>> for $to_typ {
184            fn from(value: &Vec<Vec<$from_typ>>) -> Self {
185                Self::ArrayOfIntegerArray(value.clone())
186            }
187        }
188
189        impl<const M: usize, const N: usize> From<[[$from_typ; N]; M]> for $to_typ {
190            fn from(value: [[$from_typ; N]; M]) -> Self {
191                Self::ArrayOfIntegerArray(value.iter().map(|inner| inner.to_vec()).collect())
192            }
193        }
194
195        impl<const M: usize, const N: usize> From<[&[$from_typ; N]; M]> for $to_typ {
196            fn from(value: [&[$from_typ; N]; M]) -> Self {
197                Self::ArrayOfIntegerArray(value.iter().map(|inner| inner.to_vec()).collect())
198            }
199        }
200
201        impl<const M: usize, const N: usize> From<&[[$from_typ; N]; M]> for $to_typ {
202            fn from(value: &[[$from_typ; N]; M]) -> Self {
203                Self::ArrayOfIntegerArray(value.iter().map(|inner| inner.to_vec()).collect())
204            }
205        }
206
207        impl<const M: usize, const N: usize> From<&[&[$from_typ; N]; M]> for $to_typ {
208            fn from(value: &[&[$from_typ; N]; M]) -> Self {
209                Self::ArrayOfIntegerArray(value.iter().map(|inner| inner.to_vec()).collect())
210            }
211        }
212
213        impl<const N: usize> From<[Vec<$from_typ>; N]> for $to_typ {
214            fn from(value: [Vec<$from_typ>; N]) -> Self {
215                Self::ArrayOfIntegerArray(value.to_vec())
216            }
217        }
218
219        impl<const N: usize> From<&[Vec<$from_typ>; N]> for $to_typ {
220            fn from(value: &[Vec<$from_typ>; N]) -> Self {
221                Self::ArrayOfIntegerArray(value.to_vec())
222            }
223        }
224
225        impl<const N: usize> From<[&Vec<$from_typ>; N]> for $to_typ {
226            fn from(value: [&Vec<$from_typ>; N]) -> Self {
227                Self::ArrayOfIntegerArray(value.into_iter().map(|inner| inner.clone()).collect())
228            }
229        }
230
231        impl<const N: usize> From<&[&Vec<$from_typ>; N]> for $to_typ {
232            fn from(value: &[&Vec<$from_typ>; N]) -> Self {
233                Self::ArrayOfIntegerArray(
234                    value
235                        .to_vec()
236                        .into_iter()
237                        .map(|inner| inner.clone())
238                        .collect(),
239                )
240            }
241        }
242
243        impl<const N: usize> From<Vec<[$from_typ; N]>> for $to_typ {
244            fn from(value: Vec<[$from_typ; N]>) -> Self {
245                Self::ArrayOfIntegerArray(value.into_iter().map(|inner| inner.to_vec()).collect())
246            }
247        }
248
249        impl<const N: usize> From<&Vec<[$from_typ; N]>> for $to_typ {
250            fn from(value: &Vec<[$from_typ; N]>) -> Self {
251                Self::ArrayOfIntegerArray(value.into_iter().map(|inner| inner.to_vec()).collect())
252            }
253        }
254
255        impl<const N: usize> From<Vec<&[$from_typ; N]>> for $to_typ {
256            fn from(value: Vec<&[$from_typ; N]>) -> Self {
257                Self::ArrayOfIntegerArray(value.into_iter().map(|inner| inner.to_vec()).collect())
258            }
259        }
260
261        impl<const N: usize> From<&Vec<&[$from_typ; N]>> for $to_typ {
262            fn from(value: &Vec<&[$from_typ; N]>) -> Self {
263                Self::ArrayOfIntegerArray(value.into_iter().map(|inner| inner.to_vec()).collect())
264            }
265        }
266    };
267}
268
269impl_from_for_array_of_integer_array!(u32, EmbeddingInput);
270impl_from_for_array_of_integer_array!(u32, Prompt);