bo4e_core/com/
bonus.rs

1//! Bonus (Bonus) component.
2
3use serde::{Deserialize, Serialize};
4
5use crate::enums::Currency;
6use crate::traits::{Bo4eMeta, Bo4eObject};
7
8/// A bonus or incentive payment.
9///
10/// German: Bonus
11///
12/// # Example
13///
14/// ```rust
15/// use bo4e_core::com::Bonus;
16///
17/// let bonus = Bonus {
18///     description: Some("Neukundenbonus".to_string()),
19///     value: Some(100.0),
20///     ..Default::default()
21/// };
22/// ```
23#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
24#[serde(rename_all = "camelCase")]
25pub struct Bonus {
26    /// BO4E metadata
27    #[serde(flatten)]
28    pub meta: Bo4eMeta,
29
30    /// Description/name of the bonus (Bezeichnung)
31    #[serde(skip_serializing_if = "Option::is_none")]
32    pub description: Option<String>,
33
34    /// Bonus value (Wert)
35    #[serde(skip_serializing_if = "Option::is_none")]
36    pub value: Option<f64>,
37
38    /// Currency (Waehrung)
39    #[serde(skip_serializing_if = "Option::is_none")]
40    pub currency: Option<Currency>,
41
42    /// Conditions for receiving the bonus (Bedingungen)
43    #[serde(skip_serializing_if = "Option::is_none")]
44    pub conditions: Option<String>,
45
46    /// Whether the bonus is a one-time payment (Einmalig)
47    #[serde(skip_serializing_if = "Option::is_none")]
48    pub is_one_time: Option<bool>,
49}
50
51impl Bo4eObject for Bonus {
52    fn type_name_german() -> &'static str {
53        "Bonus"
54    }
55
56    fn type_name_english() -> &'static str {
57        "Bonus"
58    }
59
60    fn meta(&self) -> &Bo4eMeta {
61        &self.meta
62    }
63
64    fn meta_mut(&mut self) -> &mut Bo4eMeta {
65        &mut self.meta
66    }
67}
68
69#[cfg(test)]
70mod tests {
71    use super::*;
72
73    #[test]
74    fn test_new_customer_bonus() {
75        let bonus = Bonus {
76            description: Some("Neukundenbonus".to_string()),
77            value: Some(100.0),
78            currency: Some(Currency::Eur),
79            is_one_time: Some(true),
80            conditions: Some("Bei Vertragsabschluss".to_string()),
81            ..Default::default()
82        };
83
84        assert_eq!(bonus.value, Some(100.0));
85        assert_eq!(bonus.is_one_time, Some(true));
86    }
87
88    #[test]
89    fn test_loyalty_bonus() {
90        let bonus = Bonus {
91            description: Some("Treuebonus".to_string()),
92            value: Some(25.0),
93            currency: Some(Currency::Eur),
94            is_one_time: Some(false),
95            ..Default::default()
96        };
97
98        assert_eq!(bonus.is_one_time, Some(false));
99    }
100
101    #[test]
102    fn test_default() {
103        let bonus = Bonus::default();
104        assert!(bonus.description.is_none());
105        assert!(bonus.value.is_none());
106    }
107
108    #[test]
109    fn test_roundtrip() {
110        let bonus = Bonus {
111            description: Some("Online-Bonus".to_string()),
112            value: Some(50.0),
113            currency: Some(Currency::Eur),
114            conditions: Some("Bei Online-Abschluss".to_string()),
115            is_one_time: Some(true),
116            ..Default::default()
117        };
118
119        let json = serde_json::to_string(&bonus).unwrap();
120        let parsed: Bonus = serde_json::from_str(&json).unwrap();
121        assert_eq!(bonus, parsed);
122    }
123
124    #[test]
125    fn test_bo4e_object_impl() {
126        assert_eq!(Bonus::type_name_german(), "Bonus");
127        assert_eq!(Bonus::type_name_english(), "Bonus");
128    }
129}