bo4e_core/com/
responsibility.rs

1//! Responsibility (Zustaendigkeit) component.
2
3use serde::{Deserialize, Serialize};
4
5use crate::enums::SubjectArea;
6use crate::traits::{Bo4eMeta, Bo4eObject};
7
8/// Area of responsibility for a contact person.
9///
10/// Contains the temporal assignment of a contact person to departments and responsibilities.
11///
12/// German: Zustaendigkeit
13///
14/// # Example
15///
16/// ```rust
17/// use bo4e_core::com::Responsibility;
18/// use bo4e_core::enums::SubjectArea;
19///
20/// let resp = Responsibility {
21///     subject_area: Some(SubjectArea::MarketCommunication),
22///     position: Some("Manager".to_string()),
23///     department: Some("IT".to_string()),
24///     ..Default::default()
25/// };
26/// ```
27#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
28#[serde(rename_all = "camelCase")]
29pub struct Responsibility {
30    /// BO4E metadata
31    #[serde(flatten)]
32    pub meta: Bo4eMeta,
33
34    /// Subject area classification of the contact person (Themengebiet)
35    #[serde(skip_serializing_if = "Option::is_none")]
36    pub subject_area: Option<SubjectArea>,
37
38    /// Professional position/role of the contact person (Position)
39    #[serde(skip_serializing_if = "Option::is_none")]
40    pub position: Option<String>,
41
42    /// Department where the contact person works (Abteilung)
43    #[serde(skip_serializing_if = "Option::is_none")]
44    pub department: Option<String>,
45}
46
47impl Bo4eObject for Responsibility {
48    fn type_name_german() -> &'static str {
49        "Zustaendigkeit"
50    }
51
52    fn type_name_english() -> &'static str {
53        "Responsibility"
54    }
55
56    fn meta(&self) -> &Bo4eMeta {
57        &self.meta
58    }
59
60    fn meta_mut(&mut self) -> &mut Bo4eMeta {
61        &mut self.meta
62    }
63}
64
65#[cfg(test)]
66mod tests {
67    use super::*;
68
69    #[test]
70    fn test_responsibility_default() {
71        let resp = Responsibility::default();
72        assert!(resp.subject_area.is_none());
73        assert!(resp.position.is_none());
74        assert!(resp.department.is_none());
75    }
76
77    #[test]
78    fn test_responsibility_serialize() {
79        let resp = Responsibility {
80            subject_area: Some(SubjectArea::MarketCommunication),
81            position: Some("Team Lead".to_string()),
82            department: Some("Market Communication".to_string()),
83            ..Default::default()
84        };
85
86        let json = serde_json::to_string(&resp).unwrap();
87        assert!(json.contains(r#""subjectArea":"MARKTKOMMUNIKATION""#));
88        assert!(json.contains(r#""position":"Team Lead""#));
89        assert!(json.contains(r#""department":"Market Communication""#));
90    }
91
92    #[test]
93    fn test_responsibility_roundtrip() {
94        let resp = Responsibility {
95            meta: Bo4eMeta::with_type("Zustaendigkeit"),
96            subject_area: Some(SubjectArea::Balancing),
97            position: Some("Analyst".to_string()),
98            department: Some("Energy Trading".to_string()),
99        };
100
101        let json = serde_json::to_string(&resp).unwrap();
102        let parsed: Responsibility = serde_json::from_str(&json).unwrap();
103        assert_eq!(resp, parsed);
104    }
105
106    #[test]
107    fn test_bo4e_object_impl() {
108        assert_eq!(Responsibility::type_name_german(), "Zustaendigkeit");
109        assert_eq!(Responsibility::type_name_english(), "Responsibility");
110    }
111}