1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use crate::{marker::*, StrongBuf, Validator};
use std::marker::PhantomData;
use thiserror::Error;

#[derive(Debug, Error)]
#[error("{raw} isn't valid")]
pub struct InvalidEmail {
    raw: String
}

#[cfg_attr(feature = "diesel", derive(SqlType, QueryId))]
pub enum Email {}

impl PartialEqTransparent for Email {}
impl EqTransparent for Email {}
impl PartialOrdTransparent for Email {}
impl OrdTransparent for Email {}
impl HashTransparent for Email {}
impl DebugTransparent for Email {}
impl DisplayTransparent for Email {}

impl Validator for Email {
    type Err = InvalidEmail;

    fn validate(raw: &str) -> Result<(), Self::Err> {
        if validator::validate_email(raw) {
            Ok(())
        } else {
            Err(InvalidEmail { raw: raw.into() })
        }
    }
}

#[cfg_attr(feature = "diesel", derive(SqlType, QueryId))]
pub struct Name<T> {
    phantom: PhantomData<T>
}

impl<T> PartialEqTransparent for Name<T> {}
impl<T> EqTransparent for Name<T> {}
impl<T> PartialOrdTransparent for Name<T> {}
impl<T> OrdTransparent for Name<T> {}
impl<T> HashTransparent for Name<T> {}
impl<T> DebugTransparent for Name<T> {}
impl<T> DisplayTransparent for Name<T> {}

impl<T> Validator for Name<T> {
    type Err = std::convert::Infallible;
}

impl<T> Default for StrongBuf<Name<T>> {
    fn default() -> Self { unsafe { Self::no_validate(String::new()) } }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{Strong as S, *};

    #[test]
    fn name() {
        #[derive(Debug, Clone, Copy, PartialEq)]
        struct UserId(i32);
        struct User<'a> {
            id: UserId,
            name: &'a S<Name<UserId>>
        }
        let name: &S<Name<UserId>> = TryFrom::try_from("Alice").unwrap();
        let u = User {
            id: UserId(3),
            name
        };
        assert_eq!(u.id, UserId(3));
        assert_eq!(u.name.as_str(), "Alice");
    }

    #[test]
    fn email() {
        assert!(StrongBuf::<Email>::validate("a".to_string()).is_err());
        let _: StrongBuf<Email> = TryFrom::try_from("a@example.com".to_string()).unwrap();
    }
}