11#![ deny( clippy:: all) ]
22
3+ use cel_interpreter:: { Context , Program , Value } ;
34use napi_derive:: napi;
4- use cel_interpreter:: { Program , Context , Value } ;
55use serde_json:: Value as JsonValue ;
66use std:: sync:: Arc ;
77
@@ -19,8 +19,8 @@ impl CelProgram {
1919
2020 #[ napi]
2121 pub fn compile ( source : String ) -> napi:: Result < CelProgram > {
22- let program = Program :: compile ( & source )
23- . map_err ( |e| napi:: Error :: from_reason ( e. to_string ( ) ) ) ?;
22+ let program =
23+ Program :: compile ( & source ) . map_err ( |e| napi:: Error :: from_reason ( e. to_string ( ) ) ) ?;
2424 Ok ( CelProgram { program } )
2525 }
2626
@@ -38,12 +38,12 @@ impl CelProgram {
3838 } else {
3939 Value :: Null
4040 }
41- } ,
41+ }
4242 JsonValue :: String ( s) => Value :: String ( Arc :: new ( s. clone ( ) ) ) ,
4343 JsonValue :: Array ( arr) => {
4444 let values: Vec < Value > = arr. iter ( ) . map ( |v| Self :: json_to_cel_value ( v) ) . collect ( ) ;
4545 Value :: List ( Arc :: new ( values) )
46- } ,
46+ }
4747 JsonValue :: Object ( map) => {
4848 let mut cel_map = std:: collections:: HashMap :: new ( ) ;
4949 for ( key, value) in map {
@@ -65,27 +65,37 @@ impl CelProgram {
6565 . unwrap_or ( JsonValue :: Null ) ,
6666 Value :: String ( s) => JsonValue :: String ( ( * s) . to_string ( ) ) ,
6767 Value :: List ( list) => JsonValue :: Array (
68- list. iter ( ) . map ( |v| Self :: cel_to_json_value ( v. clone ( ) ) ) . collect ( )
68+ list. iter ( )
69+ . map ( |v| Self :: cel_to_json_value ( v. clone ( ) ) )
70+ . collect ( ) ,
6971 ) ,
7072 Value :: Map ( map) => JsonValue :: Object (
71- map. map . iter ( ) . map ( |( k, v) | ( k. to_string ( ) , Self :: cel_to_json_value ( v. clone ( ) ) ) ) . collect ( )
73+ map. map
74+ . iter ( )
75+ . map ( |( k, v) | ( k. to_string ( ) , Self :: cel_to_json_value ( v. clone ( ) ) ) )
76+ . collect ( ) ,
7277 ) ,
78+ Value :: Timestamp ( ts) => JsonValue :: String ( ts. to_rfc3339 ( ) ) ,
79+ Value :: Duration ( dur) => {
80+ JsonValue :: Number ( serde_json:: Number :: from ( dur. num_nanoseconds ( ) . unwrap_or ( 0 ) ) )
81+ }
7382 _ => JsonValue :: Null ,
7483 }
7584 }
7685
7786 #[ napi]
7887 pub fn execute ( & self , context : JsonValue ) -> napi:: Result < JsonValue > {
7988 let mut ctx = Context :: default ( ) ;
80-
89+
8190 if let JsonValue :: Object ( map) = context {
8291 for ( key, value) in map {
8392 let cel_value = Self :: json_to_cel_value ( & value) ;
8493 ctx. add_variable_from_value ( key, cel_value) ;
8594 }
8695 }
8796
88- self . program . execute ( & ctx)
97+ self . program
98+ . execute ( & ctx)
8999 . map_err ( |e| napi:: Error :: from_reason ( e. to_string ( ) ) )
90100 . map ( Self :: cel_to_json_value)
91101 }
0 commit comments