1use std::collections::HashMap;
2use std::time::Duration;
3
4use bigdecimal::BigDecimal;
5use chrono::{DateTime, NaiveDate, NaiveDateTime, NaiveTime, Utc};
6use num_bigint::BigInt;
7use qubit_common::lang::DataType;
8use url::Url;
9
10use crate::Value;
11use crate::error::{ValueError, ValueResult};
12
13use super::multi_values::MultiValues;
14
15#[doc(hidden)]
24pub trait MultiValuesGetter<T> {
25 fn get_values(&self) -> ValueResult<Vec<T>>;
26}
27
28#[doc(hidden)]
32pub trait MultiValuesFirstGetter<T> {
33 fn get_first_value(&self) -> ValueResult<T>;
34}
35
36#[doc(hidden)]
40pub trait MultiValuesSetter<T> {
41 fn set_values(&mut self, values: Vec<T>) -> ValueResult<()>;
42}
43
44#[doc(hidden)]
46pub trait MultiValuesSetArg<'a> {
47 type Item: 'a + Clone;
49
50 fn apply(self, target: &mut MultiValues) -> ValueResult<()>;
51}
52
53#[doc(hidden)]
57pub trait MultiValuesSetterSlice<T> {
58 fn set_values_slice(&mut self, values: &[T]) -> ValueResult<()>;
59}
60
61#[doc(hidden)]
65pub trait MultiValuesSingleSetter<T> {
66 fn set_single_value(&mut self, value: T) -> ValueResult<()>;
67}
68
69#[doc(hidden)]
73pub trait MultiValuesAdder<T> {
74 fn add_value(&mut self, value: T) -> ValueResult<()>;
75}
76
77#[doc(hidden)]
81pub trait MultiValuesMultiAdder<T> {
82 fn add_values(&mut self, values: Vec<T>) -> ValueResult<()>;
83}
84
85#[doc(hidden)]
87pub trait MultiValuesAddArg<'a> {
88 type Item: 'a + Clone;
90
91 fn apply_add(self, target: &mut MultiValues) -> ValueResult<()>;
92}
93
94#[doc(hidden)]
97pub(crate) trait MultiValuesMultiAdderSlice<T> {
98 fn add_values_slice(&mut self, values: &[T]) -> ValueResult<()>;
99}
100
101#[doc(hidden)]
106pub trait MultiValuesConstructor<T> {
107 fn from_vec(values: Vec<T>) -> Self;
108}
109
110macro_rules! impl_multi_value_traits {
115 ($type:ty, $variant:ident, $data_type:expr) => {
116 impl MultiValuesGetter<$type> for MultiValues {
117 #[inline]
118 fn get_values(&self) -> ValueResult<Vec<$type>> {
119 match self {
120 MultiValues::$variant(v) => Ok(v.clone()),
121 MultiValues::Empty(dt) if *dt == $data_type => Ok(Vec::new()),
122 _ => Err(ValueError::TypeMismatch {
123 expected: $data_type,
124 actual: self.data_type(),
125 }),
126 }
127 }
128 }
129
130 impl MultiValuesFirstGetter<$type> for MultiValues {
131 #[inline]
132 fn get_first_value(&self) -> ValueResult<$type> {
133 match self {
134 MultiValues::$variant(v) if !v.is_empty() => Ok(v[0].clone()),
135 MultiValues::$variant(_) => Err(ValueError::NoValue),
136 MultiValues::Empty(dt) if *dt == $data_type => Err(ValueError::NoValue),
137 _ => Err(ValueError::TypeMismatch {
138 expected: $data_type,
139 actual: self.data_type(),
140 }),
141 }
142 }
143 }
144
145 impl MultiValuesSetter<$type> for MultiValues {
146 #[inline]
147 fn set_values(&mut self, values: Vec<$type>) -> ValueResult<()> {
148 *self = MultiValues::$variant(values);
149 Ok(())
150 }
151 }
152
153 impl MultiValuesSetterSlice<$type> for MultiValues {
157 #[inline]
158 fn set_values_slice(&mut self, values: &[$type]) -> ValueResult<()> {
159 *self = MultiValues::$variant(values.to_vec());
161 Ok(())
162 }
163 }
164
165 impl MultiValuesSingleSetter<$type> for MultiValues {
166 #[inline]
167 fn set_single_value(&mut self, value: $type) -> ValueResult<()> {
168 *self = MultiValues::$variant(vec![value]);
169 Ok(())
170 }
171 }
172
173 impl MultiValuesAdder<$type> for MultiValues {
174 #[inline]
175 fn add_value(&mut self, value: $type) -> ValueResult<()> {
176 match self {
177 MultiValues::$variant(v) => {
178 v.push(value);
179 Ok(())
180 }
181 MultiValues::Empty(dt) if *dt == $data_type => {
182 *self = MultiValues::$variant(vec![value]);
183 Ok(())
184 }
185 _ => Err(ValueError::TypeMismatch {
186 expected: $data_type,
187 actual: self.data_type(),
188 }),
189 }
190 }
191 }
192
193 impl<'a> MultiValuesSetArg<'a> for Vec<$type> {
195 type Item = $type;
196
197 #[inline]
198 fn apply(self, target: &mut MultiValues) -> ValueResult<()> {
199 <MultiValues as MultiValuesSetter<$type>>::set_values(target, self)
200 }
201 }
202
203 impl<'a> MultiValuesSetArg<'a> for &'a [$type]
204 where
205 $type: Clone,
206 {
207 type Item = $type;
208
209 #[inline]
210 fn apply(self, target: &mut MultiValues) -> ValueResult<()> {
211 <MultiValues as MultiValuesSetterSlice<$type>>::set_values_slice(target, self)
212 }
213 }
214
215 impl<'a> MultiValuesSetArg<'a> for $type {
216 type Item = $type;
217
218 #[inline]
219 fn apply(self, target: &mut MultiValues) -> ValueResult<()> {
220 <MultiValues as MultiValuesSingleSetter<$type>>::set_single_value(target, self)
221 }
222 }
223
224 impl MultiValuesMultiAdder<$type> for MultiValues {
225 #[inline]
226 fn add_values(&mut self, values: Vec<$type>) -> ValueResult<()> {
227 match self {
228 MultiValues::$variant(v) => {
229 v.extend(values);
230 Ok(())
231 }
232 MultiValues::Empty(dt) if *dt == $data_type => {
233 *self = MultiValues::$variant(values);
234 Ok(())
235 }
236 _ => Err(ValueError::TypeMismatch {
237 expected: $data_type,
238 actual: self.data_type(),
239 }),
240 }
241 }
242 }
243
244 impl MultiValuesMultiAdderSlice<$type> for MultiValues {
245 #[inline]
246 fn add_values_slice(&mut self, values: &[$type]) -> ValueResult<()> {
247 match self {
248 MultiValues::$variant(v) => {
249 v.extend_from_slice(values);
250 Ok(())
251 }
252 MultiValues::Empty(dt) if *dt == $data_type => {
253 *self = MultiValues::$variant(values.to_vec());
254 Ok(())
255 }
256 _ => Err(ValueError::TypeMismatch {
257 expected: $data_type,
258 actual: self.data_type(),
259 }),
260 }
261 }
262 }
263
264 impl<'a> MultiValuesAddArg<'a> for $type {
266 type Item = $type;
267
268 #[inline]
269 fn apply_add(self, target: &mut MultiValues) -> ValueResult<()> {
270 <MultiValues as MultiValuesAdder<$type>>::add_value(target, self)
271 }
272 }
273
274 impl<'a> MultiValuesAddArg<'a> for Vec<$type> {
275 type Item = $type;
276
277 #[inline]
278 fn apply_add(self, target: &mut MultiValues) -> ValueResult<()> {
279 <MultiValues as MultiValuesMultiAdder<$type>>::add_values(target, self)
280 }
281 }
282
283 impl<'a> MultiValuesAddArg<'a> for &'a [$type]
284 where
285 $type: Clone,
286 {
287 type Item = $type;
288
289 #[inline]
290 fn apply_add(self, target: &mut MultiValues) -> ValueResult<()> {
291 <MultiValues as MultiValuesMultiAdderSlice<$type>>::add_values_slice(target, self)
292 }
293 }
294
295 impl MultiValuesConstructor<$type> for MultiValues {
296 #[inline]
297 fn from_vec(values: Vec<$type>) -> Self {
298 MultiValues::$variant(values)
299 }
300 }
301 };
302}
303
304impl_multi_value_traits!(bool, Bool, DataType::Bool);
306impl_multi_value_traits!(char, Char, DataType::Char);
307impl_multi_value_traits!(i8, Int8, DataType::Int8);
308impl_multi_value_traits!(i16, Int16, DataType::Int16);
309impl_multi_value_traits!(i32, Int32, DataType::Int32);
310impl_multi_value_traits!(i64, Int64, DataType::Int64);
311impl_multi_value_traits!(i128, Int128, DataType::Int128);
312impl_multi_value_traits!(u8, UInt8, DataType::UInt8);
313impl_multi_value_traits!(u16, UInt16, DataType::UInt16);
314impl_multi_value_traits!(u32, UInt32, DataType::UInt32);
315impl_multi_value_traits!(u64, UInt64, DataType::UInt64);
316impl_multi_value_traits!(u128, UInt128, DataType::UInt128);
317impl_multi_value_traits!(f32, Float32, DataType::Float32);
318impl_multi_value_traits!(f64, Float64, DataType::Float64);
319impl_multi_value_traits!(String, String, DataType::String);
320impl_multi_value_traits!(NaiveDate, Date, DataType::Date);
321impl_multi_value_traits!(NaiveTime, Time, DataType::Time);
322impl_multi_value_traits!(NaiveDateTime, DateTime, DataType::DateTime);
323impl_multi_value_traits!(DateTime<Utc>, Instant, DataType::Instant);
324impl_multi_value_traits!(BigInt, BigInteger, DataType::BigInteger);
325impl_multi_value_traits!(BigDecimal, BigDecimal, DataType::BigDecimal);
326impl_multi_value_traits!(isize, IntSize, DataType::IntSize);
327impl_multi_value_traits!(usize, UIntSize, DataType::UIntSize);
328impl_multi_value_traits!(Duration, Duration, DataType::Duration);
329impl_multi_value_traits!(Url, Url, DataType::Url);
330impl_multi_value_traits!(HashMap<String, String>, StringMap, DataType::StringMap);
331impl_multi_value_traits!(serde_json::Value, Json, DataType::Json);
332
333impl MultiValuesSetArg<'_> for &str {
335 type Item = String;
336
337 #[inline]
338 fn apply(self, target: &mut MultiValues) -> ValueResult<()> {
339 <MultiValues as MultiValuesSingleSetter<String>>::set_single_value(target, self.to_string())
340 }
341}
342
343impl MultiValuesSetArg<'_> for Vec<&str> {
344 type Item = String;
345
346 #[inline]
347 fn apply(self, target: &mut MultiValues) -> ValueResult<()> {
348 let owned: Vec<String> = self.into_iter().map(|s| s.to_string()).collect();
349 <MultiValues as MultiValuesSetter<String>>::set_values(target, owned)
350 }
351}
352
353impl<'b> MultiValuesSetArg<'_> for &'b [&'b str] {
354 type Item = String;
355
356 #[inline]
357 fn apply(self, target: &mut MultiValues) -> ValueResult<()> {
358 let owned: Vec<String> = self.iter().map(|s| (*s).to_string()).collect();
359 <MultiValues as MultiValuesSetter<String>>::set_values(target, owned)
360 }
361}
362
363impl MultiValuesAddArg<'_> for &str {
364 type Item = String;
365
366 #[inline]
367 fn apply_add(self, target: &mut MultiValues) -> ValueResult<()> {
368 <MultiValues as MultiValuesAdder<String>>::add_value(target, self.to_string())
369 }
370}
371
372impl MultiValuesAddArg<'_> for Vec<&str> {
373 type Item = String;
374
375 #[inline]
376 fn apply_add(self, target: &mut MultiValues) -> ValueResult<()> {
377 let owned: Vec<String> = self.into_iter().map(|s| s.to_string()).collect();
378 <MultiValues as MultiValuesMultiAdder<String>>::add_values(target, owned)
379 }
380}
381
382impl<'b> MultiValuesAddArg<'_> for &'b [&'b str] {
383 type Item = String;
384
385 #[inline]
386 fn apply_add(self, target: &mut MultiValues) -> ValueResult<()> {
387 let owned: Vec<String> = self.iter().map(|s| (*s).to_string()).collect();
388 <MultiValues as MultiValuesMultiAdder<String>>::add_values(target, owned)
389 }
390}
391
392impl MultiValues {
397 pub fn to_value(&self) -> Value {
406 match self {
407 MultiValues::Empty(dt) => Value::Empty(*dt),
408 MultiValues::Bool(v) => v
409 .first()
410 .copied()
411 .map(Value::Bool)
412 .unwrap_or(Value::Empty(DataType::Bool)),
413 MultiValues::Char(v) => v
414 .first()
415 .copied()
416 .map(Value::Char)
417 .unwrap_or(Value::Empty(DataType::Char)),
418 MultiValues::Int8(v) => v
419 .first()
420 .copied()
421 .map(Value::Int8)
422 .unwrap_or(Value::Empty(DataType::Int8)),
423 MultiValues::Int16(v) => v
424 .first()
425 .copied()
426 .map(Value::Int16)
427 .unwrap_or(Value::Empty(DataType::Int16)),
428 MultiValues::Int32(v) => v
429 .first()
430 .copied()
431 .map(Value::Int32)
432 .unwrap_or(Value::Empty(DataType::Int32)),
433 MultiValues::Int64(v) => v
434 .first()
435 .copied()
436 .map(Value::Int64)
437 .unwrap_or(Value::Empty(DataType::Int64)),
438 MultiValues::Int128(v) => v
439 .first()
440 .copied()
441 .map(Value::Int128)
442 .unwrap_or(Value::Empty(DataType::Int128)),
443 MultiValues::UInt8(v) => v
444 .first()
445 .copied()
446 .map(Value::UInt8)
447 .unwrap_or(Value::Empty(DataType::UInt8)),
448 MultiValues::UInt16(v) => v
449 .first()
450 .copied()
451 .map(Value::UInt16)
452 .unwrap_or(Value::Empty(DataType::UInt16)),
453 MultiValues::UInt32(v) => v
454 .first()
455 .copied()
456 .map(Value::UInt32)
457 .unwrap_or(Value::Empty(DataType::UInt32)),
458 MultiValues::UInt64(v) => v
459 .first()
460 .copied()
461 .map(Value::UInt64)
462 .unwrap_or(Value::Empty(DataType::UInt64)),
463 MultiValues::UInt128(v) => v
464 .first()
465 .copied()
466 .map(Value::UInt128)
467 .unwrap_or(Value::Empty(DataType::UInt128)),
468 MultiValues::IntSize(v) => v
469 .first()
470 .copied()
471 .map(Value::IntSize)
472 .unwrap_or(Value::Empty(DataType::IntSize)),
473 MultiValues::UIntSize(v) => v
474 .first()
475 .copied()
476 .map(Value::UIntSize)
477 .unwrap_or(Value::Empty(DataType::UIntSize)),
478 MultiValues::Float32(v) => v
479 .first()
480 .copied()
481 .map(Value::Float32)
482 .unwrap_or(Value::Empty(DataType::Float32)),
483 MultiValues::Float64(v) => v
484 .first()
485 .copied()
486 .map(Value::Float64)
487 .unwrap_or(Value::Empty(DataType::Float64)),
488 MultiValues::BigInteger(v) => v
489 .first()
490 .cloned()
491 .map(Value::BigInteger)
492 .unwrap_or(Value::Empty(DataType::BigInteger)),
493 MultiValues::BigDecimal(v) => v
494 .first()
495 .cloned()
496 .map(Value::BigDecimal)
497 .unwrap_or(Value::Empty(DataType::BigDecimal)),
498 MultiValues::String(v) => v
499 .first()
500 .cloned()
501 .map(Value::String)
502 .unwrap_or(Value::Empty(DataType::String)),
503 MultiValues::Date(v) => v
504 .first()
505 .copied()
506 .map(Value::Date)
507 .unwrap_or(Value::Empty(DataType::Date)),
508 MultiValues::Time(v) => v
509 .first()
510 .copied()
511 .map(Value::Time)
512 .unwrap_or(Value::Empty(DataType::Time)),
513 MultiValues::DateTime(v) => v
514 .first()
515 .copied()
516 .map(Value::DateTime)
517 .unwrap_or(Value::Empty(DataType::DateTime)),
518 MultiValues::Instant(v) => v
519 .first()
520 .copied()
521 .map(Value::Instant)
522 .unwrap_or(Value::Empty(DataType::Instant)),
523 MultiValues::Duration(v) => v
524 .first()
525 .copied()
526 .map(Value::Duration)
527 .unwrap_or(Value::Empty(DataType::Duration)),
528 MultiValues::Url(v) => v
529 .first()
530 .cloned()
531 .map(Value::Url)
532 .unwrap_or(Value::Empty(DataType::Url)),
533 MultiValues::StringMap(v) => v
534 .first()
535 .cloned()
536 .map(Value::StringMap)
537 .unwrap_or(Value::Empty(DataType::StringMap)),
538 MultiValues::Json(v) => v
539 .first()
540 .cloned()
541 .map(Value::Json)
542 .unwrap_or(Value::Empty(DataType::Json)),
543 }
544 }
545
546 pub fn merge(&mut self, other: &MultiValues) -> ValueResult<()> {
569 if self.data_type() != other.data_type() {
570 return Err(ValueError::TypeMismatch {
571 expected: self.data_type(),
572 actual: other.data_type(),
573 });
574 }
575
576 match (self, other) {
577 (MultiValues::Bool(v), MultiValues::Bool(o)) => v.extend_from_slice(o),
578 (MultiValues::Char(v), MultiValues::Char(o)) => v.extend_from_slice(o),
579 (MultiValues::Int8(v), MultiValues::Int8(o)) => v.extend_from_slice(o),
580 (MultiValues::Int16(v), MultiValues::Int16(o)) => v.extend_from_slice(o),
581 (MultiValues::Int32(v), MultiValues::Int32(o)) => v.extend_from_slice(o),
582 (MultiValues::Int64(v), MultiValues::Int64(o)) => v.extend_from_slice(o),
583 (MultiValues::Int128(v), MultiValues::Int128(o)) => v.extend_from_slice(o),
584 (MultiValues::UInt8(v), MultiValues::UInt8(o)) => v.extend_from_slice(o),
585 (MultiValues::UInt16(v), MultiValues::UInt16(o)) => v.extend_from_slice(o),
586 (MultiValues::UInt32(v), MultiValues::UInt32(o)) => v.extend_from_slice(o),
587 (MultiValues::UInt64(v), MultiValues::UInt64(o)) => v.extend_from_slice(o),
588 (MultiValues::UInt128(v), MultiValues::UInt128(o)) => v.extend_from_slice(o),
589 (MultiValues::Float32(v), MultiValues::Float32(o)) => v.extend_from_slice(o),
590 (MultiValues::Float64(v), MultiValues::Float64(o)) => v.extend_from_slice(o),
591 (MultiValues::String(v), MultiValues::String(o)) => v.extend_from_slice(o),
592 (MultiValues::Date(v), MultiValues::Date(o)) => v.extend_from_slice(o),
593 (MultiValues::Time(v), MultiValues::Time(o)) => v.extend_from_slice(o),
594 (MultiValues::DateTime(v), MultiValues::DateTime(o)) => v.extend_from_slice(o),
595 (MultiValues::Instant(v), MultiValues::Instant(o)) => v.extend_from_slice(o),
596 (MultiValues::BigInteger(v), MultiValues::BigInteger(o)) => v.extend_from_slice(o),
597 (MultiValues::BigDecimal(v), MultiValues::BigDecimal(o)) => v.extend_from_slice(o),
598 (MultiValues::IntSize(v), MultiValues::IntSize(o)) => v.extend_from_slice(o),
599 (MultiValues::UIntSize(v), MultiValues::UIntSize(o)) => v.extend_from_slice(o),
600 (MultiValues::Duration(v), MultiValues::Duration(o)) => v.extend_from_slice(o),
601 (MultiValues::Url(v), MultiValues::Url(o)) => v.extend_from_slice(o),
602 (MultiValues::StringMap(v), MultiValues::StringMap(o)) => v.extend(o.iter().cloned()),
603 (MultiValues::Json(v), MultiValues::Json(o)) => v.extend(o.iter().cloned()),
604 (slot @ MultiValues::Empty(_), other_values) => *slot = other_values.clone(),
605 _ => unreachable!(),
606 }
607
608 Ok(())
609 }
610}
611
612impl Default for MultiValues {
613 #[inline]
614 fn default() -> Self {
615 MultiValues::Empty(DataType::String)
616 }
617}
618
619impl From<Value> for MultiValues {
620 fn from(value: Value) -> Self {
621 match value {
622 Value::Empty(dt) => MultiValues::Empty(dt),
623 Value::Bool(v) => MultiValues::Bool(vec![v]),
624 Value::Char(v) => MultiValues::Char(vec![v]),
625 Value::Int8(v) => MultiValues::Int8(vec![v]),
626 Value::Int16(v) => MultiValues::Int16(vec![v]),
627 Value::Int32(v) => MultiValues::Int32(vec![v]),
628 Value::Int64(v) => MultiValues::Int64(vec![v]),
629 Value::Int128(v) => MultiValues::Int128(vec![v]),
630 Value::UInt8(v) => MultiValues::UInt8(vec![v]),
631 Value::UInt16(v) => MultiValues::UInt16(vec![v]),
632 Value::UInt32(v) => MultiValues::UInt32(vec![v]),
633 Value::UInt64(v) => MultiValues::UInt64(vec![v]),
634 Value::UInt128(v) => MultiValues::UInt128(vec![v]),
635 Value::Float32(v) => MultiValues::Float32(vec![v]),
636 Value::Float64(v) => MultiValues::Float64(vec![v]),
637 Value::String(v) => MultiValues::String(vec![v]),
638 Value::Date(v) => MultiValues::Date(vec![v]),
639 Value::Time(v) => MultiValues::Time(vec![v]),
640 Value::DateTime(v) => MultiValues::DateTime(vec![v]),
641 Value::Instant(v) => MultiValues::Instant(vec![v]),
642 Value::BigInteger(v) => MultiValues::BigInteger(vec![v]),
643 Value::BigDecimal(v) => MultiValues::BigDecimal(vec![v]),
644 Value::IntSize(v) => MultiValues::IntSize(vec![v]),
645 Value::UIntSize(v) => MultiValues::UIntSize(vec![v]),
646 Value::Duration(v) => MultiValues::Duration(vec![v]),
647 Value::Url(v) => MultiValues::Url(vec![v]),
648 Value::StringMap(v) => MultiValues::StringMap(vec![v]),
649 Value::Json(v) => MultiValues::Json(vec![v]),
650 }
651 }
652}