relate_core/arity.rs
1//! The builtin-function arity registry: the shared spellings of the arity
2//! rejection details and the [`ArityContract`] the front-end check, the
3//! planner's rule table and the executor's dispatch read as one fact.
4
5/// The one spelling of the one-argument arity requirement shared by the
6/// planner's `FIXED_ARITY_FUNCTIONS` row and the executor's list-builtin
7/// backstop — the bare detail, without the `InvalidNumberOfArguments`
8/// class token, which each layer composes exactly once
9/// ([`crate::Error::invalid_number_of_arguments`], or the typed channel).
10pub const FUNCTION_REQUIRES_ONE_ARGUMENT: &str = "function requires one argument";
11
12/// The one spelling of `length()`'s non-path argument rejection, shared by
13/// the planner's static rules and the executor's runtime backstop — the
14/// bare detail, without the `InvalidArgumentType` class token, which each
15/// layer composes (the [`FUNCTION_REQUIRES_ONE_ARGUMENT`] precedent).
16pub const LENGTH_REQUIRES_A_PATH: &str = "length() needs a path";
17
18/// The one spelling of `nullif()`'s two-argument arity requirement: the
19/// front-end static-argument check and the executor's implementation both
20/// state it from here, so the compile-time and runtime halves cannot drift
21/// (the [`FUNCTION_REQUIRES_ONE_ARGUMENT`] precedent).
22pub(crate) const NULLIF_REQUIRES_TWO_ARGUMENTS: &str = "nullif() requires two arguments";
23
24/// The one spelling of `format()`'s one-or-two-argument arity requirement,
25/// shared by the front-end check and the executor's implementation.
26pub(crate) const FORMAT_REQUIRES_ONE_OR_TWO_ARGUMENTS: &str =
27 "format() requires one or two arguments";
28
29/// The one spelling of `vector()`'s three-argument arity requirement, shared
30/// by the front-end check and the executor's implementation.
31pub(crate) const VECTOR_REQUIRES_THREE_ARGUMENTS: &str = "vector() requires three arguments";
32
33/// One builtin's compile-phase arity contract: the accepted argument-count
34/// range together with the authored rejection *detail* (bare, without the
35/// class token). Declared in the shared contract layer so the front-end
36/// check, the planner's rule table and the executor's dispatch read one
37/// fact instead of a count and a message each.
38#[derive(Debug, Clone, Copy, PartialEq, Eq)]
39pub struct ArityContract {
40 min: usize,
41 max: usize,
42 detail: &'static str,
43}
44
45impl ArityContract {
46 /// Exactly `count` arguments.
47 pub const fn exact(count: usize, detail: &'static str) -> Self {
48 Self {
49 min: count,
50 max: count,
51 detail,
52 }
53 }
54
55 /// Between `min` and `max` arguments, inclusive.
56 pub(crate) const fn range(min: usize, max: usize, detail: &'static str) -> Self {
57 Self { min, max, detail }
58 }
59
60 /// The least accepted argument count — the executor's dispatch gate
61 /// derives its row bounds from here, so the count has one home.
62 pub const fn min(self) -> usize {
63 self.min
64 }
65
66 /// The greatest accepted argument count (see [`Self::min`]).
67 pub const fn max(self) -> usize {
68 self.max
69 }
70
71 /// The authored rejection detail a call supplying `count` arguments
72 /// violates, or `None` when it satisfies the contract. The accept test
73 /// and the rejection travel together, so no consumer can pair them
74 /// wrongly; each layer composes the class token once around the detail.
75 pub fn violation(self, count: usize) -> Option<&'static str> {
76 if (self.min..=self.max).contains(&count) {
77 None
78 } else {
79 Some(self.detail)
80 }
81 }
82}
83
84/// `char_length()`/`character_length()`'s one-argument contract.
85pub const CHAR_LENGTH_ARITY: ArityContract =
86 ArityContract::exact(1, FUNCTION_REQUIRES_ONE_ARGUMENT);
87
88/// `nullif()`'s two-argument contract.
89pub const NULLIF_ARITY: ArityContract = ArityContract::exact(2, NULLIF_REQUIRES_TWO_ARGUMENTS);
90
91/// `format()`'s one-or-two-argument contract.
92pub const FORMAT_ARITY: ArityContract =
93 ArityContract::range(1, 2, FORMAT_REQUIRES_ONE_OR_TWO_ARGUMENTS);
94
95/// `vector()`'s three-argument contract.
96pub const VECTOR_ARITY: ArityContract = ArityContract::exact(3, VECTOR_REQUIRES_THREE_ARGUMENTS);
97
98/// The arity contract shared across the front-end check, the planner's rule
99/// table and the executor's dispatch, by lowercased builtin name. `None` for
100/// a name with no cross-layer contract (its arity, if any, is owned by one
101/// layer alone).
102pub fn builtin_arity(name: &str) -> Option<ArityContract> {
103 Some(match name {
104 "char_length" | "character_length" => CHAR_LENGTH_ARITY,
105 "nullif" => NULLIF_ARITY,
106 "format" => FORMAT_ARITY,
107 "vector" => VECTOR_ARITY,
108 _ => return None,
109 })
110}