Skip to main content

lemma/computation/
operation_result.rs

1//! Result envelope for computation operations: a produced value or a domain veto.
2
3use std::fmt;
4
5use crate::planning::semantics::{
6    BoundValueKind, DataPath, LemmaType, LiteralValue, SemanticDateTime, SemanticTime,
7    TypeSpecification,
8};
9
10/// Why an operation yielded no value (domain veto).
11#[derive(Debug, Clone, PartialEq, Eq, Hash)]
12pub enum VetoType {
13    /// Evaluation needed a data that was not provided
14    MissingData {
15        data: DataPath,
16        suggestion: Option<String>,
17    },
18    /// Evaluation needed a field of a list row that was not provided
19    /// (`key` is the row path, e.g. `menu.2.price`).
20    MissingRowData { key: String },
21    /// Explicit `veto "reason"` in Lemma source
22    UserDefined { message: Option<String> },
23    /// Runtime domain failure (division by zero, date overflow, bad Data override, etc.)
24    Computation { message: String },
25}
26
27impl fmt::Display for VetoType {
28    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29        match self {
30            VetoType::MissingData {
31                data,
32                suggestion: Some(suggestion),
33            } => write!(
34                f,
35                "Required data '{data}' was not provided. Did you mean '{suggestion}'?"
36            ),
37            VetoType::MissingData {
38                data,
39                suggestion: None,
40            } => write!(f, "Required data '{data}' was not provided."),
41            VetoType::MissingRowData { key } => match split_numbered_row_field_key(key) {
42                Some((list, number, field)) => write!(
43                    f,
44                    "Required field '{field}' was not provided on row {number} of list '{list}'."
45                ),
46                None => write!(f, "Required data '{key}' was not provided."),
47            },
48            VetoType::UserDefined { message: Some(msg) } => write!(f, "{msg}"),
49            VetoType::UserDefined { message: None } => write!(f, "This rule was vetoed."),
50            VetoType::Computation { message } => write!(f, "{message}"),
51        }
52    }
53}
54
55/// `list.number.field` from [`crate::planning::semantics::row_field_input_key`].
56fn split_numbered_row_field_key(key: &str) -> Option<(&str, &str, &str)> {
57    let (list_and_number, field) = key.rsplit_once('.')?;
58    let (list, number) = list_and_number.rsplit_once('.')?;
59    if number.is_empty() || !number.bytes().all(|b| b.is_ascii_digit()) {
60        return None;
61    }
62    if list.is_empty() || field.is_empty() {
63        return None;
64    }
65    Some((list, number, field))
66}
67
68impl VetoType {
69    #[must_use]
70    pub fn computation(message: impl Into<String>) -> Self {
71        VetoType::Computation {
72            message: message.into(),
73        }
74    }
75
76    #[must_use]
77    pub fn missing_data(data: DataPath, suggestion: Option<String>) -> Self {
78        VetoType::MissingData { data, suggestion }
79    }
80}
81
82/// Result of evaluating a rule, expression, or data leaf.
83#[derive(Debug, Clone, PartialEq, Eq, Hash)]
84pub enum OperationResult {
85    /// Operation produced a value (canonical magnitude + optional written unit)
86    Value(BoundValueKind),
87    /// Operation was vetoed (valid result, no value)
88    Veto(VetoType),
89}
90
91impl OperationResult {
92    /// This result held by something of type `holder` (see [`BoundValueKind::held_as`]).
93    pub(crate) fn held_as(self, holder: &crate::planning::semantics::LemmaType) -> Self {
94        match self {
95            OperationResult::Value(bound) => Self::from_bound(bound.held_as(holder)),
96            veto => veto,
97        }
98    }
99
100    /// Wrap a typeless literal with no written-unit binding.
101    pub fn from_literal(value: LiteralValue) -> Self {
102        Self::Value(BoundValueKind::unbound(value.value))
103    }
104
105    pub fn from_bound(bound: BoundValueKind) -> Self {
106        Self::Value(bound)
107    }
108
109    pub fn vetoed(&self) -> bool {
110        matches!(self, OperationResult::Veto(_))
111    }
112
113    /// True when this result is a [`VetoType::MissingData`] or [`VetoType::MissingRowData`] veto.
114    #[must_use]
115    pub fn is_missing_data(&self) -> bool {
116        matches!(
117            self,
118            OperationResult::Veto(VetoType::MissingData { .. } | VetoType::MissingRowData { .. })
119        )
120    }
121
122    #[must_use]
123    pub fn value(&self) -> Option<&BoundValueKind> {
124        match self {
125            OperationResult::Value(value) => Some(value),
126            OperationResult::Veto(_) => None,
127        }
128    }
129
130    /// Typeless payload (written unit dropped). Prefer [`Self::value`] when the binding matters.
131    #[must_use]
132    pub fn literal_value(&self) -> Option<LiteralValue> {
133        self.value().map(BoundValueKind::to_literal)
134    }
135
136    pub fn number(number: rust_decimal::Decimal) -> Self {
137        Self::from_literal(LiteralValue::number_from_decimal(number))
138    }
139
140    pub fn measure(
141        value: rust_decimal::Decimal,
142        unit: impl Into<String>,
143        lemma_type: Option<LemmaType>,
144    ) -> Self {
145        use crate::computation::rational::checked_mul;
146        let lemma_type = std::sync::Arc::new(
147            lemma_type.unwrap_or_else(|| LemmaType::primitive(TypeSpecification::measure())),
148        );
149        let unit_name = unit.into();
150        let rational = crate::literals::rational_from_parsed_decimal(value)
151            .expect("BUG: operation result measure must lift at boundary");
152        let factor = if let TypeSpecification::Measure { units, .. } = &lemma_type.specifications {
153            units
154                .get(&unit_name)
155                .map(|u| u.require_literal_factor().clone())
156                .unwrap_or_else(|_| {
157                    panic!(
158                        "BUG: OperationResult::measure unit '{}' not declared on type",
159                        unit_name
160                    )
161                })
162        } else {
163            crate::computation::rational::rational_one()
164        };
165        let canonical = checked_mul(&rational, &factor)
166            .expect("BUG: measure canonicalization overflow in OperationResult::measure");
167        Self::from_bound(BoundValueKind {
168            value: crate::planning::semantics::ValueKind::Measure(canonical),
169            unit: Some(std::sync::Arc::from(unit_name)),
170            instance_factors: None,
171        })
172    }
173
174    pub fn text(text: impl Into<String>) -> Self {
175        Self::from_literal(LiteralValue::text(text.into()))
176    }
177
178    pub fn date(date: impl Into<SemanticDateTime>) -> Self {
179        Self::from_literal(LiteralValue::date(date.into()))
180    }
181
182    pub fn time(time: impl Into<SemanticTime>) -> Self {
183        Self::from_literal(LiteralValue::time(time.into()))
184    }
185
186    pub fn boolean(boolean: bool) -> Self {
187        Self::from_literal(LiteralValue::from_bool(boolean))
188    }
189
190    pub fn ratio(rational: rust_decimal::Decimal) -> Self {
191        Self::from_literal(LiteralValue::ratio_from_decimal(rational))
192    }
193
194    pub fn veto(veto: impl Into<String>) -> Self {
195        Self::Veto(VetoType::UserDefined {
196            message: Some(veto.into()),
197        })
198    }
199}
200
201#[cfg(test)]
202mod tests {
203    use super::VetoType;
204
205    #[test]
206    fn missing_row_data_display_names_list_row_and_field() {
207        let v = VetoType::MissingRowData {
208            key: "menu.2.price".to_string(),
209        };
210        assert_eq!(
211            v.to_string(),
212            "Required field 'price' was not provided on row 2 of list 'menu'."
213        );
214    }
215}