Skip to content

Commit

Permalink
impl from_map
Browse files Browse the repository at this point in the history
  • Loading branch information
karatakis committed Nov 28, 2024
1 parent 6fb5d3a commit 520d61d
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions tailcall-template/src/jq/jq.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,37 @@
use jaq_core::ValR;

use crate::jsonlike::JsonLike;
use crate::jsonlike::{JsonLike, JsonObjectLike};

#[derive(Clone, PartialEq, PartialOrd)]
pub struct JsonLikeHelper<A>
where
A: for<'a> JsonLike<'a>,
{
pub data: A,
}
pub struct JsonLikeHelper<A: for<'a> JsonLike<'a>>(pub A);

impl<A> jaq_core::ValT for JsonLikeHelper<A>
where
A: for<'a> JsonLike<'a> + Clone + PartialEq + PartialOrd,
{
fn from_num(n: &str) -> ValR<Self> {
match n.parse::<f64>() {
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<I: IntoIterator<Item = (Self, Self)>>(iter: I) -> ValR<Self> {
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<dyn Iterator<Item = ValR<Self>>> {
Expand Down

0 comments on commit 520d61d

Please sign in to comment.