cognito_user_reader/
models.rs

1use chrono::prelude::*;
2
3pub trait UserTypeExt {
4    fn creation_date(&self) -> DateTime<Utc>;
5    fn get_attribute(&self, name: &str) -> String;
6    fn get_email(&self) -> String;
7    fn attributes_values_to_string(&self, separator: &str) -> String;
8    fn attributes_keys_to_string(&self, separator: &str) -> String;
9}
10
11impl UserTypeExt for rusoto_cognito_idp::UserType {
12    fn get_email(&self) -> String {
13        self.get_attribute("email")
14    }
15
16    fn get_attribute(&self, name: &str) -> String {
17        self.attributes.as_ref().map_or_else(String::new, |attrs| {
18            attrs
19                .iter()
20                .find(|a| a.name == name)
21                .map_or_else(|| "None".to_owned(), |a| a.value.clone().unwrap())
22        })
23    }
24
25    #[allow(clippy::cast_possible_truncation)]
26    fn creation_date(&self) -> DateTime<Utc> {
27        Utc.timestamp(
28            self.user_create_date
29                .expect("No creation date for this user") as i64,
30            0,
31        )
32    }
33
34    fn attributes_values_to_string(&self, separator: &str) -> String {
35        self.attributes.as_ref().map_or_else(String::new, |attrs| {
36            attrs.iter().fold(String::new(), |acc, a| {
37                let value = a.value.as_deref().unwrap_or("No Value");
38                if acc.is_empty() {
39                    value.to_string()
40                } else {
41                    format!("{}{}{}", acc, separator, value)
42                }
43            })
44        })
45    }
46
47    fn attributes_keys_to_string(&self, separator: &str) -> String {
48        self.attributes.as_ref().map_or_else(String::new, |attrs| {
49            attrs.iter().fold(String::new(), |acc, a| {
50                if acc.is_empty() {
51                    a.name.to_string()
52                } else {
53                    format!("{}{}{}", acc, separator, a.name)
54                }
55            })
56        })
57    }
58}