slang_backend/value/operations/
logical.rs

1use super::super::Value;
2
3/// Logical operations on values
4pub trait LogicalOps {
5    /// Performs logical NOT on a boolean value.
6    ///
7    /// ### Returns
8    /// * The negated boolean value
9    /// * An error message if the type is incompatible
10    fn not(&self) -> Result<Self, String>
11    where
12        Self: Sized;
13
14    /// Performs logical AND between two boolean values.
15    ///
16    /// ### Arguments
17    /// * `other` - The other value
18    ///
19    /// ### Returns
20    /// * The result of the AND operation
21    /// * An error message if either value is not a boolean
22    fn and(&self, other: &Self) -> Result<Self, String>
23    where
24        Self: Sized;
25
26    /// Performs logical OR between two boolean values.
27    ///
28    /// ### Arguments
29    /// * `other` - The other value
30    ///
31    /// ### Returns
32    /// * The result of the OR operation
33    /// * An error message if either value is not a boolean
34    fn or(&self, other: &Self) -> Result<Self, String>
35    where
36        Self: Sized;
37}
38
39impl LogicalOps for Value {
40    fn not(&self) -> Result<Value, String> {
41        match self {
42            Value::Boolean(b) => Ok(Value::Boolean(!b)),
43            _ => Err("Logical NOT operator requires a boolean operand".to_string()),
44        }
45    }
46
47    fn and(&self, other: &Self) -> Result<Value, String> {
48        match (self, other) {
49            (Value::Boolean(a), Value::Boolean(b)) => Ok(Value::Boolean(*a && *b)),
50            _ => Err("Logical AND operator requires boolean operands".to_string()),
51        }
52    }
53
54    fn or(&self, other: &Self) -> Result<Value, String> {
55        match (self, other) {
56            (Value::Boolean(a), Value::Boolean(b)) => Ok(Value::Boolean(*a || *b)),
57            _ => Err("Logical OR operator requires boolean operands".to_string()),
58        }
59    }
60}