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#[non_exhaustive]
12pub enum ArithmeticOperation {
13    /// Addition
14    #[serde(rename = "ADDITION")]
15    Addition,
16
17    /// Subtraction
18    #[serde(rename = "SUBTRAKTION")]
19    Subtraction,
20
21    /// Multiplication
22    #[serde(rename = "MULTIPLIKATION")]
23    Multiplication,
24
25    /// Division
26    #[serde(rename = "DIVISION")]
27    Division,
28}
29
30impl ArithmeticOperation {
31    /// Returns the German name.
32    pub fn german_name(&self) -> &'static str {
33        match self {
34            Self::Addition => "Addition",
35            Self::Subtraction => "Subtraktion",
36            Self::Multiplication => "Multiplikation",
37            Self::Division => "Division",
38        }
39    }
40
41    /// Returns the mathematical symbol for this operation.
42    pub fn symbol(&self) -> char {
43        match self {
44            Self::Addition => '+',
45            Self::Subtraction => '-',
46            Self::Multiplication => '*',
47            Self::Division => '/',
48        }
49    }
50}
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55
56    #[test]
57    fn test_serialize() {
58        assert_eq!(
59            serde_json::to_string(&ArithmeticOperation::Addition).unwrap(),
60            r#""ADDITION""#
61        );
62        assert_eq!(
63            serde_json::to_string(&ArithmeticOperation::Division).unwrap(),
64            r#""DIVISION""#
65        );
66    }
67
68    #[test]
69    fn test_roundtrip() {
70        for op in [
71            ArithmeticOperation::Addition,
72            ArithmeticOperation::Subtraction,
73            ArithmeticOperation::Multiplication,
74            ArithmeticOperation::Division,
75        ] {
76            let json = serde_json::to_string(&op).unwrap();
77            let parsed: ArithmeticOperation = serde_json::from_str(&json).unwrap();
78            assert_eq!(op, parsed);
79        }
80    }
81
82    #[test]
83    fn test_symbol() {
84        assert_eq!(ArithmeticOperation::Addition.symbol(), '+');
85        assert_eq!(ArithmeticOperation::Subtraction.symbol(), '-');
86        assert_eq!(ArithmeticOperation::Multiplication.symbol(), '*');
87        assert_eq!(ArithmeticOperation::Division.symbol(), '/');
88    }
89}