Skip to main content

relate_core/
lib.rs

1//! Core values, errors, and identifiers for Relate.
2
3/// The byte bound of one wire message every surface frames around — the
4/// value Bolt's chunked framing caps a message at and the C ABI's params
5/// builder caps one `put_string` value at (single-sourced so the two
6/// surfaces can never disagree). Fixed constant, no knob (ADR-0514).
7pub const MAX_WIRE_MESSAGE_BYTES: usize = 16 * 1024 * 1024;
8
9mod arity;
10mod auto_literal;
11mod cancel;
12pub mod ce_messages;
13mod convert;
14mod convert_walk;
15mod dynamic;
16mod error;
17/// The shared error-kind vocabulary (ADR-0502 §2 moved it here from
18/// `relate-error`, which re-exports it): the finest TCK-aligned
19/// [`ErrorKind`], the eight [`TckErrorClass`]es, and the two detail
20/// helpers both error channels render through.
21mod error_kinds;
22pub mod file_identity;
23pub mod integer_args;
24mod memory_limit;
25mod namespace;
26mod output_safety;
27mod presence_bits;
28mod preview;
29pub mod temporal;
30pub mod tokens;
31mod traversal;
32mod typename;
33mod value;
34
35pub use arity::{
36    builtin_arity, ArityContract, CHAR_LENGTH_ARITY, FORMAT_ARITY,
37    FORMAT_REQUIRES_ONE_OR_TWO_ARGUMENTS, FUNCTION_REQUIRES_ONE_ARGUMENT, LENGTH_REQUIRES_A_PATH,
38    NULLIF_ARITY, NULLIF_REQUIRES_TWO_ARGUMENTS, VECTOR_ARITY, VECTOR_REQUIRES_THREE_ARGUMENTS,
39};
40pub use auto_literal::{auto_literal, AutoLiteralGuard};
41pub use cancel::CancelToken;
42pub use convert::{
43    column_type_name, cypher_to_value, plan_value_to_cypher, value_to_cypher, MAX_PLAN_WIRE_DEPTH,
44};
45pub use dynamic::{
46    dynamic_label_names, dynamic_property_key, DynamicAccessError, DynamicNameToken,
47};
48pub use error::{
49    expected_type_detail, negative_page_bound_text, uniqueness_violation_detail, ArithmeticFailure,
50    CapacityKind, Error, Result, NEGATIVE_PAGE_BOUND_DETAIL,
51};
52pub use error_kinds::{
53    duplicate_variable_detail, undefined_variable_detail, unknown_function_detail, ErrorKind,
54    TckErrorClass,
55};
56pub use memory_limit::{
57    parse_expression_memory_limit, parse_memory_limit, parse_positive_memory_limit,
58    require_positive_memory_limit,
59};
60pub use namespace::{
61    is_graph_selector_function_name, node_element_id, parse_node_element_id, parse_rel_element_id,
62    rel_element_id, ElementIdRef, Namespace, GRAPH_SELECTOR_FUNCTIONS,
63    GRAPH_SELECTOR_FUNCTION_NAMES,
64};
65pub use output_safety::is_unsafe_output_char;
66pub use presence_bits::PresenceBits;
67pub use preview::{
68    preview_feature_disabled_text, PreviewFeature, PreviewFeatures, PREVIEW_FEATURES,
69};
70pub use temporal::{QueryClockGuard, Temporal};
71pub use tokens::{ExecutionReceipt, TokenEstimate, TokenOutcome};
72pub use traversal::{
73    require_traversal_depth, traversal_depth_exceeded_message, MAX_TRAVERSAL_DEPTH,
74    MAX_TRAVERSAL_DEPTH_CEILING,
75};
76pub use typename::{bare_type_name, expected_value_type_detail, value_type_name};
77pub use value::{
78    bytes_logical_bytes, logical_bytes_within, next_depth, point_component, str_logical_bytes,
79    tagged_str, uuid_hyphenated, uuid_parse, value_depth_exceeded, Node, Path, PointValue,
80    PropertyMap, Relationship, TaggedStr, Value, VectorCoordKind, MAX_VALUE_DEPTH,
81};
82
83pub type NodeId = i64;
84pub type RelId = i64;
85
86/// Whether `id` belongs to a CIP-257 **virtual** entity — a graph-type
87/// projection synthesized with a negative id and carried by its value alone
88/// (it has no store entry). Persisted ids are non-negative
89/// ([`validate_persisted_id`]), so the sign is the one virtual-entity test.
90pub const fn is_virtual_id(id: i64) -> bool {
91    id < 0
92}
93
94/// The single acceptance rule for a persisted local node or relationship ID.
95///
96/// The bound `0..i64::MAX` rejects a negative ID before it can seed a
97/// next-write counter and reserves `i64::MAX` as the unsupported sentinel,
98/// because installing a row advances the ID allocator. `what` labels the
99/// caller's context in the returned storage error. See ADR-0096.
100pub fn validate_persisted_id(id: i64, what: &str) -> Result<()> {
101    if id < 0 || id == i64::MAX {
102        return Err(Error::storage(format!(
103            "{what} ID {id} is outside 0..i64::MAX"
104        )));
105    }
106    Ok(())
107}
108
109#[cfg(test)]
110mod tests {
111    use super::*;
112
113    #[test]
114    fn persisted_id_predicate_accepts_in_range_and_rejects_zero_nine_six() {
115        for id in [0, 1, 42, i64::MAX - 1] {
116            validate_persisted_id(id, "node").unwrap();
117        }
118        for id in [-1, i64::MIN, i64::MAX] {
119            let error = validate_persisted_id(id, "node").unwrap_err();
120            assert!(matches!(error, Error::Storage(_)), "{error}");
121            assert_eq!(
122                error.to_string(),
123                format!("storage error: node ID {id} is outside 0..i64::MAX")
124            );
125        }
126    }
127}