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, CoercionFamilyExt, Value};
8
9/// Returns true if the value is one of the numeric-like variants supported by
10/// numeric comparison/ordering.
11#[must_use]
12pub const fn is_numeric(value: &Value) -> bool {
13    matches!(
14        value,
15        Value::Decimal(_)
16            | Value::Duration(_)
17            | Value::Float32(_)
18            | Value::Float64(_)
19            | Value::Int(_)
20            | Value::Int128(_)
21            | Value::Timestamp(_)
22            | Value::Uint(_)
23            | Value::Uint128(_)
24    )
25}
26
27/// Returns true when numeric coercion/comparison is explicitly allowed.
28#[must_use]
29pub 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::Int(_)
37            | Value::Int128(_)
38            | Value::Timestamp(_)
39            | Value::Uint(_)
40            | Value::Uint128(_)
41    )
42}
43
44/// Returns the coercion-routing family for this value.
45#[must_use]
46pub const 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::Int(_)
57        | Value::Int128(_)
58        | Value::IntBig(_)
59        | Value::Timestamp(_)
60        | Value::Uint(_)
61        | Value::Uint128(_)
62        | Value::UintBig(_) => CoercionFamily::Numeric,
63        Value::Enum(_) => CoercionFamily::Enum,
64        Value::List(_) | Value::Map(_) => CoercionFamily::Collection,
65        Value::Null => CoercionFamily::Null,
66        Value::Text(_) => CoercionFamily::Textual,
67        Value::Unit => CoercionFamily::Unit,
68    }
69}
70
71impl Value {
72    /// Returns true if the value is one of the numeric-like variants
73    /// supported by numeric comparison/ordering.
74    #[must_use]
75    pub const fn is_numeric(&self) -> bool {
76        is_numeric(self)
77    }
78
79    /// Returns true when numeric coercion/comparison is explicitly allowed.
80    #[must_use]
81    pub const fn supports_numeric_coercion(&self) -> bool {
82        supports_numeric_coercion(self)
83    }
84}
85
86impl CoercionFamilyExt for Value {
87    /// Returns the coercion-routing family for this value.
88    ///
89    /// NOTE:
90    /// This does NOT imply numeric, arithmetic, ordering, or keyability support.
91    /// All scalar capabilities are registry-driven.
92    fn coercion_family(&self) -> CoercionFamily {
93        coercion_family(self)
94    }
95}