kittycad_execution_plan/arithmetic/
operator.rs1use serde::{Deserialize, Serialize};
2use std::fmt;
3
4#[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq, Eq)]
6pub enum Operation {
7 Unary(UnaryOperation),
9 Binary(BinaryOperation),
11}
12
13#[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq, Eq)]
15pub enum BinaryOperation {
16 Add,
18 Mul,
20 Sub,
22 Div,
24 Mod,
26 Pow,
28 Log,
30 Min,
32 Max,
34}
35
36#[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq, Eq)]
38pub enum UnaryOperation {
39 Not,
41 Neg,
43 Abs,
45 Acos,
47 Asin,
49 Atan,
51 Ceil,
53 Cos,
55 Floor,
57 Ln,
59 Log10,
61 Log2,
63 Sin,
65 Sqrt,
67 Tan,
69 ToDegrees,
71 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}