lemma/
operation_result.rs

1use crate::semantic::LiteralValue;
2use crate::LemmaError;
3
4/// Result of an operation (evaluating a rule or expression)
5#[derive(Debug, Clone, PartialEq)]
6pub enum OperationResult {
7    /// Operation produced a value
8    Value(LiteralValue),
9    /// Operation was vetoed (valid result, no value)
10    Veto(Option<String>),
11}
12
13impl OperationResult {
14    /// Check if this is a vetoed result
15    pub fn is_vetoed(&self) -> bool {
16        matches!(self, OperationResult::Veto(_))
17    }
18
19    /// Get the value if present, None if vetoed
20    pub fn value(&self) -> Option<&LiteralValue> {
21        match self {
22            OperationResult::Value(v) => Some(v),
23            OperationResult::Veto(_) => None,
24        }
25    }
26
27    /// Get the value or return an error if vetoed
28    /// This makes it explicit that the caller expects a value and provides better error messages
29    pub fn expect_value(&self, context: &str) -> Result<&LiteralValue, LemmaError> {
30        match self {
31            OperationResult::Value(v) => Ok(v),
32            OperationResult::Veto(msg) => Err(LemmaError::Engine(format!(
33                "Expected value in {}, but got veto{}",
34                context,
35                msg.as_ref().map(|m| format!(": {}", m)).unwrap_or_default()
36            ))),
37        }
38    }
39
40    /// Get the veto message if vetoed, None otherwise
41    pub fn veto_message(&self) -> Option<&Option<String>> {
42        match self {
43            OperationResult::Veto(msg) => Some(msg),
44            OperationResult::Value(_) => None,
45        }
46    }
47}