Skip to main content

bo4e_core/enums/
arithmetic_operation.rs

1//! Arithmetic operation (ArithmetischeOperation) enumeration.
2
3use serde::{Deserialize, Serialize};
4
5/// Arithmetic operation type.
6///
7/// Defines arithmetic operations that can be applied.
8///
9/// German: ArithmetischeOperation
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
11#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
12#[cfg_attr(feature = "json-schema", schemars(rename = "ArithmetischeOperation"))]
13#[non_exhaustive]
14pub enum ArithmeticOperation {
15    /// Addition
16    #[serde(rename = "ADDITION")]
17    Addition,
18
19    /// Subtraction
20    #[serde(rename = "SUBTRAKTION")]
21    Subtraction,
22
23    /// Multiplication
24    #[serde(rename = "MULTIPLIKATION")]
25    Multiplication,
26
27    /// Division
28    #[serde(rename = "DIVISION")]
29    Division,
30}
31
32impl ArithmeticOperation {
33    /// Returns the German name.
34    pub fn german_name(&self) -> &'static str {
35        match self {
36            Self::Addition => "Addition",
37            Self::Subtraction => "Subtraktion",
38            Self::Multiplication => "Multiplikation",
39            Self::Division => "Division",
40        }
41    }
42
43    /// Returns the mathematical symbol for this operation.
44    pub fn symbol(&self) -> char {
45        match self {
46            Self::Addition => '+',
47            Self::Subtraction => '-',
48            Self::Multiplication => '*',
49            Self::Division => '/',
50        }
51    }
52}
53
54#[cfg(test)]
55mod tests {
56    use super::*;
57
58    #[test]
59    fn test_serialize() {
60        assert_eq!(
61            serde_json::to_string(&ArithmeticOperation::Addition).unwrap(),
62            r#""ADDITION""#
63        );
64        assert_eq!(
65            serde_json::to_string(&ArithmeticOperation::Division).unwrap(),
66            r#""DIVISION""#
67        );
68    }
69
70    #[test]
71    fn test_roundtrip() {
72        for op in [
73            ArithmeticOperation::Addition,
74            ArithmeticOperation::Subtraction,
75            ArithmeticOperation::Multiplication,
76            ArithmeticOperation::Division,
77        ] {
78            let json = serde_json::to_string(&op).unwrap();
79            let parsed: ArithmeticOperation = serde_json::from_str(&json).unwrap();
80            assert_eq!(op, parsed);
81        }
82    }
83
84    #[test]
85    fn test_symbol() {
86        assert_eq!(ArithmeticOperation::Addition.symbol(), '+');
87        assert_eq!(ArithmeticOperation::Subtraction.symbol(), '-');
88        assert_eq!(ArithmeticOperation::Multiplication.symbol(), '*');
89        assert_eq!(ArithmeticOperation::Division.symbol(), '/');
90    }
91}