aboutsummaryrefslogtreecommitdiffstats
path: root/src/function.rs
blob: bb6462d3f2a8e62b4d01df4e3c93fba8107f5137 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use crate::AngleMode;

pub enum Function {
    Sin,  // Sine
    Cos,  // Cosine
    Tan,  // Tangent
    Asin, // Inverse sine
    Acos, // Inverse cosine
    Atan, // Inverse tangent
    Deg,  // Convert from radians to degrees
    Rad,  // Convert from degrees to radians
}

impl Function {
    pub fn parse(s: &str) -> Result<Self, ParseFunctionError> {
        match s {
            "sin" => Ok(Self::Sin),
            "cos" => Ok(Self::Cos),
            "tan" => Ok(Self::Tan),
            "asin" => Ok(Self::Asin),
            "acos" => Ok(Self::Acos),
            "atan" => Ok(Self::Atan),
            "deg" => Ok(Self::Deg),
            "rad" => Ok(Self::Rad),
            _ => Err(ParseFunctionError(s.to_string())),
        }
    }

    pub fn call(&self, val: f64, angle_mode: AngleMode) -> f64 {
        match self {
            Self::Sin => match angle_mode {
                AngleMode::Degrees => val.to_radians().sin(),
                AngleMode::Radians => val.sin(),
            },
            Self::Cos => match angle_mode {
                AngleMode::Degrees => val.to_radians().cos(),
                AngleMode::Radians => val.cos(),
            },
            Self::Tan => match angle_mode {
                AngleMode::Degrees => val.to_radians().tan(),
                AngleMode::Radians => val.tan(),
            },
            Self::Asin => match angle_mode {
                AngleMode::Degrees => val.asin().to_degrees(),
                AngleMode::Radians => val.asin(),
            },
            Self::Acos => match angle_mode {
                AngleMode::Degrees => val.acos().to_degrees(),
                AngleMode::Radians => val.acos(),
            },
            Self::Atan => match angle_mode {
                AngleMode::Degrees => val.atan().to_degrees(),
                AngleMode::Radians => val.atan(),
            },
            Self::Deg => val.to_degrees(),
            Self::Rad => val.to_radians(),
        }
    }
}

pub struct ParseFunctionError(String);