Skip to content

Commit 6fb5d3a

Browse files
committed
impl from_num
1 parent b1b75cd commit 6fb5d3a

File tree

5 files changed

+26
-7
lines changed

5 files changed

+26
-7
lines changed

tailcall-template/src/jq/jq.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,26 @@ impl<A> jaq_core::ValT for JsonLikeHelper<A>
1414
where
1515
A: for<'a> JsonLike<'a> + Clone + PartialEq + PartialOrd,
1616
{
17-
fn from_num(n: &str) -> jaq_core::ValR<Self> {
18-
todo!()
17+
fn from_num(n: &str) -> ValR<Self> {
18+
match n.parse::<f64>() {
19+
Ok(num) => ValR::Ok(JsonLikeHelper { data: A::number_f64(num) }),
20+
Err(err) => ValR::Err(jaq_core::Error::str(format!("Invalid number format: {}", err.to_string()))),
21+
}
1922
}
2023

21-
fn from_map<I: IntoIterator<Item = (Self, Self)>>(iter: I) -> jaq_core::ValR<Self> {
24+
fn from_map<I: IntoIterator<Item = (Self, Self)>>(iter: I) -> ValR<Self> {
2225
todo!()
2326
}
2427

25-
fn values(self) -> Box<dyn Iterator<Item = jaq_core::ValR<Self>>> {
28+
fn values(self) -> Box<dyn Iterator<Item = ValR<Self>>> {
2629
todo!()
2730
}
2831

29-
fn index(self, index: &Self) -> jaq_core::ValR<Self> {
32+
fn index(self, index: &Self) -> ValR<Self> {
3033
todo!()
3134
}
3235

33-
fn range(self, range: jaq_core::val::Range<&Self>) -> jaq_core::ValR<Self> {
36+
fn range(self, range: jaq_core::val::Range<&Self>) -> ValR<Self> {
3437
todo!()
3538
}
3639

tailcall-template/src/jsonlike/borrow.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ impl<'ctx> JsonLike<'ctx> for Value<'ctx> {
4545
Value::Str(s)
4646
}
4747

48+
fn number_f64(n: f64) -> Self {
49+
Value::Number(n.into())
50+
}
51+
4852
fn as_array(&self) -> Option<&Vec<Self>> {
4953
match self {
5054
Value::Array(array) => Some(array),

tailcall-template/src/jsonlike/graphql.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::borrow::Cow;
22
use std::collections::HashMap;
33

44
use async_graphql::Name;
5-
use async_graphql_value::ConstValue;
5+
use async_graphql_value::{ConstValue, Number};
66
use indexmap::IndexMap;
77

88
use super::*;
@@ -153,4 +153,8 @@ impl<'json> JsonLike<'json> for ConstValue {
153153
fn string(s: Cow<'json, str>) -> Self {
154154
ConstValue::String(s.to_string())
155155
}
156+
157+
fn number_f64(n: f64) -> Self {
158+
ConstValue::Number(Number::from_f64(n).unwrap())
159+
}
156160
}

tailcall-template/src/jsonlike/json_like.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ pub trait JsonLike<'json>: Sized {
1313
fn object(obj: Self::JsonObject) -> Self;
1414
fn array(arr: Vec<Self>) -> Self;
1515
fn string(s: Cow<'json, str>) -> Self;
16+
// TODO: try from number
17+
fn number_f64(n: f64) -> Self;
1618

1719
// Operators
1820
fn as_array(&self) -> Option<&Vec<Self>>;

tailcall-template/src/jsonlike/serde.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use std::borrow::Cow;
22
use std::collections::HashMap;
33

4+
use serde_json::Number;
5+
46
use super::{JsonLike, JsonObjectLike};
57

68
impl<'obj> JsonObjectLike<'obj> for serde_json::Map<String, serde_json::Value> {
@@ -124,4 +126,8 @@ impl<'json> JsonLike<'json> for serde_json::Value {
124126
fn string(s: Cow<'json, str>) -> Self {
125127
serde_json::Value::String(s.to_string())
126128
}
129+
130+
fn number_f64(n: f64) -> Self {
131+
Self::Number(Number::from_f64(n).unwrap())
132+
}
127133
}

0 commit comments

Comments
 (0)