|
1 | 1 | use jaq_core::ValR; |
2 | 2 |
|
3 | | -use crate::jsonlike::JsonLike; |
| 3 | +use crate::jsonlike::{JsonLike, JsonObjectLike}; |
4 | 4 |
|
5 | 5 | #[derive(Clone, PartialEq, PartialOrd)] |
6 | | -pub struct JsonLikeHelper<A> |
7 | | -where |
8 | | - A: for<'a> JsonLike<'a>, |
9 | | -{ |
10 | | - pub data: A, |
11 | | -} |
| 6 | +pub struct JsonLikeHelper<A: for<'a> JsonLike<'a>>(pub A); |
12 | 7 |
|
13 | 8 | impl<A> jaq_core::ValT for JsonLikeHelper<A> |
14 | 9 | where |
15 | 10 | A: for<'a> JsonLike<'a> + Clone + PartialEq + PartialOrd, |
16 | 11 | { |
17 | 12 | fn from_num(n: &str) -> ValR<Self> { |
18 | 13 | match n.parse::<f64>() { |
19 | | - Ok(num) => ValR::Ok(JsonLikeHelper { data: A::number_f64(num) }), |
| 14 | + Ok(num) => ValR::Ok(JsonLikeHelper(A::number_f64(num))), |
20 | 15 | Err(err) => ValR::Err(jaq_core::Error::str(format!("Invalid number format: {}", err.to_string()))), |
21 | 16 | } |
22 | 17 | } |
23 | 18 |
|
24 | 19 | fn from_map<I: IntoIterator<Item = (Self, Self)>>(iter: I) -> ValR<Self> { |
25 | | - todo!() |
| 20 | + iter.into_iter().fold(ValR::Ok(Self(JsonLike::object(JsonObjectLike::new()))), |acc, (key, value)| { |
| 21 | + let key = match JsonLike::as_str(&key.0) { |
| 22 | + Some(key) => key, |
| 23 | + None => return ValR::Err(jaq_core::Error::str("The value cannot be converted to String")), |
| 24 | + }; |
| 25 | + |
| 26 | + match acc { |
| 27 | + Ok(mut acc) => { |
| 28 | + let acc_mut = JsonLike::as_object_mut(&mut acc.0).unwrap(); |
| 29 | + acc_mut.insert_key(key, value.0); |
| 30 | + ValR::Ok(acc) |
| 31 | + }, |
| 32 | + Err(err) => ValR::Err(err), |
| 33 | + } |
| 34 | + }) |
26 | 35 | } |
27 | 36 |
|
28 | 37 | fn values(self) -> Box<dyn Iterator<Item = ValR<Self>>> { |
|
0 commit comments