| 1 | //! Runtime values. |
| 2 | |
| 3 | use std::collections::BTreeMap; |
| 4 | |
| 5 | /// The value domain for the core-language slice. |
| 6 | /// |
| 7 | /// Numbers are `f64` for now — a deliberate, documented deviation from the |
| 8 | /// FEEL spec's `decimal128` (see DECISIONS.md, ADR-2). Strings and lists were |
| 9 | /// added when the slice grew from "numbers + intervals" to the core language |
| 10 | /// (operators, control flow, `for`/`some`/`every`); `Context` (a FEEL context / |
| 11 | /// map) was added for nested-object input and context literals (ADR-12). |
| 12 | #[derive(Debug, Clone, PartialEq)] |
| 13 | pub enum Value { |
| 14 | Number(f64), |
| 15 | Boolean(bool), |
| 16 | String(String), |
| 17 | List(Vec<Value>), |
| 18 | /// A FEEL context (map of name -> value). Backed by a `BTreeMap` so equality |
| 19 | /// is key/value structural and JSON output is key-ordered (deterministic). |
| 20 | Context(BTreeMap<String, Value>), |
| 21 | /// A FEEL `date` (ADR-18). |
| 22 | Date(jiff::civil::Date), |
| 23 | /// A FEEL `time` — a civil time-of-day with an optional zone. |
| 24 | Time(crate::temporal::Time), |
| 25 | /// A FEEL `date and time` — a civil date-time with an optional zone. |
| 26 | DateTime(crate::temporal::DateTime), |
| 27 | /// A FEEL `years and months duration`, as a total number of months. |
| 28 | YearsMonthsDuration(i64), |
| 29 | /// A FEEL `days and time duration`, as an absolute signed duration. |
| 30 | DaysTimeDuration(jiff::SignedDuration), |
| 31 | /// A first-class range / interval value (DMN §10.3.4.6), e.g. `[1..10)` or |
| 32 | /// `range("[1..10]")`. Boxed so the variant stays one word and `Value` does not |
| 33 | /// grow past 40 bytes (see the size test in `lib.rs`). The endpoints are any two |
| 34 | /// orderable, same-typed [`Value`]s; the booleans carry each bracket's openness |
| 35 | /// (`[`/`]` closed, `(`/`)` open). |
| 36 | Range(Box<RangeVal>), |
| 37 | /// An opaque parenthesised comparison-test *value* — `(< 10)`, `(= 10)`, `(!= 10)` |
| 38 | /// in value position (DMN §10.3.4.6; ADR-32). Boxed so the variant stays one word |
| 39 | /// and `Value` does not grow past 40 bytes (see the size test in `lib.rs`). |
| 40 | /// |
| 41 | /// Unlike the desugar hypothesis ADR-28 floated, this is **not** a promoted |
| 42 | /// [`Range`](Value::Range): the measured TCK semantics (0068) are that it equals an |
| 43 | /// *identical test* reflexively — `(<10) = (<10)`, `(=10) = (=10)`, `(!=10) = (!=10)` |
| 44 | /// are all true — but is **never** equal to a `Range` (`(<10) = (null..10)` and |
| 45 | /// `(=10) = [10..10]` are both false). So equality is structural (same op + FEEL-equal |
| 46 | /// endpoint) and range-incompatible. It has no order, no host type, and conforms to no |
| 47 | /// `instance of` type but `Any`. |
| 48 | UnaryTestVal(Box<UnaryTestVal>), |
| 49 | /// A lazily-resolved host value (a Python/JS object behind a target-specific |
| 50 | /// [`ForeignObject`](crate::foreign::ForeignObject)). Only the wrapper crates |
| 51 | /// ever construct one (ADR-21); the FEEL dot resolves it by name on demand, |
| 52 | /// every other operation [`forced`](Value::forced)s it first. |
| 53 | Foreign(crate::foreign::Foreign), |
| 54 | /// A first-class function: a native FEEL closure or a host callable, behind a |
| 55 | /// single thin [`Arc`](std::sync::Arc) so the variant is one word and `Value` |
| 56 | /// does not grow (ADR-23). Created by `function(…)` literals and by the wrapper |
| 57 | /// crates (host callables). It is already concrete, so [`forced`](Value::forced) |
| 58 | /// passes it through unchanged. |
| 59 | Function(std::sync::Arc<crate::function::Func>), |
| 60 | /// A "value null": a legitimate absence — the `null` literal, an unbound |
| 61 | /// variable, a missing context member, or an input null. |
| 62 | Null, |
| 63 | /// An "error null": a null that arose from a *type or domain error* (e.g. |
| 64 | /// arithmetic on non-numbers, division by zero, comparing incomparable |
| 65 | /// types). It behaves exactly like [`Value::Null`] for every FEEL operation |
| 66 | /// (equality, ordering, three-valued logic) — see DECISIONS.md ADR-14 — but |
| 67 | /// carries provenance so a strict boundary can surface it instead of letting |
| 68 | /// the error pass silently. The wrappers collapse it to JSON `null` by |
| 69 | /// default (lenient) or raise on it (strict). |
| 70 | Error, |
| 71 | } |
| 72 | |
| 73 | /// The payload of a [`Value::Range`]: two endpoints plus each bracket's openness. |
| 74 | /// |
| 75 | /// An endpoint is `None` when it is **unbounded** — `±∞`, a syntactically omitted |
| 76 | /// bound (`[1..)` is everything `≥ 1`, `(..10]` everything `≤ 10`; ADR-31). This is |
| 77 | /// distinct from a *present* `null` endpoint (`Some(Value::Null)`), which keeps the |
| 78 | /// three-valued "unknown" membership of `[null..10]`. The openness flag of an |
| 79 | /// unbounded side is meaningless (`±∞` is never a member) and is ignored by every |
| 80 | /// semantic operation (equality, `.start included`, the Allen relations); it is kept |
| 81 | /// only so `string([1..))` can echo the bracket the user wrote. |
| 82 | /// |
| 83 | /// A *well-formed* range has `start <= end` and any present endpoint non-null of the |
| 84 | /// same orderable type; the constructors ([`crate::builtins`] `range`, the `[a..b]` |
| 85 | /// literal) enforce that, so consumers may assume it. |
| 86 | #[derive(Debug, Clone, PartialEq)] |
| 87 | pub struct RangeVal { |
| 88 | pub start: Option<Value>, |
| 89 | pub end: Option<Value>, |
| 90 | /// `true` if the lower bound is *exclusive* (`(`/`]` start). Meaningless when |
| 91 | /// `start` is `None` (unbounded). |
| 92 | pub start_open: bool, |
| 93 | /// `true` if the upper bound is *exclusive* (`)`/`[` end). Meaningless when `end` |
| 94 | /// is `None` (unbounded). |
| 95 | pub end_open: bool, |
| 96 | } |
| 97 | |
| 98 | /// The payload of a [`Value::UnaryTestVal`]: the comparison operator and the |
| 99 | /// already-evaluated endpoint of an opaque parenthesised comparison-test value |
| 100 | /// (`(< 10)` → `op: Lt, endpoint: Number(10)`; ADR-32). |
| 101 | #[derive(Debug, Clone, PartialEq)] |
| 102 | pub struct UnaryTestVal { |
| 103 | pub op: crate::ast::CmpOp, |
| 104 | pub endpoint: Value, |
| 105 | } |
| 106 | |
| 107 | impl Value { |
| 108 | /// True if this value is, or transitively contains, an [`Value::Error`]. |
| 109 | /// Used by the strict boundary policy in the wrappers. |
| 110 | pub fn contains_error(&self) -> bool { |
| 111 | match self { |
| 112 | Value::Error => true, |
| 113 | Value::List(items) => items.iter().any(Value::contains_error), |
| 114 | Value::Context(map) => map.values().any(Value::contains_error), |
| 115 | Value::Range(r) => { |
| 116 | r.start.as_ref().is_some_and(Value::contains_error) |
| 117 | || r.end.as_ref().is_some_and(Value::contains_error) |
| 118 | } |
| 119 | Value::UnaryTestVal(t) => t.endpoint.contains_error(), |
| 120 | _ => false, |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /// If this is a [`Value::Foreign`], materialise it one level via |
| 125 | /// [`ForeignObject::force`](crate::foreign::ForeignObject::force); otherwise |
| 126 | /// return it unchanged. Called at every value-*consuming* site in the |
| 127 | /// evaluator (arithmetic, comparison, logic, iteration, builtin arguments) so |
| 128 | /// those operations see a concrete value; the FEEL dot deliberately does |
| 129 | /// **not** call it, leaving path access lazy (ADR-21). The hot path is a |
| 130 | /// single predicted tag test, so a pure-Rust embedder (who never creates a |
| 131 | /// `Foreign`) pays nothing. |
| 132 | #[inline] |
| 133 | pub(crate) fn forced(self) -> Value { |
| 134 | match self { |
| 135 | Value::Foreign(f) => f.0.force(), |
| 136 | other => other, |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | /// Collapse every [`Value::Error`] (including nested ones) to [`Value::Null`]. |
| 141 | /// This is the lenient boundary policy: error provenance is dropped and the |
| 142 | /// result is the plain, spec-shaped value. |
| 143 | pub fn into_lenient(self) -> Value { |
| 144 | match self { |
| 145 | Value::Error => Value::Null, |
| 146 | Value::List(items) => Value::List(items.into_iter().map(Value::into_lenient).collect()), |
| 147 | Value::Context(map) => Value::Context( |
| 148 | map.into_iter() |
| 149 | .map(|(k, v)| (k, v.into_lenient())) |
| 150 | .collect(), |
| 151 | ), |
| 152 | Value::Range(r) => Value::Range(Box::new(RangeVal { |
| 153 | start: r.start.map(Value::into_lenient), |
| 154 | end: r.end.map(Value::into_lenient), |
| 155 | ..*r |
| 156 | })), |
| 157 | Value::UnaryTestVal(t) => Value::UnaryTestVal(Box::new(UnaryTestVal { |
| 158 | op: t.op, |
| 159 | endpoint: t.endpoint.into_lenient(), |
| 160 | })), |
| 161 | other => other, |
| 162 | } |
| 163 | } |
| 164 | } |