use std::fmt;
use std::hash::Hash;
use std::hash::Hasher;
use crate::expression::Expression;
use cheetah_string::CheetahString;
pub trait BinaryExpression: Expression + Send + Sync {
fn left(&self) -> &dyn Expression;
fn right(&self) -> &dyn Expression;
fn get_expression_symbol(&self) -> Option<&CheetahString>;
fn set_left(&mut self, expression: Box<dyn Expression>);
fn set_right(&mut self, expression: Box<dyn Expression>);
}
pub struct BaseBinaryExpression {
left: Box<dyn Expression>,
right: Box<dyn Expression>,
symbol: CheetahString,
}
impl BaseBinaryExpression {
pub fn new(left: Box<dyn Expression>, right: Box<dyn Expression>, symbol: CheetahString) -> Self {
Self { left, right, symbol }
}
}
impl BinaryExpression for BaseBinaryExpression {
fn left(&self) -> &dyn Expression {
self.left.as_ref()
}
fn right(&self) -> &dyn Expression {
self.right.as_ref()
}
fn get_expression_symbol(&self) -> Option<&CheetahString> {
Some(&self.symbol)
}
fn set_left(&mut self, expression: Box<dyn Expression>) {
self.left = expression;
}
fn set_right(&mut self, expression: Box<dyn Expression>) {
self.right = expression;
}
}
impl fmt::Display for BaseBinaryExpression {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "({} {} {})", self.left, self.symbol, self.right)
}
}
impl Hash for BaseBinaryExpression {
fn hash<H: Hasher>(&self, state: &mut H) {
self.to_string().hash(state);
}
}
impl PartialEq for BaseBinaryExpression {
fn eq(&self, other: &Self) -> bool {
self.to_string() == other.to_string()
}
}
impl Eq for BaseBinaryExpression {}
impl fmt::Debug for BaseBinaryExpression {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self)
}
}
impl Expression for BaseBinaryExpression {
fn evaluate(
&self,
_context: &dyn crate::expression::EvaluationContext,
) -> Result<crate::expression::Value, crate::expression::EvaluationError> {
Ok(crate::expression::Value::String(cheetah_string::CheetahString::from(
self.to_string(),
)))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::expression::MessageEvaluationContext;
#[test]
fn test_base_binary_expression_new() {
use crate::expression::AlwaysTrueExpression;
let left = Box::new(AlwaysTrueExpression) as Box<dyn Expression>;
let right = Box::new(AlwaysTrueExpression) as Box<dyn Expression>;
let expr = BaseBinaryExpression::new(left, right, CheetahString::from("+"));
assert_eq!(expr.get_expression_symbol(), Some(&CheetahString::from("+")));
}
#[test]
fn test_base_binary_expression_display() {
use crate::expression::AlwaysTrueExpression;
let left = Box::new(AlwaysTrueExpression) as Box<dyn Expression>;
let right = Box::new(AlwaysTrueExpression) as Box<dyn Expression>;
let expr = BaseBinaryExpression::new(left, right, CheetahString::from("+"));
assert_eq!(expr.to_string(), "(TRUE + TRUE)");
}
#[test]
fn test_base_binary_expression_equality() {
use crate::expression::AlwaysFalseExpression;
use crate::expression::AlwaysTrueExpression;
let expr1 = BaseBinaryExpression::new(
Box::new(AlwaysTrueExpression) as Box<dyn Expression>,
Box::new(AlwaysFalseExpression) as Box<dyn Expression>,
CheetahString::from("+"),
);
let expr2 = BaseBinaryExpression::new(
Box::new(AlwaysTrueExpression) as Box<dyn Expression>,
Box::new(AlwaysFalseExpression) as Box<dyn Expression>,
CheetahString::from("+"),
);
assert_eq!(expr1, expr2);
}
#[test]
fn test_base_binary_expression_set_left_right() {
use crate::expression::AlwaysFalseExpression;
use crate::expression::AlwaysTrueExpression;
let mut expr = BaseBinaryExpression::new(
Box::new(AlwaysTrueExpression) as Box<dyn Expression>,
Box::new(AlwaysTrueExpression) as Box<dyn Expression>,
CheetahString::from("+"),
);
assert_eq!(expr.to_string(), "(TRUE + TRUE)");
expr.set_left(Box::new(AlwaysFalseExpression) as Box<dyn Expression>);
assert_eq!(expr.to_string(), "(FALSE + TRUE)");
expr.set_right(Box::new(AlwaysFalseExpression) as Box<dyn Expression>);
assert_eq!(expr.to_string(), "(FALSE + FALSE)");
}
#[test]
fn test_base_binary_expression_evaluate() {
use crate::expression::AlwaysTrueExpression;
use crate::expression::Value;
let expr = BaseBinaryExpression::new(
Box::new(AlwaysTrueExpression) as Box<dyn Expression>,
Box::new(AlwaysTrueExpression) as Box<dyn Expression>,
CheetahString::from("+"),
);
let context = MessageEvaluationContext::new();
let result = expr.evaluate(&context);
assert!(result.is_ok());
let value = result.unwrap();
match value {
Value::String(s) => assert_eq!(s.as_str(), "(TRUE + TRUE)"),
_ => panic!("Expected Value::String, got {:?}", value),
}
}
}