async_openai_wasm/types/
impls.rs

1#[cfg(any(
2    feature = "audio-types",
3    feature = "file-types",
4    feature = "image-types",
5    feature = "video-types"
6))]
7use crate::types::InputSource;
8#[cfg(feature = "audio-types")]
9use crate::types::audio::AudioInput;
10#[cfg(any(feature = "chat-completion-types", feature = "completion-types"))]
11use crate::types::chat::{Prompt, StopConfiguration};
12#[cfg(feature = "embedding-types")]
13use crate::types::embeddings::EmbeddingInput;
14#[cfg(feature = "file-types")]
15use crate::types::files::FileInput;
16#[cfg(feature = "moderation-types")]
17use crate::types::moderations::ModerationInput;
18#[cfg(any(feature = "image-types", feature = "video-types"))]
19use crate::types::shared::ImageInput;
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 variant
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}
161
162#[cfg(feature = "audio-types")]
163impl_input!(AudioInput);
164#[cfg(feature = "file-types")]
165impl_input!(FileInput);
166#[cfg(any(feature = "image-types", feature = "video-types"))]
167impl_input!(ImageInput);
168
169#[cfg(any(
170    feature = "chat-completion-types",
171    feature = "completion-types",
172    feature = "embedding-types"
173))]
174macro_rules! impl_from_for_integer_array {
175    ($from_typ:ty, $to_typ:ty) => {
176        impl<const N: usize> From<[$from_typ; N]> for $to_typ {
177            fn from(value: [$from_typ; N]) -> Self {
178                Self::IntegerArray(value.to_vec())
179            }
180        }
181
182        impl<const N: usize> From<&[$from_typ; N]> for $to_typ {
183            fn from(value: &[$from_typ; N]) -> Self {
184                Self::IntegerArray(value.to_vec())
185            }
186        }
187
188        impl From<Vec<$from_typ>> for $to_typ {
189            fn from(value: Vec<$from_typ>) -> Self {
190                Self::IntegerArray(value)
191            }
192        }
193
194        impl From<&Vec<$from_typ>> for $to_typ {
195            fn from(value: &Vec<$from_typ>) -> Self {
196                Self::IntegerArray(value.clone())
197            }
198        }
199    };
200}
201
202#[cfg(feature = "embedding-types")]
203impl_from_for_integer_array!(u32, EmbeddingInput);
204#[cfg(any(feature = "chat-completion-types", feature = "completion-types"))]
205impl_from_for_integer_array!(u32, Prompt);
206
207#[cfg(any(
208    feature = "chat-completion-types",
209    feature = "completion-types",
210    feature = "embedding-types"
211))]
212macro_rules! impl_from_for_array_of_integer_array {
213    ($from_typ:ty, $to_typ:ty) => {
214        impl From<Vec<Vec<$from_typ>>> for $to_typ {
215            fn from(value: Vec<Vec<$from_typ>>) -> Self {
216                Self::ArrayOfIntegerArray(value)
217            }
218        }
219
220        impl From<&Vec<Vec<$from_typ>>> for $to_typ {
221            fn from(value: &Vec<Vec<$from_typ>>) -> Self {
222                Self::ArrayOfIntegerArray(value.clone())
223            }
224        }
225
226        impl<const M: usize, const N: usize> From<[[$from_typ; N]; M]> for $to_typ {
227            fn from(value: [[$from_typ; N]; M]) -> Self {
228                Self::ArrayOfIntegerArray(value.iter().map(|inner| inner.to_vec()).collect())
229            }
230        }
231
232        impl<const M: usize, const N: usize> From<[&[$from_typ; N]; M]> for $to_typ {
233            fn from(value: [&[$from_typ; N]; M]) -> Self {
234                Self::ArrayOfIntegerArray(value.iter().map(|inner| inner.to_vec()).collect())
235            }
236        }
237
238        impl<const M: usize, const N: usize> From<&[[$from_typ; N]; M]> for $to_typ {
239            fn from(value: &[[$from_typ; N]; M]) -> Self {
240                Self::ArrayOfIntegerArray(value.iter().map(|inner| inner.to_vec()).collect())
241            }
242        }
243
244        impl<const M: usize, const N: usize> From<&[&[$from_typ; N]; M]> for $to_typ {
245            fn from(value: &[&[$from_typ; N]; M]) -> Self {
246                Self::ArrayOfIntegerArray(value.iter().map(|inner| inner.to_vec()).collect())
247            }
248        }
249
250        impl<const N: usize> From<[Vec<$from_typ>; N]> for $to_typ {
251            fn from(value: [Vec<$from_typ>; N]) -> Self {
252                Self::ArrayOfIntegerArray(value.to_vec())
253            }
254        }
255
256        impl<const N: usize> From<&[Vec<$from_typ>; N]> for $to_typ {
257            fn from(value: &[Vec<$from_typ>; N]) -> Self {
258                Self::ArrayOfIntegerArray(value.to_vec())
259            }
260        }
261
262        impl<const N: usize> From<[&Vec<$from_typ>; N]> for $to_typ {
263            fn from(value: [&Vec<$from_typ>; N]) -> Self {
264                Self::ArrayOfIntegerArray(value.into_iter().map(|inner| inner.clone()).collect())
265            }
266        }
267
268        impl<const N: usize> From<&[&Vec<$from_typ>; N]> for $to_typ {
269            fn from(value: &[&Vec<$from_typ>; N]) -> Self {
270                Self::ArrayOfIntegerArray(
271                    value
272                        .to_vec()
273                        .into_iter()
274                        .map(|inner| inner.clone())
275                        .collect(),
276                )
277            }
278        }
279
280        impl<const N: usize> From<Vec<[$from_typ; N]>> for $to_typ {
281            fn from(value: Vec<[$from_typ; N]>) -> Self {
282                Self::ArrayOfIntegerArray(value.into_iter().map(|inner| inner.to_vec()).collect())
283            }
284        }
285
286        impl<const N: usize> From<&Vec<[$from_typ; N]>> for $to_typ {
287            fn from(value: &Vec<[$from_typ; N]>) -> Self {
288                Self::ArrayOfIntegerArray(value.into_iter().map(|inner| inner.to_vec()).collect())
289            }
290        }
291
292        impl<const N: usize> From<Vec<&[$from_typ; N]>> for $to_typ {
293            fn from(value: Vec<&[$from_typ; N]>) -> Self {
294                Self::ArrayOfIntegerArray(value.into_iter().map(|inner| inner.to_vec()).collect())
295            }
296        }
297
298        impl<const N: usize> From<&Vec<&[$from_typ; N]>> for $to_typ {
299            fn from(value: &Vec<&[$from_typ; N]>) -> Self {
300                Self::ArrayOfIntegerArray(value.into_iter().map(|inner| inner.to_vec()).collect())
301            }
302        }
303    };
304}
305
306#[cfg(feature = "embedding-types")]
307impl_from_for_array_of_integer_array!(u32, EmbeddingInput);
308#[cfg(any(feature = "chat-completion-types", feature = "completion-types"))]
309impl_from_for_array_of_integer_array!(u32, Prompt);