async_openai/types/
impls.rs

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