Skip to main content

qubit_value/multi_values/
multi_values_getters.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//! Type-specific read accessors for `MultiValues`.
12
13use std::collections::HashMap;
14use std::time::Duration;
15
16use bigdecimal::BigDecimal;
17use chrono::{
18    DateTime,
19    NaiveDate,
20    NaiveDateTime,
21    NaiveTime,
22    Utc,
23};
24use num_bigint::BigInt;
25use url::Url;
26
27use qubit_datatype::DataType;
28
29use crate::value_error::{
30    ValueError,
31    ValueResult,
32};
33
34use super::multi_values::MultiValues;
35
36impl MultiValues {
37    // ========================================================================
38    // Get first value (as single value access)
39    // ========================================================================
40
41    impl_get_first_value! {
42        /// Get the first boolean value.
43        ///
44        /// # Returns
45        ///
46        /// If types match and a value exists, returns the first boolean value; see `# Errors`.
47        ///
48        /// # Example
49        ///
50        /// ```rust
51        /// use qubit_value::MultiValues;
52        ///
53        /// let values = MultiValues::Bool(vec![true, false]);
54        /// assert_eq!(values.get_first_bool().unwrap(), true);
55        /// ```
56        copy: get_first_bool, Bool, bool, DataType::Bool
57    }
58
59    impl_get_first_value! {
60        /// Get the first character value
61        ///
62        /// # Returns
63        ///
64        /// If types match and a value exists, returns the first character value; see `# Errors`.
65        copy: get_first_char, Char, char, DataType::Char
66    }
67
68    impl_get_first_value! {
69        /// Get the first int8 value
70        ///
71        /// # Returns
72        ///
73        /// If types match and a value exists, returns the first int8 value; see `# Errors`.
74        copy: get_first_int8, Int8, i8, DataType::Int8
75    }
76
77    impl_get_first_value! {
78        /// Get the first int16 value
79        ///
80        /// # Returns
81        ///
82        /// If types match and a value exists, returns the first int16 value; see `# Errors`.
83        copy: get_first_int16, Int16, i16, DataType::Int16
84    }
85
86    impl_get_first_value! {
87        /// Get the first int32 value
88        ///
89        /// # Returns
90        ///
91        /// If types match and a value exists, returns the first int32 value; see `# Errors`.
92        copy: get_first_int32, Int32, i32, DataType::Int32
93    }
94
95    impl_get_first_value! {
96        /// Get the first int64 value
97        ///
98        /// # Returns
99        ///
100        /// If types match and a value exists, returns the first int64 value; see `# Errors`.
101        copy: get_first_int64, Int64, i64, DataType::Int64
102    }
103
104    impl_get_first_value! {
105        /// Get the first int128 value
106        ///
107        /// # Returns
108        ///
109        /// If types match and a value exists, returns the first int128 value; see `# Errors`.
110        copy: get_first_int128, Int128, i128, DataType::Int128
111    }
112
113    impl_get_first_value! {
114        /// Get the first uint8 value
115        ///
116        /// # Returns
117        ///
118        /// If types match and a value exists, returns the first uint8 value; see `# Errors`.
119        copy: get_first_uint8, UInt8, u8, DataType::UInt8
120    }
121
122    impl_get_first_value! {
123        /// Get the first uint16 value
124        ///
125        /// # Returns
126        ///
127        /// If types match and a value exists, returns the first uint16 value; see `# Errors`.
128        copy: get_first_uint16, UInt16, u16, DataType::UInt16
129    }
130
131    impl_get_first_value! {
132        /// Get the first uint32 value
133        ///
134        /// # Returns
135        ///
136        /// If types match and a value exists, returns the first uint32 value; see `# Errors`.
137        copy: get_first_uint32, UInt32, u32, DataType::UInt32
138    }
139
140    impl_get_first_value! {
141        /// Get the first uint64 value
142        ///
143        /// # Returns
144        ///
145        /// If types match and a value exists, returns the first uint64 value; see `# Errors`.
146        copy: get_first_uint64, UInt64, u64, DataType::UInt64
147    }
148
149    impl_get_first_value! {
150        /// Get the first uint128 value
151        ///
152        /// # Returns
153        ///
154        /// If types match and a value exists, returns the first uint128 value; see `# Errors`.
155        copy: get_first_uint128, UInt128, u128, DataType::UInt128
156    }
157
158    impl_get_first_value! {
159        /// Get the first float32 value
160        ///
161        /// # Returns
162        ///
163        /// If types match and a value exists, returns the first float32 value; see `# Errors`.
164        copy: get_first_float32, Float32, f32, DataType::Float32
165    }
166
167    impl_get_first_value! {
168        /// Get the first float64 value
169        ///
170        /// # Returns
171        ///
172        /// If types match and a value exists, returns the first float64 value; see `# Errors`.
173        copy: get_first_float64, Float64, f64, DataType::Float64
174    }
175
176    impl_get_first_value! {
177        /// Get the first string reference
178        ///
179        /// # Returns
180        ///
181        /// If types match and a value exists, returns a reference to the first
182        /// string; see `# Errors`.
183        ref: get_first_string, String, &str, DataType::String, |s: &String| s.as_str()
184    }
185
186    impl_get_first_value! {
187        /// Get the first date value
188        ///
189        /// # Returns
190        ///
191        /// If types match and a value exists, returns the first date value; see `# Errors`.
192        copy: get_first_date, Date, NaiveDate, DataType::Date
193    }
194
195    impl_get_first_value! {
196        /// Get the first time value
197        ///
198        /// # Returns
199        ///
200        /// If types match and a value exists, returns the first time value; see `# Errors`.
201        copy: get_first_time, Time, NaiveTime, DataType::Time
202    }
203
204    impl_get_first_value! {
205        /// Get the first datetime value
206        ///
207        /// # Returns
208        ///
209        /// If types match and a value exists, returns the first datetime value; see `# Errors`.
210        copy: get_first_datetime, DateTime, NaiveDateTime, DataType::DateTime
211    }
212
213    impl_get_first_value! {
214        /// Get the first UTC instant value
215        ///
216        /// # Returns
217        ///
218        /// If types match and a value exists, returns the first UTC instant
219        /// value; see `# Errors`.
220        copy: get_first_instant, Instant, DateTime<Utc>, DataType::Instant
221    }
222
223    impl_get_first_value! {
224        /// Get the first big integer value
225        ///
226        /// # Returns
227        ///
228        /// If types match and a value exists, returns the first big integer
229        /// value; see `# Errors`.
230        ref: get_first_biginteger, BigInteger, BigInt, DataType::BigInteger, |v: &BigInt| v.clone()
231    }
232
233    impl_get_first_value! {
234        /// Get the first big decimal value
235        ///
236        /// # Returns
237        ///
238        /// If types match and a value exists, returns the first big decimal
239        /// value; see `# Errors`.
240        ref: get_first_bigdecimal, BigDecimal, BigDecimal, DataType::BigDecimal, |v: &BigDecimal| v.clone()
241    }
242
243    impl_get_first_value! {
244        /// Get the first isize value
245        copy: get_first_intsize, IntSize, isize, DataType::IntSize
246    }
247
248    impl_get_first_value! {
249        /// Get the first usize value
250        copy: get_first_uintsize, UIntSize, usize, DataType::UIntSize
251    }
252
253    impl_get_first_value! {
254        /// Get the first Duration value
255        copy: get_first_duration, Duration, Duration, DataType::Duration
256    }
257
258    impl_get_first_value! {
259        /// Get the first Url value
260        ref: get_first_url, Url, Url, DataType::Url, |v: &Url| v.clone()
261    }
262
263    impl_get_first_value! {
264        /// Get the first StringMap value
265        ref: get_first_string_map, StringMap, HashMap<String, String>, DataType::StringMap, |v: &HashMap<String, String>| v.clone()
266    }
267
268    impl_get_first_value! {
269        /// Get the first Json value
270        ref: get_first_json, Json, serde_json::Value, DataType::Json, |v: &serde_json::Value| v.clone()
271    }
272
273    // ========================================================================
274    // Get all values (type checking)
275    // ========================================================================
276
277    impl_get_multi_values! {
278        /// Get reference to all boolean values
279        ///
280        /// # Returns
281        ///
282        /// If types match, returns a reference to the boolean value array; see `# Errors`.
283        ///
284        /// # Example
285        ///
286        /// ```rust
287        /// use qubit_value::MultiValues;
288        ///
289        /// let values = MultiValues::Bool(vec![true, false, true]);
290        /// assert_eq!(values.get_bools().unwrap(), &[true, false, true]);
291        /// ```
292        slice: get_bools, Bool, bool, DataType::Bool
293    }
294
295    impl_get_multi_values! {
296        /// Get reference to all character values
297        ///
298        /// # Returns
299        ///
300        /// If types match, returns a reference to the character value array; see `# Errors`.
301        slice: get_chars, Char, char, DataType::Char
302    }
303
304    impl_get_multi_values! {
305        /// Get reference to all int8 values
306        ///
307        /// # Returns
308        ///
309        /// If types match, returns a reference to the int8 value array; see `# Errors`.
310        slice: get_int8s, Int8, i8, DataType::Int8
311    }
312
313    impl_get_multi_values! {
314        /// Get reference to all int16 values
315        ///
316        /// # Returns
317        ///
318        /// If types match, returns a reference to the int16 value array; see `# Errors`.
319        slice: get_int16s, Int16, i16, DataType::Int16
320    }
321
322    impl_get_multi_values! {
323        /// Get reference to all int32 values
324        ///
325        /// # Returns
326        ///
327        /// If types match, returns a reference to the int32 value array; see `# Errors`.
328        slice: get_int32s, Int32, i32, DataType::Int32
329    }
330
331    impl_get_multi_values! {
332        /// Get reference to all int64 values
333        ///
334        /// # Returns
335        ///
336        /// If types match, returns a reference to the int64 value array; see `# Errors`.
337        slice: get_int64s, Int64, i64, DataType::Int64
338    }
339
340    impl_get_multi_values! {
341        /// Get reference to all int128 values
342        ///
343        /// # Returns
344        ///
345        /// If types match, returns a reference to the int128 value array; see `# Errors`.
346        slice: get_int128s, Int128, i128, DataType::Int128
347    }
348
349    impl_get_multi_values! {
350        /// Get reference to all uint8 values
351        ///
352        /// # Returns
353        ///
354        /// If types match, returns a reference to the uint8 value array; see `# Errors`.
355        slice: get_uint8s, UInt8, u8, DataType::UInt8
356    }
357
358    impl_get_multi_values! {
359        /// Get reference to all uint16 values
360        ///
361        /// # Returns
362        ///
363        /// If types match, returns a reference to the uint16 value array; see `# Errors`.
364        slice: get_uint16s, UInt16, u16, DataType::UInt16
365    }
366
367    impl_get_multi_values! {
368        /// Get reference to all uint32 values
369        ///
370        /// # Returns
371        ///
372        /// If types match, returns a reference to the uint32 value array; see `# Errors`.
373        slice: get_uint32s, UInt32, u32, DataType::UInt32
374    }
375
376    impl_get_multi_values! {
377        /// Get reference to all uint64 values
378        ///
379        /// # Returns
380        ///
381        /// If types match, returns a reference to the uint64 value array; see `# Errors`.
382        slice: get_uint64s, UInt64, u64, DataType::UInt64
383    }
384
385    impl_get_multi_values! {
386        /// Get reference to all uint128 values
387        ///
388        /// # Returns
389        ///
390        /// If types match, returns a reference to the uint128 value array; see `# Errors`.
391        slice: get_uint128s, UInt128, u128, DataType::UInt128
392    }
393
394    impl_get_multi_values! {
395        /// Get reference to all float32 values
396        ///
397        /// # Returns
398        ///
399        /// If types match, returns a reference to the float32 value array; see `# Errors`.
400        slice: get_float32s, Float32, f32, DataType::Float32
401    }
402
403    impl_get_multi_values! {
404        /// Get reference to all float64 values
405        ///
406        /// # Returns
407        ///
408        /// If types match, returns a reference to the float64 value array; see `# Errors`.
409        slice: get_float64s, Float64, f64, DataType::Float64
410    }
411
412    impl_get_multi_values! {
413        /// Get reference to all strings
414        ///
415        /// # Returns
416        ///
417        /// If types match, returns a reference to the string array; otherwise
418        /// returns an error
419        vec: get_strings, String, String, DataType::String
420    }
421
422    impl_get_multi_values! {
423        /// Get reference to all date values
424        ///
425        /// # Returns
426        ///
427        /// If types match, returns a reference to the date value array; see `# Errors`.
428        slice: get_dates, Date, NaiveDate, DataType::Date
429    }
430
431    impl_get_multi_values! {
432        /// Get reference to all time values
433        ///
434        /// # Returns
435        ///
436        /// If types match, returns a reference to the time value array; see `# Errors`.
437        slice: get_times, Time, NaiveTime, DataType::Time
438    }
439
440    impl_get_multi_values! {
441        /// Get reference to all datetime values
442        ///
443        /// # Returns
444        ///
445        /// If types match, returns a reference to the datetime value array; see `# Errors`.
446        slice: get_datetimes, DateTime, NaiveDateTime, DataType::DateTime
447    }
448
449    impl_get_multi_values! {
450        /// Get reference to all UTC instant values
451        ///
452        /// # Returns
453        ///
454        /// If types match, returns a reference to the UTC instant value array; see `# Errors`.
455        slice: get_instants, Instant, DateTime<Utc>, DataType::Instant
456    }
457
458    impl_get_multi_values! {
459        /// Get reference to all big integers
460        ///
461        /// # Returns
462        ///
463        /// If types match, returns a reference to the big integer array; see `# Errors`.
464        vec: get_bigintegers, BigInteger, BigInt, DataType::BigInteger
465    }
466
467    impl_get_multi_values! {
468        /// Get reference to all big decimals
469        ///
470        /// # Returns
471        ///
472        /// If types match, returns a reference to the big decimal array; see `# Errors`.
473        vec: get_bigdecimals, BigDecimal, BigDecimal, DataType::BigDecimal
474    }
475
476    impl_get_multi_values! {
477        /// Get reference to all isize values
478        slice: get_intsizes, IntSize, isize, DataType::IntSize
479    }
480
481    impl_get_multi_values! {
482        /// Get reference to all usize values
483        slice: get_uintsizes, UIntSize, usize, DataType::UIntSize
484    }
485
486    impl_get_multi_values! {
487        /// Get reference to all Duration values
488        slice: get_durations, Duration, Duration, DataType::Duration
489    }
490
491    impl_get_multi_values! {
492        /// Get reference to all Url values
493        vec: get_urls, Url, Url, DataType::Url
494    }
495
496    impl_get_multi_values! {
497        /// Get reference to all StringMap values
498        vec: get_string_maps, StringMap, HashMap<String, String>, DataType::StringMap
499    }
500
501    impl_get_multi_values! {
502        /// Get reference to all Json values
503        vec: get_jsons, Json, serde_json::Value, DataType::Json
504    }
505}