kittycad_execution_plan/arithmetic/
operator.rs

1use serde::{Deserialize, Serialize};
2use std::fmt;
3
4/// Operations that can be applied to values in memory.
5#[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq, Eq)]
6pub enum Operation {
7    /// Requires one operand (e.g. negating a number)
8    Unary(UnaryOperation),
9    /// Requires two operands (e.g. addition)
10    Binary(BinaryOperation),
11}
12
13/// Operations that can be applied to values in memory, requiring two operands.
14#[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq, Eq)]
15pub enum BinaryOperation {
16    /// Addition
17    Add,
18    /// Multiplication
19    Mul,
20    /// Subtraction
21    Sub,
22    /// Division
23    Div,
24    /// Modulo
25    Mod,
26    /// Power
27    Pow,
28    /// Logarithm
29    Log,
30    /// Smallest of two numbers
31    Min,
32    /// Largest of two numbers
33    Max,
34}
35
36/// Operations that can be applied to a value in memory, requiring one operand.
37#[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq, Eq)]
38pub enum UnaryOperation {
39    /// Logical negation
40    Not,
41    /// Flip the sign of a signed number
42    Neg,
43    /// Get the absolute value of a number
44    Abs,
45    /// Arc cosine
46    Acos,
47    /// Arc sine
48    Asin,
49    /// Arc tangent
50    Atan,
51    /// Ceiling
52    Ceil,
53    /// Cosine
54    Cos,
55    /// Floor,
56    Floor,
57    /// Natural logarithm
58    Ln,
59    /// Logarithm base 10
60    Log10,
61    /// Logarithm base 2
62    Log2,
63    /// Sine
64    Sin,
65    /// Square root
66    Sqrt,
67    /// Tangent
68    Tan,
69    /// Convert radians to degrees
70    ToDegrees,
71    /// Convert degrees to radians
72    ToRadians,
73}
74
75impl From<BinaryOperation> for Operation {
76    fn from(value: BinaryOperation) -> Self {
77        Self::Binary(value)
78    }
79}
80
81impl From<UnaryOperation> for Operation {
82    fn from(value: UnaryOperation) -> Self {
83        Self::Unary(value)
84    }
85}
86
87impl fmt::Display for Operation {
88    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
89        match self {
90            Operation::Unary(o) => o.fmt(f),
91            Operation::Binary(o) => o.fmt(f),
92        }
93    }
94}
95
96impl fmt::Display for UnaryOperation {
97    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
98        match self {
99            UnaryOperation::Neg => "-",
100            UnaryOperation::Not => "!",
101            UnaryOperation::Abs => "abs",
102            UnaryOperation::Acos => "acos",
103            UnaryOperation::Asin => "asin",
104            UnaryOperation::Atan => "atan",
105            UnaryOperation::Ceil => "ceil",
106            UnaryOperation::Cos => "cos",
107            UnaryOperation::Floor => "floor",
108            UnaryOperation::Ln => "ln",
109            UnaryOperation::Log10 => "log10",
110            UnaryOperation::Log2 => "log2",
111            UnaryOperation::Sin => "sin",
112            UnaryOperation::Sqrt => "sqrt",
113            UnaryOperation::Tan => "tan",
114            UnaryOperation::ToDegrees => "to_degrees",
115            UnaryOperation::ToRadians => "to_radians",
116        }
117        .fmt(f)
118    }
119}
120
121impl fmt::Display for BinaryOperation {
122    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
123        match self {
124            BinaryOperation::Add => "+",
125            BinaryOperation::Mul => "*",
126            BinaryOperation::Sub => "-",
127            BinaryOperation::Div => "/",
128            BinaryOperation::Mod => "%",
129            BinaryOperation::Pow => "^",
130            BinaryOperation::Log => "log",
131            BinaryOperation::Min => "min",
132            BinaryOperation::Max => "max",
133        }
134        .fmt(f)
135    }
136}