From b3fe85163f303a1592335b23c25554dd0e39a4c4 Mon Sep 17 00:00:00 2001 From: crowlKats <13135287+crowlKats@users.noreply.github.com> Date: Wed, 17 Mar 2021 22:33:29 +0100 Subject: refactor: use serde ops more (#9817) --- op_crates/fetch/lib.rs | 93 +++++++++++++++++++++++--------------------------- 1 file changed, 42 insertions(+), 51 deletions(-) (limited to 'op_crates/fetch/lib.rs') diff --git a/op_crates/fetch/lib.rs b/op_crates/fetch/lib.rs index 456358b83..c37670852 100644 --- a/op_crates/fetch/lib.rs +++ b/op_crates/fetch/lib.rs @@ -9,7 +9,6 @@ use deno_core::error::AnyError; use deno_core::futures::Future; use deno_core::futures::Stream; use deno_core::futures::StreamExt; -use deno_core::serde_json; use deno_core::serde_json::json; use deno_core::serde_json::Value; use deno_core::url::Url; @@ -103,27 +102,25 @@ pub fn get_declaration() -> PathBuf { PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("lib.deno_fetch.d.ts") } +#[derive(Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct FetchArgs { + method: Option, + url: String, + base_url: Option, + headers: Vec<(String, String)>, + client_rid: Option, + has_body: bool, +} + pub fn op_fetch( state: &mut OpState, - args: Value, + args: FetchArgs, data: &mut [ZeroCopyBuf], ) -> Result where FP: FetchPermissions + 'static, { - #[derive(Deserialize)] - #[serde(rename_all = "camelCase")] - struct FetchArgs { - method: Option, - url: String, - base_url: Option, - headers: Vec<(String, String)>, - client_rid: Option, - has_body: bool, - } - - let args: FetchArgs = serde_json::from_value(args)?; - let client = if let Some(rid) = args.client_rid { let r = state .resource_table @@ -203,19 +200,17 @@ where })) } +#[derive(Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct FetchSendArgs { + rid: u32, +} + pub async fn op_fetch_send( state: Rc>, - args: Value, + args: FetchSendArgs, _data: BufVec, ) -> Result { - #[derive(Deserialize)] - #[serde(rename_all = "camelCase")] - struct Args { - rid: u32, - } - - let args: Args = serde_json::from_value(args)?; - let request = state .borrow_mut() .resource_table @@ -273,18 +268,17 @@ pub async fn op_fetch_send( })) } +#[derive(Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct FetchRequestWriteArgs { + rid: u32, +} + pub async fn op_fetch_request_write( state: Rc>, - args: Value, + args: FetchRequestWriteArgs, data: BufVec, ) -> Result { - #[derive(Deserialize)] - #[serde(rename_all = "camelCase")] - struct Args { - rid: u32, - } - - let args: Args = serde_json::from_value(args)?; let rid = args.rid; let buf = match data.len() { @@ -304,18 +298,17 @@ pub async fn op_fetch_request_write( Ok(json!({})) } +#[derive(Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct FetchResponseReadArgs { + rid: u32, +} + pub async fn op_fetch_response_read( state: Rc>, - args: Value, + args: FetchResponseReadArgs, data: BufVec, ) -> Result { - #[derive(Deserialize)] - #[serde(rename_all = "camelCase")] - struct Args { - rid: u32, - } - - let args: Args = serde_json::from_value(args)?; let rid = args.rid; if data.len() != 1 { @@ -385,24 +378,22 @@ impl HttpClientResource { } } +#[derive(Deserialize, Default, Debug)] +#[serde(rename_all = "camelCase")] +#[serde(default)] +pub struct CreateHttpClientOptions { + ca_file: Option, + ca_data: Option, +} + pub fn op_create_http_client( state: &mut OpState, - args: Value, + args: CreateHttpClientOptions, _zero_copy: &mut [ZeroCopyBuf], ) -> Result where FP: FetchPermissions + 'static, { - #[derive(Deserialize, Default, Debug)] - #[serde(rename_all = "camelCase")] - #[serde(default)] - struct CreateHttpClientOptions { - ca_file: Option, - ca_data: Option, - } - - let args: CreateHttpClientOptions = serde_json::from_value(args)?; - if let Some(ca_file) = args.ca_file.clone() { let permissions = state.borrow::(); permissions.check_read(&PathBuf::from(ca_file))?; -- cgit v1.2.3