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;
15pub mod deadline;
16mod dynamic;
17mod error;
18/// The shared error-kind vocabulary (ADR-0502 §2 moved it here from
19/// `relate-error`, which re-exports it): the finest TCK-aligned
20/// [`ErrorKind`], the eight [`TckErrorClass`]es, and the two detail
21/// helpers both error channels render through.
22mod error_kinds;
23pub mod file_identity;
24pub mod integer_args;
25mod list_returns;
26mod literal;
27mod load_pool;
28mod memory_limit;
29mod namespace;
30mod output_safety;
31mod presence_bits;
32mod preview;
33pub mod temporal;
34pub mod tokens;
35mod traversal;
36mod typename;
37mod value;
38
39pub use arity::{
40    builtin_arity, ArityContract, CHAR_LENGTH_ARITY, FORMAT_ARITY, FUNCTION_REQUIRES_ONE_ARGUMENT,
41    LENGTH_REQUIRES_A_PATH, NULLIF_ARITY, VECTOR_ARITY,
42};
43pub use auto_literal::{auto_literal, AutoLiteralGuard};
44pub use cancel::CancelToken;
45pub use convert::{
46    column_type_name, cypher_to_value, plan_value_to_cypher, value_to_cypher, MAX_PLAN_WIRE_DEPTH,
47};
48pub use dynamic::{
49    dynamic_label_names, dynamic_property_key, DynamicAccessError, DynamicNameToken,
50};
51pub use error::{
52    expected_type_detail, invalid_argument_text, negative_page_bound_text,
53    number_out_of_range_text, uniqueness_violation_detail, ArgumentFault, ArithmeticFailure,
54    CapacityKind, Error, Result, NEGATIVE_PAGE_BOUND_DETAIL,
55};
56pub use error_kinds::{
57    duplicate_variable_detail, undefined_variable_detail, unknown_function_detail, ErrorKind,
58    TckErrorClass,
59};
60pub use list_returns::{builtin_returns_list, list_returning_names};
61pub use literal::{LiteralValueKind, TOSTRING_TYPE_ERROR_DETAIL};
62pub use load_pool::{
63    current_load_threads, record_transient_lease, resolve_load_threads, run_load_tasks,
64    with_load_scope, LoadThreads, OpenTransientTracker, TransientLease,
65};
66pub use memory_limit::{
67    parse_expression_memory_limit, parse_memory_limit, parse_positive_memory_limit,
68    require_positive_memory_limit,
69};
70pub use namespace::{
71    is_graph_selector_function_name, node_element_id, parse_node_element_id, parse_rel_element_id,
72    rel_element_id, ElementIdRef, Namespace, GRAPH_SELECTOR_FUNCTIONS,
73    GRAPH_SELECTOR_FUNCTION_NAMES,
74};
75pub use output_safety::{escape_unsafe_output, is_unsafe_output_char, quote_message};
76pub use presence_bits::PresenceBits;
77pub use preview::{
78    preview_feature_disabled_text, DetectorKind, GateOwner, GateSite, ParseOption, PreviewFeature,
79    PreviewFeatures, PreviewSource, PreviewSources, PREVIEW_ENV_VAR, PREVIEW_FEATURES,
80    STORE_CONFIG_FILE,
81};
82pub use temporal::{QueryClockGuard, Temporal, TransactionClock};
83pub use tokens::{ExecutionReceipt, TokenEstimate, TokenOutcome};
84pub use traversal::{
85    require_traversal_depth, traversal_depth_exceeded_message, MAX_TRAVERSAL_DEPTH,
86    MAX_TRAVERSAL_DEPTH_CEILING,
87};
88pub use typename::{bare_type_name, expected_value_type_detail, value_type_name};
89pub use value::{
90    bytes_logical_bytes, logical_bytes_within, logical_bytes_within_memo, next_depth,
91    pack_decoded_values, point_component, str_logical_bytes, tagged_str, uuid_hyphenated,
92    uuid_parse, value_depth_exceeded, ExpansionMemo, Node, Path, PointValue, PropertyMap,
93    Relationship, TaggedStr, Value, VectorCoordKind, MAX_VALUE_DEPTH,
94};
95
96pub type NodeId = i64;
97pub type RelId = i64;
98
99/// Whether `id` belongs to a **virtual** entity — a graph-type
100/// projection synthesized with a negative id and carried by its value alone
101/// (it has no store entry). Persisted ids are non-negative
102/// ([`validate_persisted_id`]), so the sign is the one virtual-entity test.
103pub const fn is_virtual_id(id: i64) -> bool {
104    id < 0
105}
106
107/// The single acceptance rule for a persisted local node or relationship ID.
108///
109/// The bound `0..i64::MAX` rejects a negative ID before it can seed a
110/// next-write counter and reserves `i64::MAX` as the unsupported sentinel,
111/// because installing a row advances the ID allocator. `what` labels the
112/// caller's context in the returned storage error. See ADR-0096.
113pub fn validate_persisted_id(id: i64, what: &str) -> Result<()> {
114    if id < 0 || id == i64::MAX {
115        return Err(Error::storage(format!(
116            "{what} ID {id} is outside 0..i64::MAX"
117        )));
118    }
119    Ok(())
120}
121
122#[cfg(test)]
123mod tests {
124    use super::*;
125
126    #[test]
127    fn persisted_id_predicate_accepts_in_range_and_rejects_zero_nine_six() {
128        for id in [0, 1, 42, i64::MAX - 1] {
129            validate_persisted_id(id, "node").unwrap();
130        }
131        for id in [-1, i64::MIN, i64::MAX] {
132            let error = validate_persisted_id(id, "node").unwrap_err();
133            assert!(matches!(error, Error::Storage(_)), "{error}");
134            assert_eq!(
135                error.to_string(),
136                format!("storage error: node ID {id} is outside 0..i64::MAX")
137            );
138        }
139    }
140}