@@ -2,6 +2,8 @@ cfg_if::cfg_if! { if #[cfg(feature = "bindings_wasm")] {
22 use wasm_bindgen:: prelude:: * ;
33 use js_sys:: { Object , Reflect } ;
44 use crate :: timeframe_js:: { JsTimeframe } ;
5+ use js_sys:: Float64Array ;
6+ use js_sys:: Array ;
57} }
68use crate :: ohlcv:: { zip_ohlcv_bars, Ohlcv } ;
79use crate :: timeframe:: Timeframe ;
@@ -16,6 +18,16 @@ use std::rc::Rc;
1618use std:: sync:: { Arc , RwLock } ;
1719use std:: { ffi:: OsStr , ops:: Range , path:: Path } ;
1820
21+ #[ cfg( feature = "bindings_wasm" ) ]
22+ #[ wasm_bindgen( raw_module = "../../lib/internal.js" ) ]
23+ extern "C" {
24+ #[ wasm_bindgen( js_name = readOhlcvBarsFromPath) ]
25+ fn js_read_ohlcv_bars_from_path ( format : & str , path : & str , time_unit : & str ) -> Vec < OhlcvBar > ;
26+
27+ #[ wasm_bindgen( js_name = writeOhlcvBarsToPath) ]
28+ fn js_write_ohlcv_bars_to_path ( format : & str , path : & str , bars : Vec < OhlcvBar > ) ;
29+ }
30+
1931#[ cfg( feature = "bindings_wasm" ) ]
2032#[ wasm_bindgen( js_class=OhlcvBar ) ]
2133impl OhlcvBar {
@@ -127,12 +139,12 @@ impl OhlcvBar {
127139 let obj = Object :: new ( ) ;
128140 let _ = Reflect :: set (
129141 & obj,
130- & "openTime " . into ( ) ,
142+ & "open_time " . into ( ) ,
131143 & JsValue :: from ( self . js_open_time ( ) ) ,
132144 ) ;
133145 let _ = Reflect :: set (
134146 & obj,
135- & "closeTime " . into ( ) ,
147+ & "close_time " . into ( ) ,
136148 & JsValue :: from ( self . js_close_time ( ) ) ,
137149 ) ;
138150 let _ = Reflect :: set ( & obj, & "open" . into ( ) , & JsValue :: from ( self . js_open ( ) ) ) ;
@@ -142,6 +154,43 @@ impl OhlcvBar {
142154 let _ = Reflect :: set ( & obj, & "volume" . into ( ) , & JsValue :: from ( self . js_volume ( ) ) ) ;
143155 obj. into ( )
144156 }
157+
158+ #[ wasm_bindgen( js_name = "fromJSON" ) ]
159+ pub fn js_from_json ( json : JsValue ) -> Self {
160+ let obj = json. unchecked_into :: < Object > ( ) ;
161+ let open_time = Reflect :: get ( & obj, & "open_time" . into ( ) )
162+ . unwrap ( )
163+ . unchecked_into :: < js_sys:: Date > ( ) ;
164+ let close_time = Reflect :: get ( & obj, & "close_time" . into ( ) )
165+ . unwrap ( )
166+ . unchecked_into :: < js_sys:: Date > ( ) ;
167+ let open = Reflect :: get ( & obj, & "open" . into ( ) )
168+ . unwrap ( )
169+ . as_f64 ( )
170+ . unwrap ( ) ;
171+ let high = Reflect :: get ( & obj, & "high" . into ( ) )
172+ . unwrap ( )
173+ . as_f64 ( )
174+ . unwrap ( ) ;
175+ let low = Reflect :: get ( & obj, & "low" . into ( ) ) . unwrap ( ) . as_f64 ( ) . unwrap ( ) ;
176+ let close = Reflect :: get ( & obj, & "close" . into ( ) )
177+ . unwrap ( )
178+ . as_f64 ( )
179+ . unwrap ( ) ;
180+ let volume = Reflect :: get ( & obj, & "volume" . into ( ) )
181+ . unwrap ( )
182+ . as_f64 ( )
183+ . unwrap ( ) ;
184+ Self :: new (
185+ open_time. into ( ) ,
186+ close_time. into ( ) ,
187+ open,
188+ high,
189+ low,
190+ close,
191+ volume,
192+ )
193+ }
145194}
146195
147196#[ cfg( feature = "bindings_wasm" ) ]
@@ -343,4 +392,36 @@ impl JsOhlcv {
343392 pub fn js_add_many ( & mut self , bars : Vec < OhlcvBar > ) {
344393 self . push_many ( & bars) ;
345394 }
395+
396+ #[ wasm_bindgen( js_name = "readCSV" ) ]
397+ #[ inline]
398+ #[ doc = "`time_unit: 'ms' | 's`. Default: 'ms'" ]
399+ pub fn js_read_csv ( path : & str , time_unit : Option < String > ) -> JsOhlcv {
400+ let time_unit = time_unit. unwrap_or ( "ms" . to_string ( ) ) ;
401+ let bars = js_read_ohlcv_bars_from_path ( "csv" , path, & time_unit) ;
402+ Ohlcv :: from_bars ( bars) . into ( )
403+ }
404+
405+ #[ wasm_bindgen( js_name = "readParquet" ) ]
406+ #[ inline]
407+ #[ doc = "`time_unit: 'ms' | 's`. Default: 'ms'" ]
408+ pub fn js_read_parquet ( path : & str , time_unit : Option < String > ) -> JsOhlcv {
409+ let time_unit = time_unit. unwrap_or ( "ms" . to_string ( ) ) ;
410+ let bars = js_read_ohlcv_bars_from_path ( "parquet" , path, & time_unit) ;
411+ Ohlcv :: from_bars ( bars) . into ( )
412+ }
413+
414+ #[ wasm_bindgen( js_name = "writeCSV" ) ]
415+ #[ inline]
416+ pub fn js_write_csv ( & self , path : & str ) {
417+ let bars = self . all_bars ( ) ;
418+ js_write_ohlcv_bars_to_path ( "csv" , path, bars. to_vec ( ) ) ;
419+ }
420+
421+ #[ wasm_bindgen( js_name = "writeParquet" ) ]
422+ #[ inline]
423+ pub fn js_write_parquet ( & self , path : & str ) {
424+ let bars = self . all_bars ( ) ;
425+ js_write_ohlcv_bars_to_path ( "parquet" , path, bars. to_vec ( ) ) ;
426+ }
346427}
0 commit comments