Skip to main content

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#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
29#[cfg_attr(feature = "json-schema", schemars(rename = "Zustaendigkeit"))]
30#[serde(rename_all = "camelCase")]
31pub struct Responsibility {
32    /// BO4E metadata
33    #[serde(flatten)]
34    pub meta: Bo4eMeta,
35
36    /// Subject area classification of the contact person (Themengebiet)
37    #[serde(skip_serializing_if = "Option::is_none")]
38    #[cfg_attr(feature = "json-schema", schemars(rename = "themengebiet"))]
39    pub subject_area: Option<SubjectArea>,
40
41    /// Professional position/role of the contact person (Position)
42    #[serde(skip_serializing_if = "Option::is_none")]
43    #[cfg_attr(feature = "json-schema", schemars(rename = "position"))]
44    pub position: Option<String>,
45
46    /// Department where the contact person works (Abteilung)
47    #[serde(skip_serializing_if = "Option::is_none")]
48    #[cfg_attr(feature = "json-schema", schemars(rename = "abteilung"))]
49    pub department: Option<String>,
50}
51
52impl Bo4eObject for Responsibility {
53    fn type_name_german() -> &'static str {
54        "Zustaendigkeit"
55    }
56
57    fn type_name_english() -> &'static str {
58        "Responsibility"
59    }
60
61    fn meta(&self) -> &Bo4eMeta {
62        &self.meta
63    }
64
65    fn meta_mut(&mut self) -> &mut Bo4eMeta {
66        &mut self.meta
67    }
68}
69
70#[cfg(test)]
71mod tests {
72    use super::*;
73
74    #[test]
75    fn test_responsibility_default() {
76        let resp = Responsibility::default();
77        assert!(resp.subject_area.is_none());
78        assert!(resp.position.is_none());
79        assert!(resp.department.is_none());
80    }
81
82    #[test]
83    fn test_responsibility_serialize() {
84        let resp = Responsibility {
85            subject_area: Some(SubjectArea::MarketCommunication),
86            position: Some("Team Lead".to_string()),
87            department: Some("Market Communication".to_string()),
88            ..Default::default()
89        };
90
91        let json = serde_json::to_string(&resp).unwrap();
92        assert!(json.contains(r#""subjectArea":"MARKTKOMMUNIKATION""#));
93        assert!(json.contains(r#""position":"Team Lead""#));
94        assert!(json.contains(r#""department":"Market Communication""#));
95    }
96
97    #[test]
98    fn test_responsibility_roundtrip() {
99        let resp = Responsibility {
100            meta: Bo4eMeta::with_type("Zustaendigkeit"),
101            subject_area: Some(SubjectArea::Balancing),
102            position: Some("Analyst".to_string()),
103            department: Some("Energy Trading".to_string()),
104        };
105
106        let json = serde_json::to_string(&resp).unwrap();
107        let parsed: Responsibility = serde_json::from_str(&json).unwrap();
108        assert_eq!(resp, parsed);
109    }
110
111    #[test]
112    fn test_bo4e_object_impl() {
113        assert_eq!(Responsibility::type_name_german(), "Zustaendigkeit");
114        assert_eq!(Responsibility::type_name_english(), "Responsibility");
115    }
116}