lambda_appsync/aws_scalars/
email.rs

1impl_new_string!(no_from AWSEmail);
2
3// An Email address should always be lowercase
4impl From<String> for AWSEmail {
5    fn from(value: String) -> Self {
6        // We could simply call `to_lowercase` and be done with it
7        // but this way we avoid useless allocation be using the already
8        // allocated String if possible
9        if value.chars().all(|c| !c.is_uppercase()) {
10            Self(value)
11        } else {
12            Self(value.to_lowercase())
13        }
14    }
15}
16impl From<&str> for AWSEmail {
17    fn from(value: &str) -> Self {
18        Self(value.to_lowercase())
19    }
20}
21#[cfg(test)]
22mod tests {
23    use super::*;
24
25    #[test]
26    fn email_lowercase() {
27        let email = AWSEmail::from("TEST@EXAMPLE.COM");
28        assert_eq!(*email, "test@example.com");
29    }
30
31    #[test]
32    fn email_mixed_case() {
33        let email = AWSEmail::from("Test@Example.com");
34        assert_eq!(*email, "test@example.com");
35    }
36
37    #[test]
38    fn email_already_lowercase() {
39        let input = "test@example.com".to_string();
40        let email = AWSEmail::from(input.clone());
41        assert_eq!(*email, input);
42    }
43
44    #[test]
45    fn email_from_str() {
46        let email = AWSEmail::from("Test@Example.com");
47        assert_eq!(*email, "test@example.com");
48    }
49
50    #[test]
51    fn email_into_string() {
52        let email = AWSEmail::from("test@example.com");
53        let email_string: String = email.into();
54        assert_eq!(email_string, "test@example.com");
55    }
56    #[test]
57    fn email_display() {
58        let value = "test@example.com";
59        let email = AWSEmail::from(value);
60        assert_eq!(email.to_string(), value);
61    }
62}