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