2022-05-14 12:35:08 +00:00
|
|
|
use crate::ctx::Context;
|
2022-01-13 17:37:46 +00:00
|
|
|
use crate::err::Error;
|
|
|
|
use crate::sql::value::Value;
|
|
|
|
|
|
|
|
pub enum Args {
|
|
|
|
None,
|
|
|
|
Any,
|
|
|
|
One,
|
|
|
|
Two,
|
|
|
|
Three,
|
|
|
|
NoneOne,
|
|
|
|
NoneTwo,
|
|
|
|
NoneOneTwo,
|
|
|
|
OneTwo,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn check(
|
2022-05-14 12:35:08 +00:00
|
|
|
ctx: &Context,
|
2022-03-04 16:01:32 +00:00
|
|
|
name: &str,
|
2022-01-13 17:37:46 +00:00
|
|
|
args: Vec<Value>,
|
|
|
|
size: Args,
|
2022-05-14 12:35:08 +00:00
|
|
|
func: fn(&Context, Vec<Value>) -> Result<Value, Error>,
|
2022-01-13 17:37:46 +00:00
|
|
|
) -> Result<Value, Error> {
|
|
|
|
match size {
|
|
|
|
Args::None => match args.len() {
|
|
|
|
0 => func(ctx, args),
|
2022-03-06 10:58:59 +00:00
|
|
|
_ => Err(Error::InvalidArguments {
|
2022-01-13 17:37:46 +00:00
|
|
|
name: name.to_owned(),
|
|
|
|
message: String::from("The function does not expect any arguments."),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
Args::One => match args.len() {
|
|
|
|
1 => func(ctx, args),
|
2022-03-06 10:58:59 +00:00
|
|
|
_ => Err(Error::InvalidArguments {
|
2022-01-13 17:37:46 +00:00
|
|
|
name: name.to_owned(),
|
|
|
|
message: String::from("The function expects 1 argument."),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
Args::Two => match args.len() {
|
|
|
|
2 => func(ctx, args),
|
2022-03-06 10:58:59 +00:00
|
|
|
_ => Err(Error::InvalidArguments {
|
2022-01-13 17:37:46 +00:00
|
|
|
name: name.to_owned(),
|
|
|
|
message: String::from("The function expects 2 arguments."),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
Args::Three => match args.len() {
|
|
|
|
3 => func(ctx, args),
|
2022-03-06 10:58:59 +00:00
|
|
|
_ => Err(Error::InvalidArguments {
|
2022-01-13 17:37:46 +00:00
|
|
|
name: name.to_owned(),
|
|
|
|
message: String::from("The function expects 3 arguments."),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
Args::NoneOne => match args.len() {
|
|
|
|
0 | 1 => func(ctx, args),
|
2022-03-06 10:58:59 +00:00
|
|
|
_ => Err(Error::InvalidArguments {
|
2022-01-13 17:37:46 +00:00
|
|
|
name: name.to_owned(),
|
|
|
|
message: String::from("The function expects 0 or 1 arguments."),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
Args::NoneTwo => match args.len() {
|
|
|
|
0 | 2 => func(ctx, args),
|
2022-03-06 10:58:59 +00:00
|
|
|
_ => Err(Error::InvalidArguments {
|
2022-01-13 17:37:46 +00:00
|
|
|
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),
|
2022-03-06 10:58:59 +00:00
|
|
|
_ => Err(Error::InvalidArguments {
|
2022-01-13 17:37:46 +00:00
|
|
|
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),
|
2022-03-06 10:58:59 +00:00
|
|
|
_ => Err(Error::InvalidArguments {
|
2022-01-13 17:37:46 +00:00
|
|
|
name: name.to_owned(),
|
|
|
|
message: String::from("The function expects 1 or 2 arguments."),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
Args::Any => func(ctx, args),
|
|
|
|
}
|
|
|
|
}
|