From 2aed322dd507a8568b6ee6f4897e9a8e3220f763 Mon Sep 17 00:00:00 2001 From: Aaron O'Mullan Date: Mon, 5 Apr 2021 18:40:24 +0200 Subject: refactor: convert ops to use serde_v8 (#10009) This commit rewrites most of the ops to use "serde_v8" instead of "json" serialization. --- op_crates/websocket/lib.rs | 34 +++++++++++----------------------- 1 file changed, 11 insertions(+), 23 deletions(-) (limited to 'op_crates/websocket/lib.rs') diff --git a/op_crates/websocket/lib.rs b/op_crates/websocket/lib.rs index 79ddbbee2..1e6eaafb7 100644 --- a/op_crates/websocket/lib.rs +++ b/op_crates/websocket/lib.rs @@ -82,28 +82,22 @@ impl Resource for WsStreamResource { impl WsStreamResource {} -#[derive(Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct CheckPermissionArgs { - url: String, -} - // This op is needed because creating a WS instance in JavaScript is a sync // operation and should throw error when permissions are not fulfilled, // but actual op that connects WS is async. pub fn op_ws_check_permission( state: &mut OpState, - args: CheckPermissionArgs, + url: String, _zero_copy: Option, -) -> Result +) -> Result<(), AnyError> where WP: WebSocketPermissions + 'static, { state .borrow::() - .check_net_url(&url::Url::parse(&args.url)?)?; + .check_net_url(&url::Url::parse(&url)?)?; - Ok(json!({})) + Ok(()) } #[derive(Deserialize)] @@ -224,7 +218,7 @@ pub async fn op_ws_send( state: Rc>, args: SendArgs, buf: Option, -) -> Result { +) -> Result<(), AnyError> { let msg = match args.kind.as_str() { "text" => Message::Text(args.text.unwrap()), "binary" => Message::Binary(buf.ok_or_else(null_opbuf)?.to_vec()), @@ -240,7 +234,7 @@ pub async fn op_ws_send( .ok_or_else(bad_resource_id)?; let mut tx = RcRef::map(&resource, |r| &r.tx).borrow_mut().await; tx.send(msg).await?; - Ok(json!({})) + Ok(()) } #[derive(Deserialize)] @@ -255,7 +249,7 @@ pub async fn op_ws_close( state: Rc>, args: CloseArgs, _bufs: Option, -) -> Result { +) -> Result<(), AnyError> { let rid = args.rid; let msg = Message::Close(args.code.map(|c| CloseFrame { code: CloseCode::from(c), @@ -272,24 +266,18 @@ pub async fn op_ws_close( .ok_or_else(bad_resource_id)?; let mut tx = RcRef::map(&resource, |r| &r.tx).borrow_mut().await; tx.send(msg).await?; - Ok(json!({})) -} - -#[derive(Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct NextEventArgs { - rid: ResourceId, + Ok(()) } pub async fn op_ws_next_event( state: Rc>, - args: NextEventArgs, + rid: ResourceId, _bufs: Option, ) -> Result { let resource = state .borrow_mut() .resource_table - .get::(args.rid) + .get::(rid) .ok_or_else(bad_resource_id)?; let mut rx = RcRef::map(&resource, |r| &r.rx).borrow_mut().await; @@ -325,7 +313,7 @@ pub async fn op_ws_next_event( Some(Ok(Message::Pong(_))) => json!({ "kind": "pong" }), Some(Err(_)) => json!({ "kind": "error" }), None => { - state.borrow_mut().resource_table.close(args.rid).unwrap(); + state.borrow_mut().resource_table.close(rid).unwrap(); json!({ "kind": "closed" }) } }; -- cgit v1.2.3