use crate::value::{CoercionFamily, CoercionFamilyExt, Value};
#[must_use]
pub const fn is_numeric(value: &Value) -> bool {
matches!(
value,
Value::Decimal(_)
| Value::Duration(_)
| Value::Float32(_)
| Value::Float64(_)
| Value::Int(_)
| Value::Int128(_)
| Value::Timestamp(_)
| Value::Uint(_)
| Value::Uint128(_)
)
}
#[must_use]
pub const fn supports_numeric_coercion(value: &Value) -> bool {
matches!(
value,
Value::Decimal(_)
| Value::Duration(_)
| Value::Float32(_)
| Value::Float64(_)
| Value::Int(_)
| Value::Int128(_)
| Value::Timestamp(_)
| Value::Uint(_)
| Value::Uint128(_)
)
}
#[must_use]
pub const fn coercion_family(value: &Value) -> CoercionFamily {
match value {
Value::Account(_) | Value::Principal(_) | Value::Ulid(_) => CoercionFamily::Identifier,
Value::Blob(_) | Value::Subaccount(_) => CoercionFamily::Blob,
Value::Bool(_) => CoercionFamily::Bool,
Value::Date(_)
| Value::Decimal(_)
| Value::Duration(_)
| Value::Float32(_)
| Value::Float64(_)
| Value::Int(_)
| Value::Int128(_)
| Value::IntBig(_)
| Value::Timestamp(_)
| Value::Uint(_)
| Value::Uint128(_)
| Value::UintBig(_) => CoercionFamily::Numeric,
Value::Enum(_) => CoercionFamily::Enum,
Value::List(_) | Value::Map(_) => CoercionFamily::Collection,
Value::Null => CoercionFamily::Null,
Value::Text(_) => CoercionFamily::Textual,
Value::Unit => CoercionFamily::Unit,
}
}
impl Value {
#[must_use]
pub const fn is_numeric(&self) -> bool {
is_numeric(self)
}
#[must_use]
pub const fn supports_numeric_coercion(&self) -> bool {
supports_numeric_coercion(self)
}
}
impl CoercionFamilyExt for Value {
fn coercion_family(&self) -> CoercionFamily {
coercion_family(self)
}
}