async_openai/types/
impls.rs1#[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#[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 impl From<$from_typ> for $to_typ {
38 fn from(value: $from_typ) -> Self {
39 <$to_typ>::String(value.into())
40 }
41 }
42
43 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 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 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 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#[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#[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#[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#[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#[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#[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);