acton_ern/model/
account.rs

1use std::fmt;
2
3use derive_more::{AsRef, From, Into};
4
5/// Represents an account identifier in the ERN (Entity Resource Name) system.
6#[derive(AsRef, From, Into, Eq, Debug, PartialEq, Clone, Hash, PartialOrd)]
7pub struct Account(pub(crate) String);
8
9impl Account {
10    pub fn as_str(&self) -> &str {
11        &self.0
12    }
13    pub fn new(value: impl Into<String>) -> Self {
14        Account(value.into())
15    }
16    pub fn into_owned(self) -> Account {
17        Account(self.0.to_string())
18    }
19}
20
21impl Default for Account {
22    fn default() -> Self {
23        Account("component".to_string())
24    }
25}
26
27impl fmt::Display for Account {
28    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29        write!(f, "{}", self.0)
30    }
31}
32
33impl std::str::FromStr for Account {
34    type Err = std::convert::Infallible;
35
36    fn from_str(s: &str) -> Result<Self, Self::Err> {
37        Ok(Account(s.to_string()))
38    }
39}
40//
41// impl From<Account> for String {
42//     fn from(domain: Account) -> Self {
43//         domain.0
44//     }
45// }
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50
51    #[test]
52    fn test_account_creation() {
53        let account = Account::new("test123");
54        assert_eq!(account.as_str(), "test123");
55    }
56
57    #[test]
58    fn test_account_default() {
59        let account = Account::default();
60        assert_eq!(account.as_str(), "component");
61    }
62
63    #[test]
64    fn test_account_display() {
65        let account = Account::new("example456");
66        assert_eq!(format!("{}", account), "example456");
67    }
68
69    #[test]
70    fn test_account_from_str() {
71        let account: Account = "test789".parse().unwrap();
72        assert_eq!(account.as_str(), "test789");
73    }
74
75    #[test]
76    fn test_account_equality() {
77        let account1 = Account::new("test123");
78        let account2 = Account::new("test123");
79        let account3 = Account::new("other456");
80        assert_eq!(account1, account2);
81        assert_ne!(account1, account3);
82    }
83
84    #[test]
85    fn test_account_into_string() {
86        let account = Account::new("test123");
87        let string: String = account.into();
88        assert_eq!(string, "test123");
89    }
90}