Skip to main content

qubit_value/multi_values/
multi_values_constructor.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//! `From<T>` implementations for supported `MultiValues` input forms.
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 super::multi_values::MultiValues;
28
29/// Collects borrowed string values into owned strings.
30#[inline]
31fn collect_strings<'a, I>(values: I) -> Vec<String>
32where
33    I: IntoIterator<Item = &'a str>,
34{
35    let mut result = Vec::new();
36    for value in values {
37        result.push(value.to_string());
38    }
39    result
40}
41
42macro_rules! impl_multi_values_from {
43    ($type:ty, $variant:ident) => {
44        impl From<$type> for MultiValues {
45            #[inline]
46            fn from(value: $type) -> Self {
47                MultiValues::$variant(vec![value])
48            }
49        }
50
51        impl From<Vec<$type>> for MultiValues {
52            #[inline]
53            fn from(values: Vec<$type>) -> Self {
54                MultiValues::$variant(values)
55            }
56        }
57
58        impl From<&[$type]> for MultiValues
59        where
60            $type: Clone,
61        {
62            #[inline]
63            fn from(values: &[$type]) -> Self {
64                MultiValues::$variant(values.to_vec())
65            }
66        }
67
68        impl From<&Vec<$type>> for MultiValues
69        where
70            $type: Clone,
71        {
72            #[inline]
73            fn from(values: &Vec<$type>) -> Self {
74                MultiValues::$variant(values.clone())
75            }
76        }
77
78        impl<const N: usize> From<[$type; N]> for MultiValues {
79            #[inline]
80            fn from(values: [$type; N]) -> Self {
81                MultiValues::$variant(Vec::from(values))
82            }
83        }
84
85        impl<const N: usize> From<&[$type; N]> for MultiValues
86        where
87            $type: Clone,
88        {
89            #[inline]
90            fn from(values: &[$type; N]) -> Self {
91                MultiValues::$variant(values.to_vec())
92            }
93        }
94    };
95}
96
97impl_multi_values_from!(bool, Bool);
98impl_multi_values_from!(char, Char);
99impl_multi_values_from!(i8, Int8);
100impl_multi_values_from!(i16, Int16);
101impl_multi_values_from!(i32, Int32);
102impl_multi_values_from!(i64, Int64);
103impl_multi_values_from!(i128, Int128);
104impl_multi_values_from!(u8, UInt8);
105impl_multi_values_from!(u16, UInt16);
106impl_multi_values_from!(u32, UInt32);
107impl_multi_values_from!(u64, UInt64);
108impl_multi_values_from!(u128, UInt128);
109impl_multi_values_from!(isize, IntSize);
110impl_multi_values_from!(usize, UIntSize);
111impl_multi_values_from!(f32, Float32);
112impl_multi_values_from!(f64, Float64);
113impl_multi_values_from!(String, String);
114impl_multi_values_from!(NaiveDate, Date);
115impl_multi_values_from!(NaiveTime, Time);
116impl_multi_values_from!(NaiveDateTime, DateTime);
117impl_multi_values_from!(DateTime<Utc>, Instant);
118impl_multi_values_from!(BigInt, BigInteger);
119impl_multi_values_from!(BigDecimal, BigDecimal);
120impl_multi_values_from!(Duration, Duration);
121impl_multi_values_from!(Url, Url);
122impl_multi_values_from!(HashMap<String, String>, StringMap);
123impl_multi_values_from!(serde_json::Value, Json);
124
125impl From<&str> for MultiValues {
126    #[inline]
127    fn from(value: &str) -> Self {
128        MultiValues::String(vec![value.to_string()])
129    }
130}
131
132impl<'a> From<Vec<&'a str>> for MultiValues {
133    #[inline]
134    fn from(values: Vec<&'a str>) -> Self {
135        MultiValues::String(collect_strings(values))
136    }
137}
138
139impl<'a, 'b> From<&'a [&'b str]> for MultiValues {
140    #[inline]
141    fn from(values: &'a [&'b str]) -> Self {
142        MultiValues::String(collect_strings(values.iter().copied()))
143    }
144}
145
146impl<'a, 'b> From<&'a Vec<&'b str>> for MultiValues {
147    #[inline]
148    fn from(values: &'a Vec<&'b str>) -> Self {
149        MultiValues::String(collect_strings(values.iter().copied()))
150    }
151}
152
153impl<'a, const N: usize> From<[&'a str; N]> for MultiValues {
154    #[inline]
155    fn from(values: [&'a str; N]) -> Self {
156        MultiValues::String(collect_strings(values))
157    }
158}
159
160impl<'a, 'b, const N: usize> From<&'a [&'b str; N]> for MultiValues {
161    #[inline]
162    fn from(values: &'a [&'b str; N]) -> Self {
163        MultiValues::String(collect_strings(values.iter().copied()))
164    }
165}