Skip to main content

smart_id_rust_client/models/
semantic_identifier.rs

1use crate::error::Result;
2use crate::error::SmartIdClientError;
3use serde::{Deserialize, Serialize};
4use std::fmt::Display;
5use std::str::FromStr;
6use strum_macros::{Display, EnumString};
7
8#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
9pub struct SemanticsIdentifier {
10    pub identity_type: IdentityType,
11    pub country_code: CountryCode,
12    pub identity_number: String,
13}
14
15impl SemanticsIdentifier {
16    pub fn new(
17        identity_type: IdentityType,
18        country_code: CountryCode,
19        identity_number: String,
20    ) -> Self {
21        SemanticsIdentifier {
22            identity_type,
23            country_code,
24            identity_number,
25        }
26    }
27
28    pub fn new_from_string(
29        identity_type: String,
30        country_code: String,
31        identity_number: String,
32    ) -> Result<Self> {
33        Ok(SemanticsIdentifier {
34            identity_type: IdentityType::from_str(identity_type.to_uppercase().as_str()).map_err(
35                |e| SmartIdClientError::InvalidSemanticIdentifierException(e.to_string()),
36            )?,
37            country_code: CountryCode::from_str(country_code.to_uppercase().as_str()).map_err(
38                |e| SmartIdClientError::InvalidSemanticIdentifierException(e.to_string()),
39            )?,
40            identity_number,
41        })
42    }
43
44    pub fn identifier(&self) -> String {
45        format!(
46            "{}{}-{}",
47            self.identity_type.clone(),
48            self.country_code.clone(),
49            self.identity_number.clone(),
50        )
51    }
52}
53
54impl Display for SemanticsIdentifier {
55    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
56        write!(f, "{}", self.identifier())
57    }
58}
59
60#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, EnumString, Display)]
61#[strum(serialize_all = "UPPERCASE")]
62#[allow(non_camel_case_types)]
63#[non_exhaustive]
64pub enum IdentityType {
65    PAS,
66    IDC,
67    PNO,
68}
69
70#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, EnumString, Display)]
71#[strum(serialize_all = "UPPERCASE")]
72#[allow(non_camel_case_types)]
73#[non_exhaustive]
74pub enum CountryCode {
75    EE,
76    LT,
77    LV,
78    BE,
79}
80
81#[cfg(test)]
82mod tests {
83    use super::*;
84
85    #[test]
86    fn test_new() {
87        let identifier =
88            SemanticsIdentifier::new(IdentityType::PAS, CountryCode::EE, "1234567890".to_string());
89        assert_eq!(identifier.identifier(), "PASEE-1234567890");
90    }
91
92    #[test]
93    fn test_new_from_string() {
94        let identifier = SemanticsIdentifier::new_from_string(
95            "PAS".to_string(),
96            "EE".to_string(),
97            "1234567890".to_string(),
98        )
99        .unwrap();
100        assert_eq!(identifier.identifier(), "PASEE-1234567890");
101    }
102
103    #[test]
104    fn test_new_from_string_mixed_case() {
105        let identifier = SemanticsIdentifier::new_from_string(
106            "Pas".to_string(),
107            "Ee".to_string(),
108            "1234567890".to_string(),
109        )
110        .unwrap();
111        assert_eq!(identifier.identifier(), "PASEE-1234567890");
112    }
113}