surrealpatch/lib/src/fnc/args.rs
Tobie Morgan Hitchcock 1017e2fffb Don’t clone variables when processing sub-contexts
Closes SUR-53

When creating a new context for subqueries or statement clauses, we used to have to clone any variables/values, and freeze the context, so that it could be used across threads and async boundaries.

Now with the new executor pattern for parallel queries, we can pass references instead, improving performance by removing unnecessary cloning of values.
2022-05-14 13:38:17 +01:00

83 lines
2 KiB
Rust

use crate::ctx::Context;
use crate::err::Error;
use crate::sql::value::Value;
pub enum Args {
None,
Any,
One,
Two,
Three,
NoneOne,
NoneTwo,
NoneOneTwo,
OneTwo,
}
pub fn check(
ctx: &Context,
name: &str,
args: Vec<Value>,
size: Args,
func: fn(&Context, Vec<Value>) -> Result<Value, Error>,
) -> Result<Value, Error> {
match size {
Args::None => match args.len() {
0 => func(ctx, args),
_ => Err(Error::InvalidArguments {
name: name.to_owned(),
message: String::from("The function does not expect any arguments."),
}),
},
Args::One => match args.len() {
1 => func(ctx, args),
_ => Err(Error::InvalidArguments {
name: name.to_owned(),
message: String::from("The function expects 1 argument."),
}),
},
Args::Two => match args.len() {
2 => func(ctx, args),
_ => Err(Error::InvalidArguments {
name: name.to_owned(),
message: String::from("The function expects 2 arguments."),
}),
},
Args::Three => match args.len() {
3 => func(ctx, args),
_ => Err(Error::InvalidArguments {
name: name.to_owned(),
message: String::from("The function expects 3 arguments."),
}),
},
Args::NoneOne => match args.len() {
0 | 1 => func(ctx, args),
_ => Err(Error::InvalidArguments {
name: name.to_owned(),
message: String::from("The function expects 0 or 1 arguments."),
}),
},
Args::NoneTwo => match args.len() {
0 | 2 => func(ctx, args),
_ => Err(Error::InvalidArguments {
name: name.to_owned(),
message: String::from("The function expects 0 or 2 arguments."),
}),
},
Args::NoneOneTwo => match args.len() {
0 | 1 | 2 => func(ctx, args),
_ => Err(Error::InvalidArguments {
name: name.to_owned(),
message: String::from("The function expects 0, 1, or 2 arguments."),
}),
},
Args::OneTwo => match args.len() {
1 | 2 => func(ctx, args),
_ => Err(Error::InvalidArguments {
name: name.to_owned(),
message: String::from("The function expects 1 or 2 arguments."),
}),
},
Args::Any => func(ctx, args),
}
}