Skip to main content

qubit_value/multi_values/
multi_values_converters.rs

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::ValueConverter;
12use crate::value_error::{ValueError, ValueResult};
13
14use super::multi_values::MultiValues;
15use super::multi_values_add_arg::MultiValuesAddArg;
16use super::multi_values_adder::MultiValuesAdder;
17use super::multi_values_constructor::MultiValuesConstructor;
18use super::multi_values_first_getter::MultiValuesFirstGetter;
19use super::multi_values_getter::MultiValuesGetter;
20use super::multi_values_multi_adder::MultiValuesMultiAdder;
21use super::multi_values_multi_adder_slice::MultiValuesMultiAdderSlice;
22use super::multi_values_set_arg::MultiValuesSetArg;
23use super::multi_values_setter::MultiValuesSetter;
24use super::multi_values_setter_slice::MultiValuesSetterSlice;
25use super::multi_values_single_setter::MultiValuesSingleSetter;
26
27// ============================================================================
28// Internal trait implementations (simplified using macros)
29// ============================================================================
30
31macro_rules! impl_multi_value_traits {
32    ($type:ty, $variant:ident, $data_type:expr) => {
33        impl MultiValuesGetter<$type> for MultiValues {
34            #[inline]
35            fn get_values(&self) -> ValueResult<Vec<$type>> {
36                match self {
37                    MultiValues::$variant(v) => Ok(v.clone()),
38                    MultiValues::Empty(dt) if *dt == $data_type => Ok(Vec::new()),
39                    _ => Err(ValueError::TypeMismatch {
40                        expected: $data_type,
41                        actual: self.data_type(),
42                    }),
43                }
44            }
45        }
46
47        impl MultiValuesFirstGetter<$type> for MultiValues {
48            #[inline]
49            fn get_first_value(&self) -> ValueResult<$type> {
50                match self {
51                    MultiValues::$variant(v) if !v.is_empty() => Ok(v[0].clone()),
52                    MultiValues::$variant(_) => Err(ValueError::NoValue),
53                    MultiValues::Empty(dt) if *dt == $data_type => Err(ValueError::NoValue),
54                    _ => Err(ValueError::TypeMismatch {
55                        expected: $data_type,
56                        actual: self.data_type(),
57                    }),
58                }
59            }
60        }
61
62        impl MultiValuesSetter<$type> for MultiValues {
63            #[inline]
64            fn set_values(&mut self, values: Vec<$type>) -> ValueResult<()> {
65                *self = MultiValues::$variant(values);
66                Ok(())
67            }
68        }
69
70        // Generic From implementation for SetParam is at the top level, not
71        // repeated here for specific types.
72
73        impl MultiValuesSetterSlice<$type> for MultiValues {
74            #[inline]
75            fn set_values_slice(&mut self, values: &[$type]) -> ValueResult<()> {
76                // Equivalent to set_[xxx]s_slice: replace entire list with slice
77                *self = MultiValues::$variant(values.to_vec());
78                Ok(())
79            }
80        }
81
82        impl MultiValuesSingleSetter<$type> for MultiValues {
83            #[inline]
84            fn set_single_value(&mut self, value: $type) -> ValueResult<()> {
85                *self = MultiValues::$variant(vec![value]);
86                Ok(())
87            }
88        }
89
90        impl MultiValuesAdder<$type> for MultiValues {
91            #[inline]
92            fn add_value(&mut self, value: $type) -> ValueResult<()> {
93                match self {
94                    MultiValues::$variant(v) => {
95                        v.push(value);
96                        Ok(())
97                    }
98                    MultiValues::Empty(dt) if *dt == $data_type => {
99                        *self = MultiValues::$variant(vec![value]);
100                        Ok(())
101                    }
102                    _ => Err(ValueError::TypeMismatch {
103                        expected: $data_type,
104                        actual: self.data_type(),
105                    }),
106                }
107            }
108        }
109
110        // Three types of implementations for local dispatch trait
111        impl<'a> MultiValuesSetArg<'a> for Vec<$type> {
112            type Item = $type;
113
114            #[inline]
115            fn apply(self, target: &mut MultiValues) -> ValueResult<()> {
116                <MultiValues as MultiValuesSetter<$type>>::set_values(target, self)
117            }
118        }
119
120        impl<'a> MultiValuesSetArg<'a> for &'a [$type]
121        where
122            $type: Clone,
123        {
124            type Item = $type;
125
126            #[inline]
127            fn apply(self, target: &mut MultiValues) -> ValueResult<()> {
128                <MultiValues as MultiValuesSetterSlice<$type>>::set_values_slice(target, self)
129            }
130        }
131
132        impl<'a> MultiValuesSetArg<'a> for $type {
133            type Item = $type;
134
135            #[inline]
136            fn apply(self, target: &mut MultiValues) -> ValueResult<()> {
137                <MultiValues as MultiValuesSingleSetter<$type>>::set_single_value(target, self)
138            }
139        }
140
141        impl MultiValuesMultiAdder<$type> for MultiValues {
142            #[inline]
143            fn add_values(&mut self, values: Vec<$type>) -> ValueResult<()> {
144                match self {
145                    MultiValues::$variant(v) => {
146                        v.extend(values);
147                        Ok(())
148                    }
149                    MultiValues::Empty(dt) if *dt == $data_type => {
150                        *self = MultiValues::$variant(values);
151                        Ok(())
152                    }
153                    _ => Err(ValueError::TypeMismatch {
154                        expected: $data_type,
155                        actual: self.data_type(),
156                    }),
157                }
158            }
159        }
160
161        impl MultiValuesMultiAdderSlice<$type> for MultiValues {
162            #[inline]
163            fn add_values_slice(&mut self, values: &[$type]) -> ValueResult<()> {
164                match self {
165                    MultiValues::$variant(v) => {
166                        v.extend_from_slice(values);
167                        Ok(())
168                    }
169                    MultiValues::Empty(dt) if *dt == $data_type => {
170                        *self = MultiValues::$variant(values.to_vec());
171                        Ok(())
172                    }
173                    _ => Err(ValueError::TypeMismatch {
174                        expected: $data_type,
175                        actual: self.data_type(),
176                    }),
177                }
178            }
179        }
180
181        // add dispatch: T / Vec<T> / &[T]
182        impl<'a> MultiValuesAddArg<'a> for $type {
183            type Item = $type;
184
185            #[inline]
186            fn apply_add(self, target: &mut MultiValues) -> ValueResult<()> {
187                <MultiValues as MultiValuesAdder<$type>>::add_value(target, self)
188            }
189        }
190
191        impl<'a> MultiValuesAddArg<'a> for Vec<$type> {
192            type Item = $type;
193
194            #[inline]
195            fn apply_add(self, target: &mut MultiValues) -> ValueResult<()> {
196                <MultiValues as MultiValuesMultiAdder<$type>>::add_values(target, self)
197            }
198        }
199
200        impl<'a> MultiValuesAddArg<'a> for &'a [$type]
201        where
202            $type: Clone,
203        {
204            type Item = $type;
205
206            #[inline]
207            fn apply_add(self, target: &mut MultiValues) -> ValueResult<()> {
208                <MultiValues as MultiValuesMultiAdderSlice<$type>>::add_values_slice(target, self)
209            }
210        }
211
212        impl MultiValuesConstructor<$type> for MultiValues {
213            #[inline]
214            fn from_vec(values: Vec<$type>) -> Self {
215                MultiValues::$variant(values)
216            }
217        }
218    };
219}
220
221// Implementation for Copy types
222impl_multi_value_traits!(bool, Bool, DataType::Bool);
223impl_multi_value_traits!(char, Char, DataType::Char);
224impl_multi_value_traits!(i8, Int8, DataType::Int8);
225impl_multi_value_traits!(i16, Int16, DataType::Int16);
226impl_multi_value_traits!(i32, Int32, DataType::Int32);
227impl_multi_value_traits!(i64, Int64, DataType::Int64);
228impl_multi_value_traits!(i128, Int128, DataType::Int128);
229impl_multi_value_traits!(u8, UInt8, DataType::UInt8);
230impl_multi_value_traits!(u16, UInt16, DataType::UInt16);
231impl_multi_value_traits!(u32, UInt32, DataType::UInt32);
232impl_multi_value_traits!(u64, UInt64, DataType::UInt64);
233impl_multi_value_traits!(u128, UInt128, DataType::UInt128);
234impl_multi_value_traits!(f32, Float32, DataType::Float32);
235impl_multi_value_traits!(f64, Float64, DataType::Float64);
236impl_multi_value_traits!(String, String, DataType::String);
237impl_multi_value_traits!(NaiveDate, Date, DataType::Date);
238impl_multi_value_traits!(NaiveTime, Time, DataType::Time);
239impl_multi_value_traits!(NaiveDateTime, DateTime, DataType::DateTime);
240impl_multi_value_traits!(DateTime<Utc>, Instant, DataType::Instant);
241impl_multi_value_traits!(BigInt, BigInteger, DataType::BigInteger);
242impl_multi_value_traits!(BigDecimal, BigDecimal, DataType::BigDecimal);
243impl_multi_value_traits!(isize, IntSize, DataType::IntSize);
244impl_multi_value_traits!(usize, UIntSize, DataType::UIntSize);
245impl_multi_value_traits!(Duration, Duration, DataType::Duration);
246impl_multi_value_traits!(Url, Url, DataType::Url);
247impl_multi_value_traits!(HashMap<String, String>, StringMap, DataType::StringMap);
248impl_multi_value_traits!(serde_json::Value, Json, DataType::Json);
249
250// Convenience adaptation: &str supported as input type for String
251impl MultiValuesSetArg<'_> for &str {
252    type Item = String;
253
254    #[inline]
255    fn apply(self, target: &mut MultiValues) -> ValueResult<()> {
256        <MultiValues as MultiValuesSingleSetter<String>>::set_single_value(target, self.to_string())
257    }
258}
259
260impl MultiValuesSetArg<'_> for Vec<&str> {
261    type Item = String;
262
263    #[inline]
264    fn apply(self, target: &mut MultiValues) -> ValueResult<()> {
265        let owned: Vec<String> = self.into_iter().map(|s| s.to_string()).collect();
266        <MultiValues as MultiValuesSetter<String>>::set_values(target, owned)
267    }
268}
269
270impl<'b> MultiValuesSetArg<'_> for &'b [&'b str] {
271    type Item = String;
272
273    #[inline]
274    fn apply(self, target: &mut MultiValues) -> ValueResult<()> {
275        let owned: Vec<String> = self.iter().map(|s| (*s).to_string()).collect();
276        <MultiValues as MultiValuesSetter<String>>::set_values(target, owned)
277    }
278}
279
280impl MultiValuesAddArg<'_> for &str {
281    type Item = String;
282
283    #[inline]
284    fn apply_add(self, target: &mut MultiValues) -> ValueResult<()> {
285        <MultiValues as MultiValuesAdder<String>>::add_value(target, self.to_string())
286    }
287}
288
289impl MultiValuesAddArg<'_> for Vec<&str> {
290    type Item = String;
291
292    #[inline]
293    fn apply_add(self, target: &mut MultiValues) -> ValueResult<()> {
294        let owned: Vec<String> = self.into_iter().map(|s| s.to_string()).collect();
295        <MultiValues as MultiValuesMultiAdder<String>>::add_values(target, owned)
296    }
297}
298
299impl<'b> MultiValuesAddArg<'_> for &'b [&'b str] {
300    type Item = String;
301
302    #[inline]
303    fn apply_add(self, target: &mut MultiValues) -> ValueResult<()> {
304        let owned: Vec<String> = self.iter().map(|s| (*s).to_string()).collect();
305        <MultiValues as MultiValuesMultiAdder<String>>::add_values(target, owned)
306    }
307}
308
309// ============================================================================
310// Inherent conversion APIs and `Value` interop
311// ============================================================================
312
313/// Converts an iterator of [`Value`] items into a target vector.
314///
315/// # Type Parameters
316///
317/// * `T` - Target element type.
318/// * `I` - Iterator type producing [`Value`] items.
319///
320/// # Parameters
321///
322/// * `values` - Values to convert.
323///
324/// # Returns
325///
326/// Converted values in the original order.
327///
328/// # Errors
329///
330/// Returns the first [`ValueError`] produced by [`Value::to`].
331fn convert_values<T, I>(values: I) -> ValueResult<Vec<T>>
332where
333    Value: ValueConverter<T>,
334    I: IntoIterator<Item = Value>,
335{
336    values.into_iter().map(|value| value.to::<T>()).collect()
337}
338
339impl MultiValues {
340    /// Converts the first stored value to `T`.
341    ///
342    /// Unlike [`Self::get_first`], this method uses [`Value::to`] conversion
343    /// rules instead of strict type matching. For example, a stored
344    /// `String("1")` can be converted to `bool`.
345    ///
346    /// # Type Parameters
347    ///
348    /// * `T` - Target type.
349    ///
350    /// # Returns
351    ///
352    /// The converted first value.
353    ///
354    /// # Errors
355    ///
356    /// Returns [`ValueError::NoValue`] when no value is stored, or a conversion
357    /// error when the first value cannot be converted to `T`.
358    #[inline]
359    pub fn to<T>(&self) -> ValueResult<T>
360    where
361        Value: ValueConverter<T>,
362    {
363        self.to_value().to::<T>()
364    }
365
366    /// Converts all stored values to `T`.
367    ///
368    /// Unlike [`Self::get`], this method uses [`Value::to`] conversion rules
369    /// for every element instead of strict type matching. Empty values return
370    /// an empty vector.
371    ///
372    /// # Type Parameters
373    ///
374    /// * `T` - Target element type.
375    ///
376    /// # Returns
377    ///
378    /// A vector containing all converted values in the original order.
379    ///
380    /// # Errors
381    ///
382    /// Returns the first conversion error encountered while converting an
383    /// element.
384    pub fn to_list<T>(&self) -> ValueResult<Vec<T>>
385    where
386        Value: ValueConverter<T>,
387    {
388        match self {
389            MultiValues::Empty(_) => Ok(Vec::new()),
390            MultiValues::Bool(v) => convert_values(v.iter().copied().map(Value::Bool)),
391            MultiValues::Char(v) => convert_values(v.iter().copied().map(Value::Char)),
392            MultiValues::Int8(v) => convert_values(v.iter().copied().map(Value::Int8)),
393            MultiValues::Int16(v) => convert_values(v.iter().copied().map(Value::Int16)),
394            MultiValues::Int32(v) => convert_values(v.iter().copied().map(Value::Int32)),
395            MultiValues::Int64(v) => convert_values(v.iter().copied().map(Value::Int64)),
396            MultiValues::Int128(v) => convert_values(v.iter().copied().map(Value::Int128)),
397            MultiValues::UInt8(v) => convert_values(v.iter().copied().map(Value::UInt8)),
398            MultiValues::UInt16(v) => convert_values(v.iter().copied().map(Value::UInt16)),
399            MultiValues::UInt32(v) => convert_values(v.iter().copied().map(Value::UInt32)),
400            MultiValues::UInt64(v) => convert_values(v.iter().copied().map(Value::UInt64)),
401            MultiValues::UInt128(v) => convert_values(v.iter().copied().map(Value::UInt128)),
402            MultiValues::IntSize(v) => convert_values(v.iter().copied().map(Value::IntSize)),
403            MultiValues::UIntSize(v) => convert_values(v.iter().copied().map(Value::UIntSize)),
404            MultiValues::Float32(v) => convert_values(v.iter().copied().map(Value::Float32)),
405            MultiValues::Float64(v) => convert_values(v.iter().copied().map(Value::Float64)),
406            MultiValues::BigInteger(v) => convert_values(v.iter().cloned().map(Value::BigInteger)),
407            MultiValues::BigDecimal(v) => convert_values(v.iter().cloned().map(Value::BigDecimal)),
408            MultiValues::String(v) => convert_values(v.iter().cloned().map(Value::String)),
409            MultiValues::Date(v) => convert_values(v.iter().copied().map(Value::Date)),
410            MultiValues::Time(v) => convert_values(v.iter().copied().map(Value::Time)),
411            MultiValues::DateTime(v) => convert_values(v.iter().copied().map(Value::DateTime)),
412            MultiValues::Instant(v) => convert_values(v.iter().copied().map(Value::Instant)),
413            MultiValues::Duration(v) => convert_values(v.iter().copied().map(Value::Duration)),
414            MultiValues::Url(v) => convert_values(v.iter().cloned().map(Value::Url)),
415            MultiValues::StringMap(v) => convert_values(v.iter().cloned().map(Value::StringMap)),
416            MultiValues::Json(v) => convert_values(v.iter().cloned().map(Value::Json)),
417        }
418    }
419
420    /// Convert to a single [`Value`] by taking the first element.
421    ///
422    /// If there is no element, returns `Value::Empty(self.data_type())`.
423    ///
424    /// # Returns
425    ///
426    /// Returns the first element wrapped as [`Value`], or an empty value
427    /// preserving the current data type.
428    pub fn to_value(&self) -> Value {
429        match self {
430            MultiValues::Empty(dt) => Value::Empty(*dt),
431            MultiValues::Bool(v) => v
432                .first()
433                .copied()
434                .map(Value::Bool)
435                .unwrap_or(Value::Empty(DataType::Bool)),
436            MultiValues::Char(v) => v
437                .first()
438                .copied()
439                .map(Value::Char)
440                .unwrap_or(Value::Empty(DataType::Char)),
441            MultiValues::Int8(v) => v
442                .first()
443                .copied()
444                .map(Value::Int8)
445                .unwrap_or(Value::Empty(DataType::Int8)),
446            MultiValues::Int16(v) => v
447                .first()
448                .copied()
449                .map(Value::Int16)
450                .unwrap_or(Value::Empty(DataType::Int16)),
451            MultiValues::Int32(v) => v
452                .first()
453                .copied()
454                .map(Value::Int32)
455                .unwrap_or(Value::Empty(DataType::Int32)),
456            MultiValues::Int64(v) => v
457                .first()
458                .copied()
459                .map(Value::Int64)
460                .unwrap_or(Value::Empty(DataType::Int64)),
461            MultiValues::Int128(v) => v
462                .first()
463                .copied()
464                .map(Value::Int128)
465                .unwrap_or(Value::Empty(DataType::Int128)),
466            MultiValues::UInt8(v) => v
467                .first()
468                .copied()
469                .map(Value::UInt8)
470                .unwrap_or(Value::Empty(DataType::UInt8)),
471            MultiValues::UInt16(v) => v
472                .first()
473                .copied()
474                .map(Value::UInt16)
475                .unwrap_or(Value::Empty(DataType::UInt16)),
476            MultiValues::UInt32(v) => v
477                .first()
478                .copied()
479                .map(Value::UInt32)
480                .unwrap_or(Value::Empty(DataType::UInt32)),
481            MultiValues::UInt64(v) => v
482                .first()
483                .copied()
484                .map(Value::UInt64)
485                .unwrap_or(Value::Empty(DataType::UInt64)),
486            MultiValues::UInt128(v) => v
487                .first()
488                .copied()
489                .map(Value::UInt128)
490                .unwrap_or(Value::Empty(DataType::UInt128)),
491            MultiValues::IntSize(v) => v
492                .first()
493                .copied()
494                .map(Value::IntSize)
495                .unwrap_or(Value::Empty(DataType::IntSize)),
496            MultiValues::UIntSize(v) => v
497                .first()
498                .copied()
499                .map(Value::UIntSize)
500                .unwrap_or(Value::Empty(DataType::UIntSize)),
501            MultiValues::Float32(v) => v
502                .first()
503                .copied()
504                .map(Value::Float32)
505                .unwrap_or(Value::Empty(DataType::Float32)),
506            MultiValues::Float64(v) => v
507                .first()
508                .copied()
509                .map(Value::Float64)
510                .unwrap_or(Value::Empty(DataType::Float64)),
511            MultiValues::BigInteger(v) => v
512                .first()
513                .cloned()
514                .map(Value::BigInteger)
515                .unwrap_or(Value::Empty(DataType::BigInteger)),
516            MultiValues::BigDecimal(v) => v
517                .first()
518                .cloned()
519                .map(Value::BigDecimal)
520                .unwrap_or(Value::Empty(DataType::BigDecimal)),
521            MultiValues::String(v) => v
522                .first()
523                .cloned()
524                .map(Value::String)
525                .unwrap_or(Value::Empty(DataType::String)),
526            MultiValues::Date(v) => v
527                .first()
528                .copied()
529                .map(Value::Date)
530                .unwrap_or(Value::Empty(DataType::Date)),
531            MultiValues::Time(v) => v
532                .first()
533                .copied()
534                .map(Value::Time)
535                .unwrap_or(Value::Empty(DataType::Time)),
536            MultiValues::DateTime(v) => v
537                .first()
538                .copied()
539                .map(Value::DateTime)
540                .unwrap_or(Value::Empty(DataType::DateTime)),
541            MultiValues::Instant(v) => v
542                .first()
543                .copied()
544                .map(Value::Instant)
545                .unwrap_or(Value::Empty(DataType::Instant)),
546            MultiValues::Duration(v) => v
547                .first()
548                .copied()
549                .map(Value::Duration)
550                .unwrap_or(Value::Empty(DataType::Duration)),
551            MultiValues::Url(v) => v
552                .first()
553                .cloned()
554                .map(Value::Url)
555                .unwrap_or(Value::Empty(DataType::Url)),
556            MultiValues::StringMap(v) => v
557                .first()
558                .cloned()
559                .map(Value::StringMap)
560                .unwrap_or(Value::Empty(DataType::StringMap)),
561            MultiValues::Json(v) => v
562                .first()
563                .cloned()
564                .map(Value::Json)
565                .unwrap_or(Value::Empty(DataType::Json)),
566        }
567    }
568
569    /// Merge another multiple values
570    ///
571    /// Append all values from another multiple values to the current multiple values
572    ///
573    /// # Parameters
574    ///
575    /// * `other` - The multiple values to merge
576    ///
577    /// # Returns
578    ///
579    /// If types match, returns `Ok(())`; otherwise returns an error
580    ///
581    /// # Example
582    ///
583    /// ```rust
584    /// use qubit_value::MultiValues;
585    ///
586    /// let mut a = MultiValues::Int32(vec![1, 2]);
587    /// let b = MultiValues::Int32(vec![3, 4]);
588    /// a.merge(&b).unwrap();
589    /// assert_eq!(a.get_int32s().unwrap(), &[1, 2, 3, 4]);
590    /// ```
591    pub fn merge(&mut self, other: &MultiValues) -> ValueResult<()> {
592        if self.data_type() != other.data_type() {
593            return Err(ValueError::TypeMismatch {
594                expected: self.data_type(),
595                actual: other.data_type(),
596            });
597        }
598        if other.count() == 0 {
599            return Ok(());
600        }
601
602        match (self, other) {
603            (MultiValues::Bool(v), MultiValues::Bool(o)) => v.extend_from_slice(o),
604            (MultiValues::Char(v), MultiValues::Char(o)) => v.extend_from_slice(o),
605            (MultiValues::Int8(v), MultiValues::Int8(o)) => v.extend_from_slice(o),
606            (MultiValues::Int16(v), MultiValues::Int16(o)) => v.extend_from_slice(o),
607            (MultiValues::Int32(v), MultiValues::Int32(o)) => v.extend_from_slice(o),
608            (MultiValues::Int64(v), MultiValues::Int64(o)) => v.extend_from_slice(o),
609            (MultiValues::Int128(v), MultiValues::Int128(o)) => v.extend_from_slice(o),
610            (MultiValues::UInt8(v), MultiValues::UInt8(o)) => v.extend_from_slice(o),
611            (MultiValues::UInt16(v), MultiValues::UInt16(o)) => v.extend_from_slice(o),
612            (MultiValues::UInt32(v), MultiValues::UInt32(o)) => v.extend_from_slice(o),
613            (MultiValues::UInt64(v), MultiValues::UInt64(o)) => v.extend_from_slice(o),
614            (MultiValues::UInt128(v), MultiValues::UInt128(o)) => v.extend_from_slice(o),
615            (MultiValues::Float32(v), MultiValues::Float32(o)) => v.extend_from_slice(o),
616            (MultiValues::Float64(v), MultiValues::Float64(o)) => v.extend_from_slice(o),
617            (MultiValues::String(v), MultiValues::String(o)) => v.extend_from_slice(o),
618            (MultiValues::Date(v), MultiValues::Date(o)) => v.extend_from_slice(o),
619            (MultiValues::Time(v), MultiValues::Time(o)) => v.extend_from_slice(o),
620            (MultiValues::DateTime(v), MultiValues::DateTime(o)) => v.extend_from_slice(o),
621            (MultiValues::Instant(v), MultiValues::Instant(o)) => v.extend_from_slice(o),
622            (MultiValues::BigInteger(v), MultiValues::BigInteger(o)) => v.extend_from_slice(o),
623            (MultiValues::BigDecimal(v), MultiValues::BigDecimal(o)) => v.extend_from_slice(o),
624            (MultiValues::IntSize(v), MultiValues::IntSize(o)) => v.extend_from_slice(o),
625            (MultiValues::UIntSize(v), MultiValues::UIntSize(o)) => v.extend_from_slice(o),
626            (MultiValues::Duration(v), MultiValues::Duration(o)) => v.extend_from_slice(o),
627            (MultiValues::Url(v), MultiValues::Url(o)) => v.extend_from_slice(o),
628            (MultiValues::StringMap(v), MultiValues::StringMap(o)) => v.extend(o.iter().cloned()),
629            (MultiValues::Json(v), MultiValues::Json(o)) => v.extend(o.iter().cloned()),
630            (slot @ MultiValues::Empty(_), other_values) => *slot = other_values.clone(),
631            _ => unreachable!(),
632        }
633
634        Ok(())
635    }
636}
637
638impl Default for MultiValues {
639    #[inline]
640    fn default() -> Self {
641        MultiValues::Empty(DataType::String)
642    }
643}
644
645impl From<Value> for MultiValues {
646    fn from(value: Value) -> Self {
647        match value {
648            Value::Empty(dt) => MultiValues::Empty(dt),
649            Value::Bool(v) => MultiValues::Bool(vec![v]),
650            Value::Char(v) => MultiValues::Char(vec![v]),
651            Value::Int8(v) => MultiValues::Int8(vec![v]),
652            Value::Int16(v) => MultiValues::Int16(vec![v]),
653            Value::Int32(v) => MultiValues::Int32(vec![v]),
654            Value::Int64(v) => MultiValues::Int64(vec![v]),
655            Value::Int128(v) => MultiValues::Int128(vec![v]),
656            Value::UInt8(v) => MultiValues::UInt8(vec![v]),
657            Value::UInt16(v) => MultiValues::UInt16(vec![v]),
658            Value::UInt32(v) => MultiValues::UInt32(vec![v]),
659            Value::UInt64(v) => MultiValues::UInt64(vec![v]),
660            Value::UInt128(v) => MultiValues::UInt128(vec![v]),
661            Value::Float32(v) => MultiValues::Float32(vec![v]),
662            Value::Float64(v) => MultiValues::Float64(vec![v]),
663            Value::String(v) => MultiValues::String(vec![v]),
664            Value::Date(v) => MultiValues::Date(vec![v]),
665            Value::Time(v) => MultiValues::Time(vec![v]),
666            Value::DateTime(v) => MultiValues::DateTime(vec![v]),
667            Value::Instant(v) => MultiValues::Instant(vec![v]),
668            Value::BigInteger(v) => MultiValues::BigInteger(vec![v]),
669            Value::BigDecimal(v) => MultiValues::BigDecimal(vec![v]),
670            Value::IntSize(v) => MultiValues::IntSize(vec![v]),
671            Value::UIntSize(v) => MultiValues::UIntSize(vec![v]),
672            Value::Duration(v) => MultiValues::Duration(vec![v]),
673            Value::Url(v) => MultiValues::Url(vec![v]),
674            Value::StringMap(v) => MultiValues::StringMap(vec![v]),
675            Value::Json(v) => MultiValues::Json(vec![v]),
676        }
677    }
678}