shape_ast/ast/
operators.rs1use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
7pub enum RangeKind {
8 Exclusive,
10 Inclusive,
12}
13
14#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
15pub enum BinaryOp {
16 Add,
18 Sub,
19 Mul,
20 Div,
21 Mod,
22 Pow,
23
24 Greater,
26 Less,
27 GreaterEq,
28 LessEq,
29 Equal,
30 NotEqual,
31
32 FuzzyEqual, FuzzyGreater, FuzzyLess, BitAnd,
39 BitOr,
40 BitXor,
41 BitShl,
42 BitShr,
43
44 And,
46 Or,
47
48 NullCoalesce, ErrorContext, Pipe, }
55
56#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
57pub enum UnaryOp {
58 Not,
59 Neg,
60 BitNot,
61}
62
63#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
66pub enum FuzzyTolerance {
67 Absolute(f64),
69 Percentage(f64),
71}
72
73impl FuzzyTolerance {
74 pub fn is_within(&self, a: f64, b: f64) -> bool {
76 let diff = (a - b).abs();
77 match self {
78 FuzzyTolerance::Absolute(tol) => diff <= *tol,
79 FuzzyTolerance::Percentage(pct) => {
80 let avg = (a.abs() + b.abs()) / 2.0;
81 if avg == 0.0 {
82 diff == 0.0
83 } else {
84 diff / avg <= *pct
85 }
86 }
87 }
88 }
89
90 pub fn value(&self) -> f64 {
92 match self {
93 FuzzyTolerance::Absolute(v) | FuzzyTolerance::Percentage(v) => *v,
94 }
95 }
96}
97
98#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
100pub enum FuzzyOp {
101 Equal,
103 Greater,
105 Less,
107}