@@ -3,11 +3,7 @@ use crate::flow::DateTime;
33use crate :: flow:: FlowError ;
44use crate :: flow:: Message ;
55use crate :: js_runtime:: JsRuntime ;
6- use anyhow:: Context ;
7- use rquickjs:: Ctx ;
8- use rquickjs:: FromJs ;
9- use rquickjs:: IntoJs ;
10- use rquickjs:: Value ;
6+ use crate :: js_value:: JsonValue ;
117use std:: path:: Path ;
128use std:: path:: PathBuf ;
139use tracing:: debug;
@@ -23,15 +19,6 @@ pub struct JsScript {
2319 pub no_js_on_interval_fun : bool ,
2420}
2521
26- #[ derive( Clone , Debug ) ]
27- pub struct JsonValue ( serde_json:: Value ) ;
28-
29- impl Default for JsonValue {
30- fn default ( ) -> Self {
31- JsonValue ( serde_json:: Value :: Object ( Default :: default ( ) ) )
32- }
33- }
34-
3522impl JsScript {
3623 pub fn new ( flow : PathBuf , index : usize , path : PathBuf ) -> Self {
3724 let module_name = format ! ( "{}|{}|{}" , flow. display( ) , index, path. display( ) ) ;
@@ -159,158 +146,6 @@ impl JsScript {
159146 }
160147}
161148
162- impl From < Message > for JsonValue {
163- fn from ( value : Message ) -> Self {
164- JsonValue ( value. json ( ) )
165- }
166- }
167-
168- impl From < DateTime > for JsonValue {
169- fn from ( value : DateTime ) -> Self {
170- JsonValue ( value. json ( ) )
171- }
172- }
173-
174- impl TryFrom < serde_json:: Value > for Message {
175- type Error = FlowError ;
176-
177- fn try_from ( value : serde_json:: Value ) -> Result < Self , Self :: Error > {
178- let message = serde_json:: from_value ( value)
179- . with_context ( || "Couldn't extract message payload and topic" ) ?;
180- Ok ( message)
181- }
182- }
183-
184- impl TryFrom < JsonValue > for Message {
185- type Error = FlowError ;
186-
187- fn try_from ( value : JsonValue ) -> Result < Self , Self :: Error > {
188- Message :: try_from ( value. 0 )
189- }
190- }
191-
192- impl TryFrom < JsonValue > for Vec < Message > {
193- type Error = FlowError ;
194-
195- fn try_from ( value : JsonValue ) -> Result < Self , Self :: Error > {
196- match value. 0 {
197- serde_json:: Value :: Array ( array) => array. into_iter ( ) . map ( Message :: try_from) . collect ( ) ,
198- serde_json:: Value :: Object ( map) => {
199- Message :: try_from ( serde_json:: Value :: Object ( map) ) . map ( |message| vec ! [ message] )
200- }
201- serde_json:: Value :: Null => Ok ( vec ! [ ] ) ,
202- _ => Err (
203- anyhow:: anyhow!( "Flow scripts are expected to return an array of messages" ) . into ( ) ,
204- ) ,
205- }
206- }
207- }
208-
209- struct JsonValueRef < ' a > ( & ' a serde_json:: Value ) ;
210-
211- impl < ' js > IntoJs < ' js > for JsonValue {
212- fn into_js ( self , ctx : & Ctx < ' js > ) -> rquickjs:: Result < Value < ' js > > {
213- JsonValueRef ( & self . 0 ) . into_js ( ctx)
214- }
215- }
216-
217- impl < ' js > IntoJs < ' js > for & JsonValue {
218- fn into_js ( self , ctx : & Ctx < ' js > ) -> rquickjs:: Result < Value < ' js > > {
219- JsonValueRef ( & self . 0 ) . into_js ( ctx)
220- }
221- }
222-
223- impl < ' js > IntoJs < ' js > for JsonValueRef < ' _ > {
224- fn into_js ( self , ctx : & Ctx < ' js > ) -> rquickjs:: Result < Value < ' js > > {
225- match self . 0 {
226- serde_json:: Value :: Null => Ok ( Value :: new_null ( ctx. clone ( ) ) ) ,
227- serde_json:: Value :: Bool ( value) => Ok ( Value :: new_bool ( ctx. clone ( ) , * value) ) ,
228- serde_json:: Value :: Number ( value) => {
229- if let Some ( n) = value. as_i64 ( ) {
230- if let Ok ( n) = i32:: try_from ( n) {
231- return Ok ( Value :: new_int ( ctx. clone ( ) , n) ) ;
232- }
233- }
234- if let Some ( f) = value. as_f64 ( ) {
235- return Ok ( Value :: new_float ( ctx. clone ( ) , f) ) ;
236- }
237- let nan = rquickjs:: String :: from_str ( ctx. clone ( ) , "NaN" ) ?;
238- Ok ( nan. into_value ( ) )
239- }
240- serde_json:: Value :: String ( value) => {
241- let string = rquickjs:: String :: from_str ( ctx. clone ( ) , value) ?;
242- Ok ( string. into_value ( ) )
243- }
244- serde_json:: Value :: Array ( values) => {
245- let array = rquickjs:: Array :: new ( ctx. clone ( ) ) ?;
246- for ( i, value) in values. iter ( ) . enumerate ( ) {
247- array. set ( i, JsonValueRef ( value) ) ?;
248- }
249- Ok ( array. into_value ( ) )
250- }
251- serde_json:: Value :: Object ( values) => {
252- let object = rquickjs:: Object :: new ( ctx. clone ( ) ) ?;
253- for ( key, value) in values. into_iter ( ) {
254- object. set ( key, JsonValueRef ( value) ) ?;
255- }
256- Ok ( object. into_value ( ) )
257- }
258- }
259- }
260- }
261-
262- impl < ' js > FromJs < ' js > for JsonValue {
263- fn from_js ( _ctx : & Ctx < ' js > , value : Value < ' js > ) -> rquickjs:: Result < Self > {
264- JsonValue :: from_js_value ( value)
265- }
266- }
267-
268- impl JsonValue {
269- fn from_js_value ( value : Value < ' _ > ) -> rquickjs:: Result < Self > {
270- if let Some ( promise) = value. as_promise ( ) {
271- // Beware checking the value is a promise must be done first
272- // as a promise can also be used as an object
273- return promise. finish ( ) ;
274- }
275- if let Some ( b) = value. as_bool ( ) {
276- return Ok ( JsonValue ( serde_json:: Value :: Bool ( b) ) ) ;
277- }
278- if let Some ( n) = value. as_int ( ) {
279- return Ok ( JsonValue ( serde_json:: Value :: Number ( n. into ( ) ) ) ) ;
280- }
281- if let Some ( n) = value. as_float ( ) {
282- let js_n = serde_json:: Number :: from_f64 ( n)
283- . map ( serde_json:: Value :: Number )
284- . unwrap_or ( serde_json:: Value :: Null ) ;
285- return Ok ( JsonValue ( js_n) ) ;
286- }
287- if let Some ( string) = value. as_string ( ) {
288- return Ok ( JsonValue ( serde_json:: Value :: String ( string. to_string ( ) ?) ) ) ;
289- }
290- if let Some ( array) = value. as_array ( ) {
291- let array: rquickjs:: Result < Vec < JsonValue > > = array. iter ( ) . collect ( ) ;
292- let array = array?. into_iter ( ) . map ( |v| v. 0 ) . collect ( ) ;
293- return Ok ( JsonValue ( serde_json:: Value :: Array ( array) ) ) ;
294- }
295- if let Some ( object) = value. as_object ( ) {
296- let mut js_object = serde_json:: Map :: new ( ) ;
297- for key in object. keys :: < String > ( ) . flatten ( ) {
298- if let Ok ( JsonValue ( v) ) = object. get ( & key) {
299- js_object. insert ( key, v. clone ( ) ) ;
300- }
301- }
302- return Ok ( JsonValue ( serde_json:: Value :: Object ( js_object) ) ) ;
303- }
304-
305- Ok ( JsonValue ( serde_json:: Value :: Null ) )
306- }
307-
308- pub ( crate ) fn display ( value : Value < ' _ > ) -> String {
309- let json = JsonValue :: from_js_value ( value) . unwrap_or_default ( ) ;
310- serde_json:: to_string_pretty ( & json. 0 ) . unwrap ( )
311- }
312- }
313-
314149#[ cfg( test) ]
315150mod tests {
316151 use super :: * ;
0 commit comments