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