1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//! Scalar and structured values used in queries and interventions.
//!
//! SPDX-License-Identifier: MIT OR Apache-2.0
use std::sync::Arc;
/// A concrete value assigned by an intervention or query contrast.
#[derive(Clone, Debug, PartialEq)]
pub enum Value {
/// Floating-point scalar.
Float64(f64),
/// Integer scalar.
Int64(i64),
/// Boolean.
Bool(bool),
/// Category code (raw u32; domain lives in the schema).
Category(u32),
/// Opaque label for diagnostics only (not used in hot paths as a key).
Label(Arc<str>),
}
impl Value {
/// Convenience for a float64 value.
#[must_use]
pub const fn f64(v: f64) -> Self {
Self::Float64(v)
}
/// Interpret as an `f64` level when possible.
#[must_use]
#[allow(clippy::cast_precision_loss)]
pub fn as_f64(&self) -> Option<f64> {
match self {
Self::Float64(v) => Some(*v),
Self::Int64(v) => Some(*v as f64),
Self::Bool(v) => Some(f64::from(u8::from(*v))),
Self::Category(v) => Some(f64::from(*v)),
Self::Label(_) => None,
}
}
}
impl Eq for Value {}
impl core::hash::Hash for Value {
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
core::mem::discriminant(self).hash(state);
match self {
Self::Float64(v) => v.to_bits().hash(state),
Self::Int64(v) => v.hash(state),
Self::Bool(v) => v.hash(state),
Self::Category(v) => v.hash(state),
Self::Label(v) => v.hash(state),
}
}
}