Skip to main content

qubit_value/multi_values/
multi_values_set_arg.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 dispatch implementations for `MultiValues::set<S>()` arguments.
12
13use super::multi_values::MultiValues;
14use crate::value_error::ValueResult;
15use std::collections::HashMap;
16use std::time::Duration;
17
18use bigdecimal::BigDecimal;
19use chrono::{
20    DateTime,
21    NaiveDate,
22    NaiveDateTime,
23    NaiveTime,
24    Utc,
25};
26use num_bigint::BigInt;
27use url::Url;
28
29use super::multi_values_setter::MultiValuesSetter;
30use super::multi_values_setter_slice::MultiValuesSetterSlice;
31use super::multi_values_single_setter::MultiValuesSingleSetter;
32
33/// Private marker trait used to prevent downstream implementations.
34trait MultiValuesSetArgSealed {}
35
36/// Internal dispatch trait for `MultiValues::set<S>()`.
37///
38/// Implementations route `Vec<T>`, `&[T]`, and `T` to the matching set path.
39#[allow(private_bounds)]
40pub trait MultiValuesSetArg<'a>: MultiValuesSetArgSealed {
41    /// Element type being set.
42    type Item: 'a + Clone;
43
44    /// Applies this argument to `target`.
45    ///
46    /// # Returns
47    ///
48    /// Returns `Ok(())` when the target is updated, or a `ValueError` from the
49    /// selected set path.
50    fn apply(self, target: &mut MultiValues) -> ValueResult<()>;
51}
52
53macro_rules! impl_multi_values_set_arg {
54    ($type:ty) => {
55        impl MultiValuesSetArgSealed for Vec<$type> {}
56
57        impl<'a> MultiValuesSetArg<'a> for Vec<$type> {
58            type Item = $type;
59
60            #[inline]
61            fn apply(self, target: &mut MultiValues) -> ValueResult<()> {
62                <MultiValues as MultiValuesSetter<$type>>::set_values(target, self)
63            }
64        }
65
66        impl<'a> MultiValuesSetArgSealed for &'a [$type] {}
67
68        impl<'a> MultiValuesSetArg<'a> for &'a [$type]
69        where
70            $type: Clone,
71        {
72            type Item = $type;
73
74            #[inline]
75            fn apply(self, target: &mut MultiValues) -> ValueResult<()> {
76                <MultiValues as MultiValuesSetterSlice<$type>>::set_values_slice(target, self)
77            }
78        }
79
80        impl<'a> MultiValuesSetArgSealed for &'a Vec<$type> {}
81
82        impl<'a> MultiValuesSetArg<'a> for &'a Vec<$type>
83        where
84            $type: Clone,
85        {
86            type Item = $type;
87
88            #[inline]
89            fn apply(self, target: &mut MultiValues) -> ValueResult<()> {
90                <MultiValues as MultiValuesSetterSlice<$type>>::set_values_slice(
91                    target,
92                    self.as_slice(),
93                )
94            }
95        }
96
97        impl<const N: usize> MultiValuesSetArgSealed for [$type; N] {}
98
99        impl<'a, const N: usize> MultiValuesSetArg<'a> for [$type; N] {
100            type Item = $type;
101
102            #[inline]
103            fn apply(self, target: &mut MultiValues) -> ValueResult<()> {
104                <MultiValues as MultiValuesSetter<$type>>::set_values(target, Vec::from(self))
105            }
106        }
107
108        impl<'a, const N: usize> MultiValuesSetArgSealed for &'a [$type; N] {}
109
110        impl<'a, const N: usize> MultiValuesSetArg<'a> for &'a [$type; N]
111        where
112            $type: Clone,
113        {
114            type Item = $type;
115
116            #[inline]
117            fn apply(self, target: &mut MultiValues) -> ValueResult<()> {
118                <MultiValues as MultiValuesSetterSlice<$type>>::set_values_slice(
119                    target,
120                    self.as_slice(),
121                )
122            }
123        }
124
125        impl MultiValuesSetArgSealed for $type {}
126
127        impl<'a> MultiValuesSetArg<'a> for $type {
128            type Item = $type;
129
130            #[inline]
131            fn apply(self, target: &mut MultiValues) -> ValueResult<()> {
132                <MultiValues as MultiValuesSingleSetter<$type>>::set_single_value(target, self)
133            }
134        }
135    };
136}
137
138impl_multi_values_set_arg!(bool);
139impl_multi_values_set_arg!(char);
140impl_multi_values_set_arg!(i8);
141impl_multi_values_set_arg!(i16);
142impl_multi_values_set_arg!(i32);
143impl_multi_values_set_arg!(i64);
144impl_multi_values_set_arg!(i128);
145impl_multi_values_set_arg!(u8);
146impl_multi_values_set_arg!(u16);
147impl_multi_values_set_arg!(u32);
148impl_multi_values_set_arg!(u64);
149impl_multi_values_set_arg!(u128);
150impl_multi_values_set_arg!(isize);
151impl_multi_values_set_arg!(usize);
152impl_multi_values_set_arg!(f32);
153impl_multi_values_set_arg!(f64);
154impl_multi_values_set_arg!(String);
155impl_multi_values_set_arg!(NaiveDate);
156impl_multi_values_set_arg!(NaiveTime);
157impl_multi_values_set_arg!(NaiveDateTime);
158impl_multi_values_set_arg!(DateTime<Utc>);
159impl_multi_values_set_arg!(BigInt);
160impl_multi_values_set_arg!(BigDecimal);
161impl_multi_values_set_arg!(Duration);
162impl_multi_values_set_arg!(Url);
163impl_multi_values_set_arg!(HashMap<String, String>);
164impl_multi_values_set_arg!(serde_json::Value);
165
166impl MultiValuesSetArgSealed for &str {}
167
168impl MultiValuesSetArg<'_> for &str {
169    type Item = String;
170
171    #[inline]
172    fn apply(self, target: &mut MultiValues) -> ValueResult<()> {
173        <MultiValues as MultiValuesSingleSetter<String>>::set_single_value(target, self.to_string())
174    }
175}
176
177impl MultiValuesSetArgSealed for Vec<&str> {}
178
179impl MultiValuesSetArg<'_> for Vec<&str> {
180    type Item = String;
181
182    #[inline]
183    fn apply(self, target: &mut MultiValues) -> ValueResult<()> {
184        let owned: Vec<String> = self.into_iter().map(|s| s.to_string()).collect();
185        <MultiValues as MultiValuesSetter<String>>::set_values(target, owned)
186    }
187}
188
189impl<'b> MultiValuesSetArgSealed for &'b [&'b str] {}
190
191impl<'b> MultiValuesSetArg<'b> for &'b [&'b str] {
192    type Item = String;
193
194    #[inline]
195    fn apply(self, target: &mut MultiValues) -> ValueResult<()> {
196        let owned: Vec<String> = self.iter().map(|s| (*s).to_string()).collect();
197        <MultiValues as MultiValuesSetter<String>>::set_values(target, owned)
198    }
199}
200
201impl MultiValuesSetArgSealed for &Vec<&str> {}
202
203impl MultiValuesSetArg<'_> for &Vec<&str> {
204    type Item = String;
205
206    #[inline]
207    fn apply(self, target: &mut MultiValues) -> ValueResult<()> {
208        let owned: Vec<String> = self.iter().map(|s| (*s).to_string()).collect();
209        <MultiValues as MultiValuesSetter<String>>::set_values(target, owned)
210    }
211}
212
213impl<const N: usize> MultiValuesSetArgSealed for [&str; N] {}
214
215impl<const N: usize> MultiValuesSetArg<'_> for [&str; N] {
216    type Item = String;
217
218    #[inline]
219    fn apply(self, target: &mut MultiValues) -> ValueResult<()> {
220        let owned: Vec<String> = self.into_iter().map(str::to_string).collect();
221        <MultiValues as MultiValuesSetter<String>>::set_values(target, owned)
222    }
223}
224
225impl<const N: usize> MultiValuesSetArgSealed for &[&str; N] {}
226
227impl<const N: usize> MultiValuesSetArg<'_> for &[&str; N] {
228    type Item = String;
229
230    #[inline]
231    fn apply(self, target: &mut MultiValues) -> ValueResult<()> {
232        let owned: Vec<String> = self.iter().map(|s| (*s).to_string()).collect();
233        <MultiValues as MultiValuesSetter<String>>::set_values(target, owned)
234    }
235}