Skip to main content

icydb_core/value/
semantics.rs

1//! Module: value::semantics
2//!
3//! Responsibility: semantic classification for dynamic `Value` variants.
4//! Does not own: operator execution, map normalization, or numeric conversion.
5//! Boundary: lightweight capability and coercion-family classification.
6
7use crate::value::{CoercionFamily, Value};
8
9/// Returns true if the value is one of the numeric-like variants supported by
10/// numeric comparison/ordering.
11#[must_use]
12const fn is_numeric(value: &Value) -> bool {
13    matches!(
14        value,
15        Value::Decimal(_)
16            | Value::Duration(_)
17            | Value::Float32(_)
18            | Value::Float64(_)
19            | Value::Int64(_)
20            | Value::Int128(_)
21            | Value::Timestamp(_)
22            | Value::Nat64(_)
23            | Value::Nat128(_)
24    )
25}
26
27/// Returns true when numeric coercion/comparison is explicitly allowed.
28#[must_use]
29pub(crate) const fn supports_numeric_coercion(value: &Value) -> bool {
30    matches!(
31        value,
32        Value::Decimal(_)
33            | Value::Duration(_)
34            | Value::Float32(_)
35            | Value::Float64(_)
36            | Value::Int64(_)
37            | Value::Int128(_)
38            | Value::Timestamp(_)
39            | Value::Nat64(_)
40            | Value::Nat128(_)
41    )
42}
43
44/// Returns the coercion-routing family for this value.
45#[must_use]
46const fn coercion_family(value: &Value) -> CoercionFamily {
47    match value {
48        Value::Account(_) | Value::Principal(_) | Value::Ulid(_) => CoercionFamily::Identifier,
49        Value::Blob(_) | Value::Subaccount(_) => CoercionFamily::Blob,
50        Value::Bool(_) => CoercionFamily::Bool,
51        Value::Date(_)
52        | Value::Decimal(_)
53        | Value::Duration(_)
54        | Value::Float32(_)
55        | Value::Float64(_)
56        | Value::Int64(_)
57        | Value::Int128(_)
58        | Value::IntBig(_)
59        | Value::Timestamp(_)
60        | Value::Nat64(_)
61        | Value::Nat128(_)
62        | Value::NatBig(_)
63        | Value::U256(_) => CoercionFamily::Numeric,
64        Value::Enum(_) => CoercionFamily::Enum,
65        Value::List(_) | Value::Map(_) => CoercionFamily::Collection,
66        Value::Null => CoercionFamily::Null,
67        Value::Text(_) => CoercionFamily::Textual,
68        Value::Unit => CoercionFamily::Unit,
69    }
70}
71
72impl Value {
73    /// Returns true if the value is one of the numeric-like variants
74    /// supported by numeric comparison/ordering.
75    #[must_use]
76    pub const fn is_numeric(&self) -> bool {
77        is_numeric(self)
78    }
79
80    /// Returns true when numeric coercion/comparison is explicitly allowed.
81    #[must_use]
82    pub const fn supports_numeric_coercion(&self) -> bool {
83        supports_numeric_coercion(self)
84    }
85
86    /// Returns the coercion-routing family for this value.
87    ///
88    /// NOTE:
89    /// This does NOT imply numeric, arithmetic, ordering, or keyability support.
90    /// All scalar capabilities are registry-driven.
91    #[must_use]
92    pub const fn coercion_family(&self) -> CoercionFamily {
93        coercion_family(self)
94    }
95}