Skip to main content

bo4e_core/enums/
rounding_mode.rs

1//! Rounding mode (Rundungsverfahren) enumeration.
2
3use serde::{Deserialize, Serialize};
4
5/// Rounding mode/method.
6///
7/// Defines how numerical values should be rounded.
8///
9/// German: Rundungsverfahren
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 = "Rundungsverfahren"))]
13#[non_exhaustive]
14pub enum RoundingMode {
15    /// No rounding (Keine Rundung)
16    #[serde(rename = "KEINE")]
17    None,
18
19    /// Commercial rounding / round half up (Kaufmännische Rundung)
20    #[serde(rename = "KAUFMAENNISCH")]
21    Commercial,
22
23    /// Round down / floor (Abrunden)
24    #[serde(rename = "ABRUNDEN")]
25    Floor,
26
27    /// Round up / ceiling (Aufrunden)
28    #[serde(rename = "AUFRUNDEN")]
29    Ceiling,
30}
31
32impl RoundingMode {
33    /// Returns the German name.
34    pub fn german_name(&self) -> &'static str {
35        match self {
36            Self::None => "Keine Rundung",
37            Self::Commercial => "Kaufmännische Rundung",
38            Self::Floor => "Abrunden",
39            Self::Ceiling => "Aufrunden",
40        }
41    }
42}
43
44#[cfg(test)]
45mod tests {
46    use super::*;
47
48    #[test]
49    fn test_serialize() {
50        assert_eq!(
51            serde_json::to_string(&RoundingMode::Commercial).unwrap(),
52            r#""KAUFMAENNISCH""#
53        );
54    }
55
56    #[test]
57    fn test_roundtrip() {
58        for mode in [
59            RoundingMode::None,
60            RoundingMode::Commercial,
61            RoundingMode::Floor,
62            RoundingMode::Ceiling,
63        ] {
64            let json = serde_json::to_string(&mode).unwrap();
65            let parsed: RoundingMode = serde_json::from_str(&json).unwrap();
66            assert_eq!(mode, parsed);
67        }
68    }
69}