summaryrefslogtreecommitdiff
path: root/cli/ops/dispatch_json.rs
diff options
context:
space:
mode:
authorGurwinder Singh <vargwin@gmail.com>2020-07-14 23:52:02 +0530
committerGitHub <noreply@github.com>2020-07-14 14:22:02 -0400
commitf83d672ffad7afb1473bd4f9b9c645539064c620 (patch)
treef8a0b30ffd79965b7672c1c130af08dd798b750e /cli/ops/dispatch_json.rs
parent7be29fab8d4267bca9c8f0020f634fe5f1bd3caa (diff)
refactor: new trait JsonOpDispatcher (#6742)
Diffstat (limited to 'cli/ops/dispatch_json.rs')
-rw-r--r--cli/ops/dispatch_json.rs28
1 files changed, 25 insertions, 3 deletions
diff --git a/cli/ops/dispatch_json.rs b/cli/ops/dispatch_json.rs
index 30a3a4433..c3a1c3b3f 100644
--- a/cli/ops/dispatch_json.rs
+++ b/cli/ops/dispatch_json.rs
@@ -45,14 +45,36 @@ struct AsyncArgs {
promise_id: Option<u64>,
}
-pub fn json_op<D>(d: D) -> impl OpDispatcher
+/// Like OpDispatcher but with additional json `Value` parameter
+/// and return a result of `JsonOp` instead of `Op`.
+pub trait JsonOpDispatcher {
+ fn dispatch(
+ &self,
+ isolate_state: &mut CoreIsolateState,
+ json: Value,
+ zero_copy: &mut [ZeroCopyBuf],
+ ) -> Result<JsonOp, OpError>;
+}
+
+impl<F> JsonOpDispatcher for F
where
- D: Fn(
+ F: Fn(
&mut CoreIsolateState,
Value,
&mut [ZeroCopyBuf],
) -> Result<JsonOp, OpError>,
{
+ fn dispatch(
+ &self,
+ isolate_state: &mut CoreIsolateState,
+ json: Value,
+ zero_copy: &mut [ZeroCopyBuf],
+ ) -> Result<JsonOp, OpError> {
+ self(isolate_state, json, zero_copy)
+ }
+}
+
+pub fn json_op(d: impl JsonOpDispatcher) -> impl OpDispatcher {
move |isolate_state: &mut CoreIsolateState, zero_copy: &mut [ZeroCopyBuf]| {
assert!(!zero_copy.is_empty(), "Expected JSON string at position 0");
let async_args: AsyncArgs = match serde_json::from_slice(&zero_copy[0]) {
@@ -67,7 +89,7 @@ where
let result = serde_json::from_slice(&zero_copy[0])
.map_err(OpError::from)
- .and_then(|args| d(isolate_state, args, &mut zero_copy[1..]));
+ .and_then(|args| d.dispatch(isolate_state, args, &mut zero_copy[1..]));
// Convert to Op
match result {