• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

qubit-ltd / rs-value / 7bb4a305-cab1-4f73-ae1d-45fd12406bed

02 May 2026 05:05PM UTC coverage: 98.991% (-0.5%) from 99.488%
7bb4a305-cab1-4f73-ae1d-45fd12406bed

push

circleci

Haixing-Hu
docs: expand value README examples

1177 of 1189 relevant lines covered (98.99%)

31.12 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.42
/src/multi_values/multi_values_converters.rs
1
/*******************************************************************************
2
 *
3
 *    Copyright (c) 2025 - 2026 Haixing Hu.
4
 *
5
 *    SPDX-License-Identifier: Apache-2.0
6
 *
7
 *    Licensed under the Apache License, Version 2.0.
8
 *
9
 ******************************************************************************/
10

11
//! Internal conversion and interoperability implementations for `MultiValues`.
12
//!
13
//! This module keeps generic conversion logic (`to`, `to_list`, `to_value`, etc.)
14
//! while dispatch traits are implemented in dedicated `multi_values_*` modules.
15

16
use qubit_datatype::{
17
    DataConversionError,
18
    DataConversionOptions,
19
    DataConvertTo,
20
    DataConverter,
21
    DataConverters,
22
    DataListConversionError,
23
    DataType,
24
    ScalarStringDataConverters,
25
};
26

27
use crate::value_error::{
28
    ValueError,
29
    ValueResult,
30
};
31
use crate::{
32
    IntoValueDefault,
33
    Value,
34
};
35

36
use super::multi_values::MultiValues;
37

38
// ============================================================================
39
// Inherent conversion APIs and `Value` interop
40
// ============================================================================
41

42
/// Maps a shared single-value conversion error into `ValueError`.
43
///
44
/// # Parameters
45
///
46
/// * `error` - Error returned by `DataConverter`.
47
///
48
/// # Returns
49
///
50
/// Returns the corresponding `ValueError` variant.
51
fn map_data_conversion_error(error: DataConversionError) -> ValueError {
8✔
52
    match error {
8✔
53
        DataConversionError::NoValue => ValueError::NoValue,
3✔
54
        DataConversionError::ConversionFailed { from, to } => {
1✔
55
            ValueError::ConversionFailed { from, to }
1✔
56
        }
57
        DataConversionError::ConversionError(message) => ValueError::ConversionError(message),
3✔
58
        DataConversionError::JsonSerializationError(message) => {
×
59
            ValueError::JsonSerializationError(message)
×
60
        }
61
        DataConversionError::JsonDeserializationError(message) => {
1✔
62
            ValueError::JsonDeserializationError(message)
1✔
63
        }
64
    }
65
}
8✔
66

67
/// Maps a shared batch conversion error into `ValueError`.
68
///
69
/// # Parameters
70
///
71
/// * `error` - Error returned by `DataConverters`.
72
///
73
/// # Returns
74
///
75
/// Returns a `ValueError::ConversionError` whose message includes the failing
76
/// source element index and the underlying conversion error.
77
#[inline]
78
fn map_data_list_conversion_error(error: DataListConversionError) -> ValueError {
2✔
79
    let source = map_data_conversion_error(error.source);
2✔
80
    ValueError::ConversionError(format!(
2✔
81
        "Cannot convert value at index {}: {}",
2✔
82
        error.index, source
2✔
83
    ))
2✔
84
}
2✔
85

86
/// Converts the first item from a batch converter using conversion options.
87
///
88
/// # Type Parameters
89
///
90
/// * `T` - Target type.
91
/// * `I` - Iterator type wrapped by `DataConverters`.
92
///
93
/// # Parameters
94
///
95
/// * `values` - Batch converter containing source values.
96
/// * `options` - Conversion options forwarded to `qubit_datatype`.
97
///
98
/// # Returns
99
///
100
/// Returns the converted first value.
101
///
102
/// # Errors
103
///
104
/// Returns `ValueError::NoValue` for empty sources or the mapped single-value
105
/// conversion error for an invalid first source value.
106
#[inline]
107
fn convert_first_with<'a, T, I>(
31✔
108
    values: DataConverters<'a, I>,
31✔
109
    options: &DataConversionOptions,
31✔
110
) -> ValueResult<T>
31✔
111
where
31✔
112
    DataConverter<'a>: DataConvertTo<T>,
31✔
113
    I: Iterator,
31✔
114
    I::Item: Into<DataConverter<'a>>,
31✔
115
{
116
    values
31✔
117
        .to_first_with(options)
31✔
118
        .map_err(map_data_conversion_error)
31✔
119
}
31✔
120

121
/// Converts every item from a batch converter using conversion options.
122
///
123
/// # Type Parameters
124
///
125
/// * `T` - Target element type.
126
/// * `I` - Iterator type wrapped by `DataConverters`.
127
///
128
/// # Parameters
129
///
130
/// * `values` - Batch converter containing source values.
131
/// * `options` - Conversion options forwarded to `qubit_datatype`.
132
///
133
/// # Returns
134
///
135
/// Returns converted values in the original order.
136
///
137
/// # Errors
138
///
139
/// Returns a mapped batch conversion error containing the failing source index.
140
#[inline]
141
fn convert_values_with<'a, T, I>(
32✔
142
    values: DataConverters<'a, I>,
32✔
143
    options: &DataConversionOptions,
32✔
144
) -> ValueResult<Vec<T>>
32✔
145
where
32✔
146
    DataConverter<'a>: DataConvertTo<T>,
32✔
147
    I: Iterator,
32✔
148
    I::Item: Into<DataConverter<'a>>,
32✔
149
{
150
    values
32✔
151
        .to_vec_with(options)
32✔
152
        .map_err(map_data_list_conversion_error)
32✔
153
}
32✔
154

155
impl MultiValues {
156
    /// Converts the first stored value to `T`.
157
    ///
158
    /// Unlike [`Self::get_first`], this method uses shared `DataConverter`
159
    /// conversion rules instead of strict type matching. For example, a stored
160
    /// `String("1")` can be converted to `bool`.
161
    ///
162
    /// # Type Parameters
163
    ///
164
    /// * `T` - Target type.
165
    ///
166
    /// # Returns
167
    ///
168
    /// The converted first value.
169
    ///
170
    /// # Errors
171
    ///
172
    /// Returns [`ValueError::NoValue`] when no value is stored, or a conversion
173
    /// error when the first value cannot be converted to `T`.
174
    #[inline]
175
    pub fn to<T>(&self) -> ValueResult<T>
37✔
176
    where
37✔
177
        for<'a> DataConverter<'a>: DataConvertTo<T>,
37✔
178
    {
179
        self.to_with(&DataConversionOptions::default())
37✔
180
    }
37✔
181

182
    /// Converts the first stored value to `T`, or returns `default` when no
183
    /// value is stored.
184
    #[inline]
185
    pub fn to_or<T>(&self, default: impl IntoValueDefault<T>) -> ValueResult<T>
3✔
186
    where
3✔
187
        for<'a> DataConverter<'a>: DataConvertTo<T>,
3✔
188
    {
189
        match self.to() {
3✔
190
            Err(ValueError::NoValue) => Ok(default.into_value_default()),
1✔
191
            result => result,
2✔
192
        }
193
    }
3✔
194

195
    /// Converts the first stored value to `T` using conversion options.
196
    ///
197
    /// A `MultiValues::String` containing exactly one string is treated as a
198
    /// scalar string source, so collection options can split it before taking
199
    /// the first converted item. Multiple stored string values are treated as
200
    /// an already-materialized list and are converted element by element.
201
    ///
202
    /// # Type Parameters
203
    ///
204
    /// * `T` - Target type.
205
    ///
206
    /// # Parameters
207
    ///
208
    /// * `options` - Conversion options forwarded to `qubit_datatype`.
209
    ///
210
    /// # Returns
211
    ///
212
    /// The converted first value.
213
    ///
214
    /// # Errors
215
    ///
216
    /// Returns [`ValueError::NoValue`] when no value is stored, or a conversion
217
    /// error when the first value cannot be converted to `T`.
218
    #[inline]
219
    pub fn to_with<T>(&self, options: &DataConversionOptions) -> ValueResult<T>
39✔
220
    where
39✔
221
        for<'a> DataConverter<'a>: DataConvertTo<T>,
39✔
222
    {
223
        match self {
10✔
224
            MultiValues::Empty(_) => Err(ValueError::NoValue),
1✔
225
            MultiValues::Bool(v) => convert_first_with(DataConverters::from(v), options),
1✔
226
            MultiValues::Char(v) => convert_first_with(DataConverters::from(v), options),
1✔
227
            MultiValues::Int8(v) => convert_first_with(DataConverters::from(v), options),
1✔
228
            MultiValues::Int16(v) => convert_first_with(DataConverters::from(v), options),
1✔
229
            MultiValues::Int32(v) => convert_first_with(DataConverters::from(v), options),
2✔
230
            MultiValues::Int64(v) => convert_first_with(DataConverters::from(v), options),
1✔
231
            MultiValues::Int128(v) => convert_first_with(DataConverters::from(v), options),
1✔
232
            MultiValues::UInt8(v) => convert_first_with(DataConverters::from(v), options),
1✔
233
            MultiValues::UInt16(v) => convert_first_with(DataConverters::from(v), options),
1✔
234
            MultiValues::UInt32(v) => convert_first_with(DataConverters::from(v), options),
1✔
235
            MultiValues::UInt64(v) => convert_first_with(DataConverters::from(v), options),
1✔
236
            MultiValues::UInt128(v) => convert_first_with(DataConverters::from(v), options),
1✔
237
            MultiValues::IntSize(v) => convert_first_with(DataConverters::from(v), options),
1✔
238
            MultiValues::UIntSize(v) => convert_first_with(DataConverters::from(v), options),
1✔
239
            MultiValues::Float32(v) => convert_first_with(DataConverters::from(v), options),
1✔
240
            MultiValues::Float64(v) => convert_first_with(DataConverters::from(v), options),
1✔
241
            MultiValues::BigInteger(v) => convert_first_with(DataConverters::from(v), options),
1✔
242
            MultiValues::BigDecimal(v) => convert_first_with(DataConverters::from(v), options),
1✔
243
            MultiValues::String(v) if v.len() == 1 => {
10✔
244
                ScalarStringDataConverters::from(v[0].as_str())
7✔
245
                    .to_first_with(options)
7✔
246
                    .map_err(map_data_conversion_error)
7✔
247
            }
248
            MultiValues::String(v) => convert_first_with(DataConverters::from(v), options),
3✔
249
            MultiValues::Date(v) => convert_first_with(DataConverters::from(v), options),
2✔
250
            MultiValues::Time(v) => convert_first_with(DataConverters::from(v), options),
1✔
251
            MultiValues::DateTime(v) => convert_first_with(DataConverters::from(v), options),
1✔
252
            MultiValues::Instant(v) => convert_first_with(DataConverters::from(v), options),
1✔
253
            MultiValues::Duration(v) => convert_first_with(DataConverters::from(v), options),
1✔
254
            MultiValues::Url(v) => convert_first_with(DataConverters::from(v), options),
1✔
255
            MultiValues::StringMap(v) => convert_first_with(DataConverters::from(v), options),
1✔
256
            MultiValues::Json(v) => convert_first_with(DataConverters::from(v), options),
1✔
257
        }
258
    }
39✔
259

260
    /// Converts the first stored value to `T` using conversion options, or
261
    /// returns `default` when no value is stored.
262
    #[inline]
263
    pub fn to_or_with<T>(
1✔
264
        &self,
1✔
265
        default: impl IntoValueDefault<T>,
1✔
266
        options: &DataConversionOptions,
1✔
267
    ) -> ValueResult<T>
1✔
268
    where
1✔
269
        for<'a> DataConverter<'a>: DataConvertTo<T>,
1✔
270
    {
271
        match self.to_with(options) {
1✔
272
            Err(ValueError::NoValue) => Ok(default.into_value_default()),
1✔
273
            result => result,
×
274
        }
275
    }
1✔
276

277
    /// Converts all stored values to `T`.
278
    ///
279
    /// Unlike [`Self::get`], this method uses shared `DataConverter` conversion
280
    /// rules for every element instead of strict type matching. Empty values
281
    /// return an empty vector.
282
    ///
283
    /// # Type Parameters
284
    ///
285
    /// * `T` - Target element type.
286
    ///
287
    /// # Returns
288
    ///
289
    /// A vector containing all converted values in the original order.
290
    ///
291
    /// # Errors
292
    ///
293
    /// Returns the first conversion error encountered while converting an
294
    /// element.
295
    pub fn to_list<T>(&self) -> ValueResult<Vec<T>>
33✔
296
    where
33✔
297
        for<'a> DataConverter<'a>: DataConvertTo<T>,
33✔
298
    {
299
        self.to_list_with(&DataConversionOptions::default())
33✔
300
    }
33✔
301

302
    /// Converts all stored values to `T`, or returns `default` when the
303
    /// converted list is empty.
304
    #[inline]
305
    pub fn to_list_or<T>(&self, default: impl IntoValueDefault<Vec<T>>) -> ValueResult<Vec<T>>
1✔
306
    where
1✔
307
        for<'a> DataConverter<'a>: DataConvertTo<T>,
1✔
308
    {
309
        let values = self.to_list()?;
1✔
310
        if values.is_empty() {
1✔
311
            Ok(default.into_value_default())
1✔
312
        } else {
313
            Ok(values)
×
314
        }
315
    }
1✔
316

317
    /// Converts all stored values to `T` using conversion options.
318
    ///
319
    /// A `MultiValues::String` containing exactly one string is treated as a
320
    /// scalar string source, so collection options can split it into items.
321
    /// Multiple stored string values are treated as an already-materialized
322
    /// list and are converted element by element.
323
    ///
324
    /// # Type Parameters
325
    ///
326
    /// * `T` - Target element type.
327
    ///
328
    /// # Parameters
329
    ///
330
    /// * `options` - Conversion options forwarded to `qubit_datatype`.
331
    ///
332
    /// # Returns
333
    ///
334
    /// A vector containing all converted values in the original order.
335
    ///
336
    /// # Errors
337
    ///
338
    /// Returns the first conversion error encountered while converting an
339
    /// element.
340
    pub fn to_list_with<T>(&self, options: &DataConversionOptions) -> ValueResult<Vec<T>>
35✔
341
    where
35✔
342
        for<'a> DataConverter<'a>: DataConvertTo<T>,
35✔
343
    {
344
        match self {
7✔
345
            MultiValues::Empty(_) => Ok(Vec::new()),
1✔
346
            MultiValues::Bool(v) => convert_values_with(DataConverters::from(v), options),
1✔
347
            MultiValues::Char(v) => convert_values_with(DataConverters::from(v), options),
1✔
348
            MultiValues::Int8(v) => convert_values_with(DataConverters::from(v), options),
1✔
349
            MultiValues::Int16(v) => convert_values_with(DataConverters::from(v), options),
1✔
350
            MultiValues::Int32(v) => convert_values_with(DataConverters::from(v), options),
2✔
351
            MultiValues::Int64(v) => convert_values_with(DataConverters::from(v), options),
1✔
352
            MultiValues::Int128(v) => convert_values_with(DataConverters::from(v), options),
1✔
353
            MultiValues::UInt8(v) => convert_values_with(DataConverters::from(v), options),
1✔
354
            MultiValues::UInt16(v) => convert_values_with(DataConverters::from(v), options),
1✔
355
            MultiValues::UInt32(v) => convert_values_with(DataConverters::from(v), options),
1✔
356
            MultiValues::UInt64(v) => convert_values_with(DataConverters::from(v), options),
1✔
357
            MultiValues::UInt128(v) => convert_values_with(DataConverters::from(v), options),
1✔
358
            MultiValues::IntSize(v) => convert_values_with(DataConverters::from(v), options),
1✔
359
            MultiValues::UIntSize(v) => convert_values_with(DataConverters::from(v), options),
1✔
360
            MultiValues::Float32(v) => convert_values_with(DataConverters::from(v), options),
1✔
361
            MultiValues::Float64(v) => convert_values_with(DataConverters::from(v), options),
1✔
362
            MultiValues::BigInteger(v) => convert_values_with(DataConverters::from(v), options),
1✔
363
            MultiValues::BigDecimal(v) => convert_values_with(DataConverters::from(v), options),
1✔
364
            MultiValues::String(v) if v.len() == 1 => {
7✔
365
                ScalarStringDataConverters::from(v[0].as_str())
2✔
366
                    .to_vec_with(options)
2✔
367
                    .map_err(map_data_list_conversion_error)
2✔
368
            }
369
            MultiValues::String(v) => convert_values_with(DataConverters::from(v), options),
5✔
370
            MultiValues::Date(v) => convert_values_with(DataConverters::from(v), options),
1✔
371
            MultiValues::Time(v) => convert_values_with(DataConverters::from(v), options),
1✔
372
            MultiValues::DateTime(v) => convert_values_with(DataConverters::from(v), options),
1✔
373
            MultiValues::Instant(v) => convert_values_with(DataConverters::from(v), options),
1✔
374
            MultiValues::Duration(v) => convert_values_with(DataConverters::from(v), options),
1✔
375
            MultiValues::Url(v) => convert_values_with(DataConverters::from(v), options),
1✔
376
            MultiValues::StringMap(v) => convert_values_with(DataConverters::from(v), options),
1✔
377
            MultiValues::Json(v) => convert_values_with(DataConverters::from(v), options),
1✔
378
        }
379
    }
35✔
380

381
    /// Converts all stored values to `T` using conversion options, or returns
382
    /// `default` when the converted list is empty.
383
    #[inline]
384
    pub fn to_list_or_with<T>(
1✔
385
        &self,
1✔
386
        default: impl IntoValueDefault<Vec<T>>,
1✔
387
        options: &DataConversionOptions,
1✔
388
    ) -> ValueResult<Vec<T>>
1✔
389
    where
1✔
390
        for<'a> DataConverter<'a>: DataConvertTo<T>,
1✔
391
    {
392
        let values = self.to_list_with(options)?;
1✔
393
        if values.is_empty() {
1✔
394
            Ok(default.into_value_default())
1✔
395
        } else {
396
            Ok(values)
×
397
        }
398
    }
1✔
399

400
    /// Convert to a single [`Value`] by taking the first element.
401
    ///
402
    /// If there is no element, returns `Value::Empty(self.data_type())`.
403
    ///
404
    /// # Returns
405
    ///
406
    /// Returns the first element wrapped as [`Value`], or an empty value
407
    /// preserving the current data type.
408
    pub fn to_value(&self) -> Value {
63✔
409
        match self {
63✔
410
            MultiValues::Empty(dt) => Value::Empty(*dt),
2✔
411
            MultiValues::Bool(v) => v
2✔
412
                .first()
2✔
413
                .copied()
2✔
414
                .map(Value::Bool)
2✔
415
                .unwrap_or(Value::Empty(DataType::Bool)),
2✔
416
            MultiValues::Char(v) => v
2✔
417
                .first()
2✔
418
                .copied()
2✔
419
                .map(Value::Char)
2✔
420
                .unwrap_or(Value::Empty(DataType::Char)),
2✔
421
            MultiValues::Int8(v) => v
2✔
422
                .first()
2✔
423
                .copied()
2✔
424
                .map(Value::Int8)
2✔
425
                .unwrap_or(Value::Empty(DataType::Int8)),
2✔
426
            MultiValues::Int16(v) => v
2✔
427
                .first()
2✔
428
                .copied()
2✔
429
                .map(Value::Int16)
2✔
430
                .unwrap_or(Value::Empty(DataType::Int16)),
2✔
431
            MultiValues::Int32(v) => v
7✔
432
                .first()
7✔
433
                .copied()
7✔
434
                .map(Value::Int32)
7✔
435
                .unwrap_or(Value::Empty(DataType::Int32)),
7✔
436
            MultiValues::Int64(v) => v
2✔
437
                .first()
2✔
438
                .copied()
2✔
439
                .map(Value::Int64)
2✔
440
                .unwrap_or(Value::Empty(DataType::Int64)),
2✔
441
            MultiValues::Int128(v) => v
2✔
442
                .first()
2✔
443
                .copied()
2✔
444
                .map(Value::Int128)
2✔
445
                .unwrap_or(Value::Empty(DataType::Int128)),
2✔
446
            MultiValues::UInt8(v) => v
2✔
447
                .first()
2✔
448
                .copied()
2✔
449
                .map(Value::UInt8)
2✔
450
                .unwrap_or(Value::Empty(DataType::UInt8)),
2✔
451
            MultiValues::UInt16(v) => v
2✔
452
                .first()
2✔
453
                .copied()
2✔
454
                .map(Value::UInt16)
2✔
455
                .unwrap_or(Value::Empty(DataType::UInt16)),
2✔
456
            MultiValues::UInt32(v) => v
2✔
457
                .first()
2✔
458
                .copied()
2✔
459
                .map(Value::UInt32)
2✔
460
                .unwrap_or(Value::Empty(DataType::UInt32)),
2✔
461
            MultiValues::UInt64(v) => v
2✔
462
                .first()
2✔
463
                .copied()
2✔
464
                .map(Value::UInt64)
2✔
465
                .unwrap_or(Value::Empty(DataType::UInt64)),
2✔
466
            MultiValues::UInt128(v) => v
2✔
467
                .first()
2✔
468
                .copied()
2✔
469
                .map(Value::UInt128)
2✔
470
                .unwrap_or(Value::Empty(DataType::UInt128)),
2✔
471
            MultiValues::IntSize(v) => v
2✔
472
                .first()
2✔
473
                .copied()
2✔
474
                .map(Value::IntSize)
2✔
475
                .unwrap_or(Value::Empty(DataType::IntSize)),
2✔
476
            MultiValues::UIntSize(v) => v
4✔
477
                .first()
4✔
478
                .copied()
4✔
479
                .map(Value::UIntSize)
4✔
480
                .unwrap_or(Value::Empty(DataType::UIntSize)),
4✔
481
            MultiValues::Float32(v) => v
2✔
482
                .first()
2✔
483
                .copied()
2✔
484
                .map(Value::Float32)
2✔
485
                .unwrap_or(Value::Empty(DataType::Float32)),
2✔
486
            MultiValues::Float64(v) => v
2✔
487
                .first()
2✔
488
                .copied()
2✔
489
                .map(Value::Float64)
2✔
490
                .unwrap_or(Value::Empty(DataType::Float64)),
2✔
491
            MultiValues::BigInteger(v) => v
2✔
492
                .first()
2✔
493
                .cloned()
2✔
494
                .map(Value::BigInteger)
2✔
495
                .unwrap_or(Value::Empty(DataType::BigInteger)),
2✔
496
            MultiValues::BigDecimal(v) => v
2✔
497
                .first()
2✔
498
                .cloned()
2✔
499
                .map(Value::BigDecimal)
2✔
500
                .unwrap_or(Value::Empty(DataType::BigDecimal)),
2✔
501
            MultiValues::String(v) => v
1✔
502
                .first()
1✔
503
                .cloned()
1✔
504
                .map(Value::String)
1✔
505
                .unwrap_or(Value::Empty(DataType::String)),
1✔
506
            MultiValues::Date(v) => v
2✔
507
                .first()
2✔
508
                .copied()
2✔
509
                .map(Value::Date)
2✔
510
                .unwrap_or(Value::Empty(DataType::Date)),
2✔
511
            MultiValues::Time(v) => v
2✔
512
                .first()
2✔
513
                .copied()
2✔
514
                .map(Value::Time)
2✔
515
                .unwrap_or(Value::Empty(DataType::Time)),
2✔
516
            MultiValues::DateTime(v) => v
2✔
517
                .first()
2✔
518
                .copied()
2✔
519
                .map(Value::DateTime)
2✔
520
                .unwrap_or(Value::Empty(DataType::DateTime)),
2✔
521
            MultiValues::Instant(v) => v
2✔
522
                .first()
2✔
523
                .copied()
2✔
524
                .map(Value::Instant)
2✔
525
                .unwrap_or(Value::Empty(DataType::Instant)),
2✔
526
            MultiValues::Duration(v) => v
3✔
527
                .first()
3✔
528
                .copied()
3✔
529
                .map(Value::Duration)
3✔
530
                .unwrap_or(Value::Empty(DataType::Duration)),
3✔
531
            MultiValues::Url(v) => v
2✔
532
                .first()
2✔
533
                .cloned()
2✔
534
                .map(Value::Url)
2✔
535
                .unwrap_or(Value::Empty(DataType::Url)),
2✔
536
            MultiValues::StringMap(v) => v
2✔
537
                .first()
2✔
538
                .cloned()
2✔
539
                .map(Value::StringMap)
2✔
540
                .unwrap_or(Value::Empty(DataType::StringMap)),
2✔
541
            MultiValues::Json(v) => v
2✔
542
                .first()
2✔
543
                .cloned()
2✔
544
                .map(Value::Json)
2✔
545
                .unwrap_or(Value::Empty(DataType::Json)),
2✔
546
        }
547
    }
63✔
548

549
    /// Merge another multiple values
550
    ///
551
    /// Append all values from another multiple values to the current multiple values
552
    ///
553
    /// # Parameters
554
    ///
555
    /// * `other` - The multiple values to merge
556
    ///
557
    /// # Returns
558
    ///
559
    /// If types match, returns `Ok(())`; otherwise returns an error
560
    ///
561
    /// # Example
562
    ///
563
    /// ```rust
564
    /// use qubit_value::MultiValues;
565
    ///
566
    /// let mut a = MultiValues::Int32(vec![1, 2]);
567
    /// let b = MultiValues::Int32(vec![3, 4]);
568
    /// a.merge(&b).unwrap();
569
    /// assert_eq!(a.get_int32s().unwrap(), &[1, 2, 3, 4]);
570
    /// ```
571
    pub fn merge(&mut self, other: &MultiValues) -> ValueResult<()> {
36✔
572
        if self.data_type() != other.data_type() {
36✔
573
            return Err(ValueError::TypeMismatch {
2✔
574
                expected: self.data_type(),
2✔
575
                actual: other.data_type(),
2✔
576
            });
2✔
577
        }
34✔
578
        if other.count() == 0 {
34✔
579
            return Ok(());
4✔
580
        }
30✔
581

582
        match (self, other) {
30✔
583
            (MultiValues::Bool(v), MultiValues::Bool(o)) => v.extend_from_slice(o),
1✔
584
            (MultiValues::Char(v), MultiValues::Char(o)) => v.extend_from_slice(o),
1✔
585
            (MultiValues::Int8(v), MultiValues::Int8(o)) => v.extend_from_slice(o),
1✔
586
            (MultiValues::Int16(v), MultiValues::Int16(o)) => v.extend_from_slice(o),
1✔
587
            (MultiValues::Int32(v), MultiValues::Int32(o)) => v.extend_from_slice(o),
2✔
588
            (MultiValues::Int64(v), MultiValues::Int64(o)) => v.extend_from_slice(o),
1✔
589
            (MultiValues::Int128(v), MultiValues::Int128(o)) => v.extend_from_slice(o),
1✔
590
            (MultiValues::UInt8(v), MultiValues::UInt8(o)) => v.extend_from_slice(o),
1✔
591
            (MultiValues::UInt16(v), MultiValues::UInt16(o)) => v.extend_from_slice(o),
1✔
592
            (MultiValues::UInt32(v), MultiValues::UInt32(o)) => v.extend_from_slice(o),
1✔
593
            (MultiValues::UInt64(v), MultiValues::UInt64(o)) => v.extend_from_slice(o),
1✔
594
            (MultiValues::UInt128(v), MultiValues::UInt128(o)) => v.extend_from_slice(o),
1✔
595
            (MultiValues::Float32(v), MultiValues::Float32(o)) => v.extend_from_slice(o),
1✔
596
            (MultiValues::Float64(v), MultiValues::Float64(o)) => v.extend_from_slice(o),
1✔
597
            (MultiValues::String(v), MultiValues::String(o)) => v.extend_from_slice(o),
1✔
598
            (MultiValues::Date(v), MultiValues::Date(o)) => v.extend_from_slice(o),
1✔
599
            (MultiValues::Time(v), MultiValues::Time(o)) => v.extend_from_slice(o),
1✔
600
            (MultiValues::DateTime(v), MultiValues::DateTime(o)) => v.extend_from_slice(o),
1✔
601
            (MultiValues::Instant(v), MultiValues::Instant(o)) => v.extend_from_slice(o),
1✔
602
            (MultiValues::BigInteger(v), MultiValues::BigInteger(o)) => v.extend_from_slice(o),
1✔
603
            (MultiValues::BigDecimal(v), MultiValues::BigDecimal(o)) => v.extend_from_slice(o),
1✔
604
            (MultiValues::IntSize(v), MultiValues::IntSize(o)) => v.extend_from_slice(o),
1✔
605
            (MultiValues::UIntSize(v), MultiValues::UIntSize(o)) => v.extend_from_slice(o),
1✔
606
            (MultiValues::Duration(v), MultiValues::Duration(o)) => v.extend_from_slice(o),
1✔
607
            (MultiValues::Url(v), MultiValues::Url(o)) => v.extend_from_slice(o),
1✔
608
            (MultiValues::StringMap(v), MultiValues::StringMap(o)) => v.extend(o.iter().cloned()),
1✔
609
            (MultiValues::Json(v), MultiValues::Json(o)) => v.extend(o.iter().cloned()),
1✔
610
            (slot @ MultiValues::Empty(_), other_values) => *slot = other_values.clone(),
2✔
611
            _ => unreachable!(),
×
612
        }
613

614
        Ok(())
30✔
615
    }
36✔
616
}
617

618
impl Default for MultiValues {
619
    #[inline]
620
    fn default() -> Self {
1✔
621
        MultiValues::Empty(DataType::String)
1✔
622
    }
1✔
623
}
624

625
impl From<Value> for MultiValues {
626
    fn from(value: Value) -> Self {
32✔
627
        match value {
32✔
628
            Value::Empty(dt) => MultiValues::Empty(dt),
1✔
629
            Value::Bool(v) => MultiValues::Bool(vec![v]),
1✔
630
            Value::Char(v) => MultiValues::Char(vec![v]),
1✔
631
            Value::Int8(v) => MultiValues::Int8(vec![v]),
1✔
632
            Value::Int16(v) => MultiValues::Int16(vec![v]),
1✔
633
            Value::Int32(v) => MultiValues::Int32(vec![v]),
3✔
634
            Value::Int64(v) => MultiValues::Int64(vec![v]),
1✔
635
            Value::Int128(v) => MultiValues::Int128(vec![v]),
1✔
636
            Value::UInt8(v) => MultiValues::UInt8(vec![v]),
1✔
637
            Value::UInt16(v) => MultiValues::UInt16(vec![v]),
1✔
638
            Value::UInt32(v) => MultiValues::UInt32(vec![v]),
1✔
639
            Value::UInt64(v) => MultiValues::UInt64(vec![v]),
1✔
640
            Value::UInt128(v) => MultiValues::UInt128(vec![v]),
1✔
641
            Value::Float32(v) => MultiValues::Float32(vec![v]),
1✔
642
            Value::Float64(v) => MultiValues::Float64(vec![v]),
1✔
643
            Value::String(v) => MultiValues::String(vec![v]),
1✔
644
            Value::Date(v) => MultiValues::Date(vec![v]),
1✔
645
            Value::Time(v) => MultiValues::Time(vec![v]),
1✔
646
            Value::DateTime(v) => MultiValues::DateTime(vec![v]),
1✔
647
            Value::Instant(v) => MultiValues::Instant(vec![v]),
1✔
648
            Value::BigInteger(v) => MultiValues::BigInteger(vec![v]),
2✔
649
            Value::BigDecimal(v) => MultiValues::BigDecimal(vec![v]),
2✔
650
            Value::IntSize(v) => MultiValues::IntSize(vec![v]),
1✔
651
            Value::UIntSize(v) => MultiValues::UIntSize(vec![v]),
1✔
652
            Value::Duration(v) => MultiValues::Duration(vec![v]),
1✔
653
            Value::Url(v) => MultiValues::Url(vec![v]),
1✔
654
            Value::StringMap(v) => MultiValues::StringMap(vec![v]),
1✔
655
            Value::Json(v) => MultiValues::Json(vec![v]),
1✔
656
        }
657
    }
32✔
658
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc