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}
21impl core::str::FromStr for AWSEmail {
22    type Err = core::convert::Infallible;
23    fn from_str(s: &str) -> Result<Self, Self::Err> {
24        Ok(Self::from(s))
25    }
26}
27#[cfg(test)]
28mod tests {
29    use super::*;
30
31    #[test]
32    fn email_lowercase() {
33        let email = AWSEmail::from("TEST@EXAMPLE.COM");
34        assert_eq!(*email, "test@example.com");
35    }
36
37    #[test]
38    fn email_mixed_case() {
39        let email = AWSEmail::from("Test@Example.com");
40        assert_eq!(*email, "test@example.com");
41    }
42
43    #[test]
44    fn email_already_lowercase() {
45        let input = "test@example.com".to_string();
46        let email = AWSEmail::from(input.clone());
47        assert_eq!(*email, input);
48    }
49
50    #[test]
51    fn email_from_str() {
52        let email = AWSEmail::from("Test@Example.com");
53        assert_eq!(*email, "test@example.com");
54    }
55
56    #[test]
57    fn email_into_string() {
58        let email = AWSEmail::from("test@example.com");
59        let email_string: String = email.into();
60        assert_eq!(email_string, "test@example.com");
61    }
62    #[test]
63    fn email_display() {
64        let value = "test@example.com";
65        let email = AWSEmail::from(value);
66        assert_eq!(email.to_string(), value);
67    }
68}