Skip to main content

raekna_common/
function_name.rs

1use std::{fmt, str::FromStr};
2
3use crate::errors::{CommonError, CommonResult};
4
5#[derive(Debug, Copy, Clone, PartialEq, Eq)]
6pub enum FunctionName {
7    // Arithmetic
8    Add,
9    Subtract,
10    Multiply,
11    Divide,
12    Modulus,
13    Power,
14    Negate,
15    // Trigonometry
16    Sin,
17    Cos,
18    Tan,
19    SinH,
20    CosH,
21    TanH,
22    ArcSin,
23    ArcCos,
24    ArcTan,
25    ArcSinH,
26    ArcCosH,
27    ArcTanH,
28    // Misc math
29    SquareRoot,
30    CubeRoot,
31    Factorial,
32    Log,
33    Log2,
34    Log10,
35    Ln,
36    Abs,
37    // Rounding
38    Ceil,
39    CeilPrec,
40    Floor,
41    FloorPrec,
42    Round,
43    RoundPrec,
44    Trunc,
45    TruncPrec,
46    // Comparisons
47    Max,
48    Min,
49}
50
51impl FunctionName {
52    pub fn num_arguments(&self) -> usize {
53        match self {
54            Self::SquareRoot
55            | Self::CubeRoot
56            | Self::Log2
57            | Self::Log10
58            | Self::Ln
59            | Self::Abs
60            | Self::Ceil
61            | Self::Floor
62            | Self::Round
63            | Self::Trunc
64            | Self::Factorial
65            | Self::Negate
66            | Self::Sin
67            | Self::Cos
68            | Self::Tan
69            | Self::SinH
70            | Self::CosH
71            | Self::TanH
72            | Self::ArcSin
73            | Self::ArcCos
74            | Self::ArcTan
75            | Self::ArcSinH
76            | Self::ArcCosH
77            | Self::ArcTanH => 1,
78            Self::Max
79            | Self::Min
80            | Self::Add
81            | Self::Subtract
82            | Self::Multiply
83            | Self::Divide
84            | Self::Modulus
85            | Self::Power
86            | Self::Log
87            | Self::CeilPrec
88            | Self::FloorPrec
89            | Self::RoundPrec
90            | Self::TruncPrec => 2,
91        }
92    }
93}
94
95impl FromStr for FunctionName {
96    type Err = CommonError;
97
98    fn from_str(arg: &str) -> CommonResult<Self> {
99        match arg.to_lowercase().as_str() {
100            "sqrt" | "squareroot" | "square_root" => Ok(Self::SquareRoot),
101            "fact" | "factorial" => Ok(Self::Factorial),
102            "neg" | "negate" => Ok(Self::Negate),
103            "min" | "minimum" => Ok(Self::Min),
104            "max" | "maximum" => Ok(Self::Max),
105            "add" => Ok(Self::Add),
106            "sub" | "subtract" => Ok(Self::Subtract),
107            "mul" | "multiply" => Ok(Self::Multiply),
108            "div" | "divide" => Ok(Self::Divide),
109            "mod" | "modulus" => Ok(Self::Modulus),
110            "pow" | "power" => Ok(Self::Power),
111            "sin" => Ok(Self::Sin),
112            "cos" => Ok(Self::Cos),
113            "tan" => Ok(Self::Tan),
114            "sinh" => Ok(Self::SinH),
115            "cosh" => Ok(Self::CosH),
116            "tanh" => Ok(Self::TanH),
117            "asin" | "arcsin" => Ok(Self::ArcSin),
118            "acos" | "arccos" => Ok(Self::ArcCos),
119            "atan" | "arctan" => Ok(Self::ArcTan),
120            "asinh" | "arcsinh" => Ok(Self::ArcSinH),
121            "acosh" | "arccosh" => Ok(Self::ArcCosH),
122            "atanh" | "arctanh" => Ok(Self::ArcTanH),
123            "cbrt" | "cuberoot" | "cube_root" => Ok(Self::CubeRoot),
124            "log" => Ok(Self::Log),
125            "log2" => Ok(Self::Log2),
126            "log10" => Ok(Self::Log10),
127            "ln" => Ok(Self::Ln),
128            "abs" => Ok(Self::Abs),
129            "ceil" => Ok(Self::Ceil),
130            "floor" => Ok(Self::Floor),
131            "round" => Ok(Self::Round),
132            "trunc" => Ok(Self::Trunc),
133            _ => Err(CommonError::UnknownFunctionName(arg.to_owned())),
134        }
135    }
136}
137
138impl fmt::Display for FunctionName {
139    fn fmt(&self, fmt: &mut fmt::Formatter) -> std::result::Result<(), std::fmt::Error> {
140        match self {
141            Self::SquareRoot => write!(fmt, "sqrt"),
142            Self::CubeRoot => write!(fmt, "cbrt"),
143            Self::Factorial => write!(fmt, "factorial"),
144            Self::Negate => write!(fmt, "negate"),
145            Self::Max => write!(fmt, "max"),
146            Self::Min => write!(fmt, "min"),
147            Self::Add => write!(fmt, "add"),
148            Self::Subtract => write!(fmt, "sub"),
149            Self::Multiply => write!(fmt, "mul"),
150            Self::Divide => write!(fmt, "div"),
151            Self::Modulus => write!(fmt, "mod"),
152            Self::Power => write!(fmt, "pow"),
153            Self::Sin => write!(fmt, "sin"),
154            Self::Cos => write!(fmt, "cos"),
155            Self::Tan => write!(fmt, "tan"),
156            Self::SinH => write!(fmt, "sinh"),
157            Self::CosH => write!(fmt, "cosh"),
158            Self::TanH => write!(fmt, "tanh"),
159            Self::ArcSin => write!(fmt, "asin"),
160            Self::ArcCos => write!(fmt, "acos"),
161            Self::ArcTan => write!(fmt, "atan"),
162            Self::ArcSinH => write!(fmt, "asinh"),
163            Self::ArcCosH => write!(fmt, "acosh"),
164            Self::ArcTanH => write!(fmt, "atanh"),
165            Self::Log => write!(fmt, "log"),
166            Self::Log2 => write!(fmt, "log2"),
167            Self::Log10 => write!(fmt, "log10"),
168            Self::Ln => write!(fmt, "ln"),
169            Self::Abs => write!(fmt, "abs"),
170            Self::Ceil => write!(fmt, "ceil"),
171            Self::Floor => write!(fmt, "floor"),
172            Self::Round => write!(fmt, "round"),
173            Self::CeilPrec => write!(fmt, "ceil with precision"),
174            Self::FloorPrec => write!(fmt, "floor with precision"),
175            Self::RoundPrec => write!(fmt, "round with precision"),
176            Self::Trunc => write!(fmt, "trunc"),
177            Self::TruncPrec => write!(fmt, "trunc with precision"),
178        }
179    }
180}