Skip to main content

relate_core/
literal.rs

1//! The literal-kind vocabulary: which `Value` kinds a Cypher literal can
2//! spell, their CIP-271 element class, and the one homogeneous-list rule.
3//! The front end's static-type inference reads them from here, so the
4//! literal-type spelling and the homogeneity rule have one home. The
5//! executor deliberately does not: Neo4j draws the rule's boundary at the
6//! static type (a property or parameter read renders), so a runtime value
7//! carries no application of it.
8
9use crate::Value;
10
11/// The authored detail CIP-271's `toString()` rejection carries after its
12/// `InvalidArgumentValue:` token — one home for the compile-phase front-end
13/// rejection and the runtime value-level error.
14pub const TOSTRING_TYPE_ERROR_DETAIL: &str = "toString()";
15
16/// The kinds a Cypher literal can spell — the one `Value` → literal-kind
17/// table. Exhaustive over `Value`, so a new variant makes its author state
18/// whether a literal can spell it.
19#[derive(Clone, Copy, PartialEq, Eq, Debug)]
20pub enum LiteralValueKind {
21    Null,
22    Boolean,
23    Integer,
24    Float,
25    String,
26    List,
27    Map,
28}
29
30impl LiteralValueKind {
31    /// The kind of a value a literal can spell; `None` for every
32    /// runtime-only kind (an entity, a path, a temporal, a point, a UUID, a
33    /// vector, a byte array).
34    pub fn of_value(value: &Value) -> Option<Self> {
35        Some(match value {
36            Value::Null => Self::Null,
37            Value::Bool(_) => Self::Boolean,
38            Value::Int(_) => Self::Integer,
39            Value::Float(_) => Self::Float,
40            Value::String(_) => Self::String,
41            Value::List(_) => Self::List,
42            Value::Map(_) => Self::Map,
43            Value::Node(_)
44            | Value::Relationship(_)
45            | Value::Path(_)
46            | Value::Temporal(_)
47            | Value::Point(_)
48            | Value::Uuid(_)
49            | Value::Vector { .. }
50            | Value::Bytes(_) => return None,
51        })
52    }
53
54    /// The kind's Cypher type-name spelling — the one vocabulary the static
55    /// rules' rejection details render.
56    pub fn name(self) -> &'static str {
57        match self {
58            Self::Null => "NULL",
59            Self::Boolean => "BOOLEAN",
60            Self::Integer => "INTEGER",
61            Self::Float => "FLOAT",
62            Self::String => "STRING",
63            Self::List => "LIST",
64            Self::Map => "MAP",
65        }
66    }
67
68    /// Whether the kind is a number (INTEGER or FLOAT).
69    pub fn is_numeric(self) -> bool {
70        matches!(self, Self::Integer | Self::Float)
71    }
72}
73
74/// The element class CIP-271's homogeneous-list gate compares: INTEGER and
75/// FLOAT are one numeric element type, every other literal kind keeps its
76/// own.
77#[derive(Clone, Copy, PartialEq, Eq, Debug)]
78pub enum LiteralElementClass {
79    Numeric,
80    Boolean,
81    String,
82    List,
83    Map,
84}
85
86impl LiteralElementClass {
87    /// The class of a literal kind, or `None` for null: a null element makes
88    /// the list `LIST<ANY>`, which is the CIP-271 rule rather than an
89    /// unclassifiable value.
90    pub fn of_kind(kind: LiteralValueKind) -> Option<Self> {
91        Some(match kind {
92            LiteralValueKind::Integer | LiteralValueKind::Float => Self::Numeric,
93            LiteralValueKind::Boolean => Self::Boolean,
94            LiteralValueKind::String => Self::String,
95            LiteralValueKind::List => Self::List,
96            LiteralValueKind::Map => Self::Map,
97            LiteralValueKind::Null => return None,
98        })
99    }
100
101    /// The class of a value, or `None` for null and every runtime-only kind.
102    pub fn of_value(value: &Value) -> Option<Self> {
103        LiteralValueKind::of_value(value).and_then(Self::of_kind)
104    }
105}
106
107/// The one CIP-271 homogeneity rule, shared by the front-end's static
108/// `toString()` check (over statically-known literal types) and the
109/// executor's value-level one (over runtime values): `Some(class)` when the
110/// list is non-empty and every element has the same known class; `None` for
111/// an empty list, a null or otherwise unclassifiable element, or two classes
112/// mixing — all of which are `LIST<ANY>`, which renders.
113pub fn homogeneous_element_class(
114    classes: impl IntoIterator<Item = Option<LiteralElementClass>>,
115) -> Option<LiteralElementClass> {
116    let mut classes = classes.into_iter();
117    let first = classes.next()??;
118    classes.all(|class| class == Some(first)).then_some(first)
119}
120
121#[cfg(test)]
122mod tests {
123    use super::*;
124    use crate::temporal::Temporal;
125
126    /// The one CIP-271 homogeneity rule: numbers are one class, null and
127    /// non-literal kinds break it, and an empty list is `LIST<ANY>`.
128    #[test]
129    fn homogeneous_element_class_is_the_one_rule() {
130        let homogeneous = |values: &[Value]| {
131            homogeneous_element_class(values.iter().map(LiteralElementClass::of_value)).is_some()
132        };
133        assert!(homogeneous(&[Value::Int(1), Value::Int(2)]));
134        assert!(homogeneous(&[Value::Int(1), Value::Float(2.0)]));
135        assert!(homogeneous(&[Value::Bool(true), Value::Bool(false)]));
136        assert!(homogeneous(&[
137            Value::String("a".into()),
138            Value::String("b".into())
139        ]));
140        assert!(homogeneous(&[Value::List(vec![Value::Int(1)].into())]));
141        assert!(!homogeneous(&[]));
142        assert!(!homogeneous(&[Value::Int(1), Value::String("a".into())]));
143        assert!(!homogeneous(&[Value::Int(1), Value::Null]));
144        // A kind a literal cannot spell leaves the list `LIST<ANY>`.
145        assert!(!homogeneous(&[Value::Temporal(Temporal::CivilDate {
146            year: 2020,
147            month: 1,
148            day: 1,
149        })]));
150    }
151}