pub const FUNCTION_REQUIRES_ONE_ARGUMENT: &str = "function requires one argument";
pub const LENGTH_REQUIRES_A_PATH: &str = "length() needs a path";
pub const NULLIF_REQUIRES_TWO_ARGUMENTS: &str = "nullif() requires two arguments";
pub const FORMAT_REQUIRES_ONE_OR_TWO_ARGUMENTS: &str = "format() requires one or two arguments";
pub const VECTOR_REQUIRES_THREE_ARGUMENTS: &str = "vector() requires three arguments";
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ArityContract {
min: usize,
max: usize,
detail: &'static str,
}
impl ArityContract {
pub const fn exact(count: usize, detail: &'static str) -> Self {
Self {
min: count,
max: count,
detail,
}
}
pub(crate) const fn range(min: usize, max: usize, detail: &'static str) -> Self {
Self { min, max, detail }
}
pub const fn min(self) -> usize {
self.min
}
pub const fn max(self) -> usize {
self.max
}
pub fn violation(self, count: usize) -> Option<&'static str> {
if (self.min..=self.max).contains(&count) {
None
} else {
Some(self.detail)
}
}
}
pub const CHAR_LENGTH_ARITY: ArityContract =
ArityContract::exact(1, FUNCTION_REQUIRES_ONE_ARGUMENT);
pub const NULLIF_ARITY: ArityContract = ArityContract::exact(2, NULLIF_REQUIRES_TWO_ARGUMENTS);
pub const FORMAT_ARITY: ArityContract =
ArityContract::range(1, 2, FORMAT_REQUIRES_ONE_OR_TWO_ARGUMENTS);
pub const VECTOR_ARITY: ArityContract = ArityContract::exact(3, VECTOR_REQUIRES_THREE_ARGUMENTS);
pub fn builtin_arity(name: &str) -> Option<ArityContract> {
Some(match name {
"char_length" | "character_length" => CHAR_LENGTH_ARITY,
"nullif" => NULLIF_ARITY,
"format" => FORMAT_ARITY,
"vector" => VECTOR_ARITY,
_ => return None,
})
}