File tree Expand file tree Collapse file tree 5 files changed +26
-7
lines changed Expand file tree Collapse file tree 5 files changed +26
-7
lines changed Original file line number Diff line number Diff line change @@ -14,23 +14,26 @@ impl<A> jaq_core::ValT for JsonLikeHelper<A>
1414where
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
Original file line number Diff line number Diff 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) ,
Original file line number Diff line number Diff line change @@ -2,7 +2,7 @@ use std::borrow::Cow;
22use std:: collections:: HashMap ;
33
44use async_graphql:: Name ;
5- use async_graphql_value:: ConstValue ;
5+ use async_graphql_value:: { ConstValue , Number } ;
66use indexmap:: IndexMap ;
77
88use 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}
Original file line number Diff line number Diff 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 > > ;
Original file line number Diff line number Diff line change 11use std:: borrow:: Cow ;
22use std:: collections:: HashMap ;
33
4+ use serde_json:: Number ;
5+
46use super :: { JsonLike , JsonObjectLike } ;
57
68impl < ' 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}
You can’t perform that action at this time.
0 commit comments