Skip to main content

bo4e_core/enums/
title.rs

1//! Title (Titel) enumeration.
2
3use serde::{Deserialize, Serialize};
4
5/// Academic or professional title of a person.
6///
7/// German: Titel
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
9#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
10#[cfg_attr(feature = "json-schema", schemars(rename = "Titel"))]
11#[non_exhaustive]
12pub enum Title {
13    /// Doctor (Doktor)
14    #[serde(rename = "DR")]
15    Dr,
16
17    /// Professor
18    #[serde(rename = "PROF")]
19    Prof,
20
21    /// Professor Doctor
22    #[serde(rename = "PROF_DR")]
23    ProfDr,
24}
25
26impl Title {
27    /// Returns the German name.
28    pub fn german_name(&self) -> &'static str {
29        match self {
30            Self::Dr => "Dr.",
31            Self::Prof => "Prof.",
32            Self::ProfDr => "Prof. Dr.",
33        }
34    }
35}
36
37#[cfg(test)]
38mod tests {
39    use super::*;
40
41    #[test]
42    fn test_serialize() {
43        assert_eq!(serde_json::to_string(&Title::Dr).unwrap(), r#""DR""#);
44        assert_eq!(serde_json::to_string(&Title::Prof).unwrap(), r#""PROF""#);
45        assert_eq!(
46            serde_json::to_string(&Title::ProfDr).unwrap(),
47            r#""PROF_DR""#
48        );
49    }
50
51    #[test]
52    fn test_deserialize() {
53        assert_eq!(serde_json::from_str::<Title>(r#""DR""#).unwrap(), Title::Dr);
54        assert_eq!(
55            serde_json::from_str::<Title>(r#""PROF_DR""#).unwrap(),
56            Title::ProfDr
57        );
58    }
59
60    #[test]
61    fn test_roundtrip() {
62        for title in [Title::Dr, Title::Prof, Title::ProfDr] {
63            let json = serde_json::to_string(&title).unwrap();
64            let parsed: Title = serde_json::from_str(&json).unwrap();
65            assert_eq!(title, parsed);
66        }
67    }
68}