From f8848e36f506dbd9516a552c183cce5853861e5f Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Wed, 19 Jan 2022 12:51:28 +0000 Subject: [PATCH] Ensure functions can be run asynchronously --- src/fnc/args.rs | 8 ------ src/fnc/http.rs | 60 ++++++++++++++++++++++++++++++++++++--------- src/fnc/mod.rs | 14 +++++------ src/sql/function.rs | 2 +- 4 files changed, 56 insertions(+), 28 deletions(-) diff --git a/src/fnc/args.rs b/src/fnc/args.rs index a4b6346a..3560308d 100644 --- a/src/fnc/args.rs +++ b/src/fnc/args.rs @@ -12,7 +12,6 @@ pub enum Args { NoneTwo, NoneOneTwo, OneTwo, - OneTwoThree, } pub fn check( @@ -79,13 +78,6 @@ pub fn check( message: String::from("The function expects 1 or 2 arguments."), }), }, - Args::OneTwoThree => match args.len() { - 1 | 2 | 3 => func(ctx, args), - _ => Err(Error::ArgumentsError { - name: name.to_owned(), - message: String::from("The function expects 1, 2, or 3 arguments."), - }), - }, Args::Any => func(ctx, args), } } diff --git a/src/fnc/http.rs b/src/fnc/http.rs index 7b151a50..3f26b3ad 100644 --- a/src/fnc/http.rs +++ b/src/fnc/http.rs @@ -2,26 +2,62 @@ use crate::dbs::Runtime; use crate::err::Error; use crate::sql::value::Value; -pub fn head(_ctx: &Runtime, _args: Vec) -> Result { - todo!() +pub async fn head(_ctx: &Runtime, args: Vec) -> Result { + match args.len() { + 1 | 2 => todo!(), + _ => Err(Error::ArgumentsError { + name: String::from("http::head"), + message: String::from("The function expects 1 or 2 arguments."), + }), + } } -pub fn get(_ctx: &Runtime, _args: Vec) -> Result { - todo!() +pub async fn get(_ctx: &Runtime, args: Vec) -> Result { + match args.len() { + 1 | 2 => todo!(), + _ => Err(Error::ArgumentsError { + name: String::from("http::get"), + message: String::from("The function expects 1 or 2 arguments."), + }), + } } -pub fn put(_ctx: &Runtime, _args: Vec) -> Result { - todo!() +pub async fn put(_ctx: &Runtime, args: Vec) -> Result { + match args.len() { + 1 | 2 | 3 => todo!(), + _ => Err(Error::ArgumentsError { + name: String::from("http::put"), + message: String::from("The function expects 1, 2, or 3 arguments."), + }), + } } -pub fn post(_ctx: &Runtime, _args: Vec) -> Result { - todo!() +pub async fn post(_ctx: &Runtime, args: Vec) -> Result { + match args.len() { + 1 | 2 | 3 => todo!(), + _ => Err(Error::ArgumentsError { + name: String::from("http::post"), + message: String::from("The function expects 1, 2, or 3 arguments."), + }), + } } -pub fn patch(_ctx: &Runtime, _args: Vec) -> Result { - todo!() +pub async fn patch(_ctx: &Runtime, args: Vec) -> Result { + match args.len() { + 1 | 2 | 3 => todo!(), + _ => Err(Error::ArgumentsError { + name: String::from("http::patch"), + message: String::from("The function expects 1, 2, or 3 arguments."), + }), + } } -pub fn delete(_ctx: &Runtime, _args: Vec) -> Result { - todo!() +pub async fn delete(_ctx: &Runtime, args: Vec) -> Result { + match args.len() { + 1 | 2 => todo!(), + _ => Err(Error::ArgumentsError { + name: String::from("http::delete"), + message: String::from("The function expects 1 or 2 arguments."), + }), + } } diff --git a/src/fnc/mod.rs b/src/fnc/mod.rs index 089430f6..89ffff41 100644 --- a/src/fnc/mod.rs +++ b/src/fnc/mod.rs @@ -22,7 +22,7 @@ pub mod time; pub mod r#type; pub mod util; -pub fn run(ctx: &Runtime, name: &String, args: Vec) -> Result { +pub async fn run(ctx: &Runtime, name: &String, args: Vec) -> Result { match name.as_ref() { // "array::combine" => args::check(ctx, name, args, Args::Two, array::combine), @@ -55,12 +55,12 @@ pub fn run(ctx: &Runtime, name: &String, args: Vec) -> Result args::check(ctx, name, args, Args::One, geo::hash::decode), "geo::hash::encode" => args::check(ctx, name, args, Args::OneTwo, geo::hash::encode), // - "http::head" => args::check(ctx, name, args, Args::OneTwo, http::head), - "http::get" => args::check(ctx, name, args, Args::OneTwo, http::get), - "http::put" => args::check(ctx, name, args, Args::OneTwoThree, http::put), - "http::post" => args::check(ctx, name, args, Args::OneTwoThree, http::post), - "http::patch" => args::check(ctx, name, args, Args::OneTwoThree, http::patch), - "http::delete" => args::check(ctx, name, args, Args::OneTwo, http::delete), + "http::head" => http::head(ctx, args).await, + "http::get" => http::get(ctx, args).await, + "http::put" => http::put(ctx, args).await, + "http::post" => http::post(ctx, args).await, + "http::patch" => http::patch(ctx, args).await, + "http::delete" => http::delete(ctx, args).await, // "is::alphanum" => args::check(ctx, name, args, Args::One, is::alphanum), "is::alpha" => args::check(ctx, name, args, Args::One, is::alpha), diff --git a/src/sql/function.rs b/src/sql/function.rs index 7b3d4598..181bada4 100644 --- a/src/sql/function.rs +++ b/src/sql/function.rs @@ -60,7 +60,7 @@ impl Function { let v = v.compute(ctx, opt, exe, doc).await?; a.push(v); } - fnc::run(ctx, s, a) + fnc::run(ctx, s, a).await } } }