Skip to content

Commit 8eaf375

Browse files
committed
refactor json_to_array_buffer()
1 parent 0c89ee8 commit 8eaf375

File tree

2 files changed

+25
-44
lines changed

2 files changed

+25
-44
lines changed

packages/cubejs-backend-native/src/node_export.rs

Lines changed: 24 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,27 @@ fn debug_js_to_clrepr_to_js(mut cx: FunctionContext) -> JsResult<JsValue> {
514514

515515
//============ sql orchestrator ===================
516516

517+
fn json_to_array_buffer<'a, C>(
518+
mut cx: C,
519+
json_data: Result<String, anyhow::Error>,
520+
) -> JsResult<'a, JsArrayBuffer>
521+
where
522+
C: Context<'a>,
523+
{
524+
match json_data {
525+
Ok(json_data) => {
526+
let json_bytes = json_data.as_bytes();
527+
let mut js_buffer = cx.array_buffer(json_bytes.len())?;
528+
{
529+
let buffer = js_buffer.as_mut_slice(&mut cx);
530+
buffer.copy_from_slice(json_bytes);
531+
}
532+
Ok(js_buffer)
533+
}
534+
Err(err) => cx.throw_error(err.to_string()),
535+
}
536+
}
537+
517538
fn parse_cubestore_ws_result_message(mut cx: FunctionContext) -> JsResult<JsPromise> {
518539
let msg = cx.argument::<JsBuffer>(0)?;
519540
let msg_data = msg.as_slice(&cx).to_vec();
@@ -617,20 +638,7 @@ fn final_cubestore_result(mut cx: FunctionContext) -> JsResult<JsPromise> {
617638
Err(err) => Err(anyhow::Error::from(err)),
618639
}
619640
})
620-
.promise(move |mut cx, json_string| match json_string {
621-
Ok(json_string) => {
622-
let json_bytes = json_string.as_bytes();
623-
624-
let mut js_buffer = cx.array_buffer(json_bytes.len())?;
625-
{
626-
let buffer = js_buffer.as_mut_slice(&mut cx);
627-
buffer.copy_from_slice(json_bytes);
628-
}
629-
630-
Ok(js_buffer)
631-
}
632-
Err(err) => cx.throw_error(err.to_string()),
633-
});
641+
.promise(move |cx, json_data| json_to_array_buffer(cx, json_data));
634642

635643
Ok(promise)
636644
}
@@ -679,20 +687,7 @@ fn final_cubestore_result_array(mut cx: FunctionContext) -> JsResult<JsPromise>
679687
Err(err) => Err(anyhow::Error::from(err)),
680688
}
681689
})
682-
.promise(move |mut cx, json_data| match json_data {
683-
Ok(json_data) => {
684-
let json_bytes = json_data.as_bytes();
685-
686-
let mut js_buffer = cx.array_buffer(json_bytes.len())?;
687-
{
688-
let buffer = js_buffer.as_mut_slice(&mut cx);
689-
buffer.copy_from_slice(json_bytes);
690-
}
691-
692-
Ok(js_buffer)
693-
}
694-
Err(err) => cx.throw_error(err.to_string()),
695-
});
690+
.promise(move |cx, json_data| json_to_array_buffer(cx, json_data));
696691

697692
Ok(promise)
698693
}
@@ -733,20 +728,7 @@ fn final_cubestore_result_multi(mut cx: FunctionContext) -> JsResult<JsPromise>
733728
Err(err) => Err(anyhow::Error::from(err)),
734729
}
735730
})
736-
.promise(move |mut cx, json_data| match json_data {
737-
Ok(json_data) => {
738-
let json_bytes = json_data.as_bytes();
739-
740-
let mut js_buffer = cx.array_buffer(json_bytes.len())?;
741-
{
742-
let buffer = js_buffer.as_mut_slice(&mut cx);
743-
buffer.copy_from_slice(json_bytes);
744-
}
745-
746-
Ok(js_buffer)
747-
}
748-
Err(err) => cx.throw_error(err.to_string()),
749-
});
731+
.promise(move |cx, json_data| json_to_array_buffer(cx, json_data));
750732

751733
Ok(promise)
752734
}

packages/cubejs-backend-native/src/node_obj_deserializer.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,7 @@ impl<'de, 'a, 'b> Deserializer<'de> for JsValueDeserializer<'a, 'b> {
255255

256256
forward_to_deserialize_any! {
257257
bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string
258-
bytes byte_buf
259-
identifier ignored_any
258+
bytes byte_buf identifier ignored_any
260259
}
261260
}
262261

0 commit comments

Comments
 (0)