metrics_evaluation/
compare.rs

1use crate::{value::Value, Calculation};
2
3/// Logic for comparisons
4#[derive(Debug, Clone, Copy, PartialEq)]
5pub enum Logic {
6    And,
7    Or,
8}
9
10/// Comparison-operators
11#[derive(Debug, PartialEq, Clone, Copy)]
12pub enum Operator {
13    Equal,        // ==
14    NotEqual,     // !=
15    GreaterEqual, // >=
16    LessEqual,    // <=
17    Greater,      // >
18    Less,         // <
19}
20
21/// Helper to bind `compare` with our custom [Operator] to a struct
22pub trait Compareable {
23    /// Compare self with `against` using the given [Operator]
24    fn compare(&self, other: &Self, operator: Operator) -> bool;
25}
26
27/// Defines if a comparison is against a value or against another variable
28#[derive(Debug, PartialEq)]
29pub enum ComparisonType {
30    /// A comparison of a variable against a fixed value (wich optional calculations)
31    Value(Value, Vec<Calculation>),
32    /// A comparison of a variable against an other variable (wich optional calculations)
33    Variable(String, Vec<Calculation>),
34}
35
36impl ComparisonType {
37    pub fn with_calculation(&mut self, calculation: Calculation) {
38        match self {
39            Self::Value(_, calculations) => calculations.push(calculation),
40            Self::Variable(_, calculations) => calculations.push(calculation),
41        }
42    }
43}
44
45#[derive(Debug, PartialEq)]
46pub struct Comparison {
47    /// Left-Hand-Side of the comparison (which the rhs will be compared to)
48    pub what: ComparisonType,
49    /// [Operator] to use for the comparison
50    pub operator: Operator,
51    /// Right-Hand-Side to compare the content of `lhs` to
52    pub against: ComparisonType,
53}
54
55/// Triplet (variable-name, operator, value) to [Comparison] conversion
56impl From<(&str, Operator, Value)> for Comparison {
57    fn from((variable_name, operator, value): (&str, Operator, Value)) -> Self {
58        Self {
59            what: ComparisonType::Variable(variable_name.into(), Vec::new()),
60            operator,
61            against: ComparisonType::Value(value, Vec::new()),
62        }
63    }
64}
65
66/// Triplet (variable-name, operator, variable-name) to [Comparison] conversion
67impl From<(&str, Operator, &str)> for Comparison {
68    fn from((variable_name, operator, against_variable_name): (&str, Operator, &str)) -> Self {
69        Self {
70            what: ComparisonType::Variable(variable_name.into(), Vec::new()),
71            operator,
72            against: ComparisonType::Variable(against_variable_name.into(), Vec::new()),
73        }
74    }
75}