Skip to main content

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