vk_method/params/pairs_array.rs
1use super::Params;
2use serde::Serialize;
3
4/// Newtype for conversion array of pairs into [Params]
5///
6/// See also <https://stackoverflow.com/questions/63119000/why-am-i-required-to-cover-t-in-impl-foreigntraitlocaltype-for-t-e0210>
7///
8/// # Example
9/// ```
10/// use vk_method::{Params, PairsArray};
11/// use ijson::ijson;
12///
13/// let array = [("user_id", 1)];
14///
15/// let params = Params::try_from(PairsArray(array)).unwrap();
16///
17/// assert_eq!(
18/// params,
19/// Params(vec![("user_id".to_string(), ijson!(1))])
20/// );
21///```
22pub struct PairsArray<K, V, const N: usize>(pub [(K, V); N])
23where
24 K: ToString,
25 V: Serialize;
26
27impl<K, V, const N: usize> TryFrom<PairsArray<K, V, N>> for Params
28where
29 K: ToString,
30 V: Serialize
31{
32 type Error = serde_json::Error;
33
34 fn try_from(array: PairsArray<K, V, N>) -> Result<Self, Self::Error> {
35 let mut vector = Vec::with_capacity(N);
36
37 for (key, value) in array.0 {
38 vector.push((key.to_string(), ijson::to_value(value)?))
39 }
40
41 Ok(Params(vector))
42 }
43}