From 520d61df048de77fe3ac5d15cd18709150dfadc8 Mon Sep 17 00:00:00 2001 From: Panagiotis Date: Thu, 28 Nov 2024 12:25:04 +0000 Subject: [PATCH] impl `from_map` --- tailcall-template/src/jq/jq.rs | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/tailcall-template/src/jq/jq.rs b/tailcall-template/src/jq/jq.rs index b6551a9..a9a6390 100644 --- a/tailcall-template/src/jq/jq.rs +++ b/tailcall-template/src/jq/jq.rs @@ -1,14 +1,9 @@ use jaq_core::ValR; -use crate::jsonlike::JsonLike; +use crate::jsonlike::{JsonLike, JsonObjectLike}; #[derive(Clone, PartialEq, PartialOrd)] -pub struct JsonLikeHelper -where - A: for<'a> JsonLike<'a>, -{ - pub data: A, -} +pub struct JsonLikeHelper JsonLike<'a>>(pub A); impl jaq_core::ValT for JsonLikeHelper where @@ -16,13 +11,27 @@ where { fn from_num(n: &str) -> ValR { match n.parse::() { - Ok(num) => ValR::Ok(JsonLikeHelper { data: A::number_f64(num) }), + Ok(num) => ValR::Ok(JsonLikeHelper(A::number_f64(num))), Err(err) => ValR::Err(jaq_core::Error::str(format!("Invalid number format: {}", err.to_string()))), } } fn from_map>(iter: I) -> ValR { - todo!() + iter.into_iter().fold(ValR::Ok(Self(JsonLike::object(JsonObjectLike::new()))), |acc, (key, value)| { + let key = match JsonLike::as_str(&key.0) { + Some(key) => key, + None => return ValR::Err(jaq_core::Error::str("The value cannot be converted to String")), + }; + + match acc { + Ok(mut acc) => { + let acc_mut = JsonLike::as_object_mut(&mut acc.0).unwrap(); + acc_mut.insert_key(key, value.0); + ValR::Ok(acc) + }, + Err(err) => ValR::Err(err), + } + }) } fn values(self) -> Box>> {