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
83
84
85
use crate::{Strong, Validator};

pub use imp::StrongBuf;

mod imp {
    #[cfg(feature = "diesel")]
    use crate::impl_diesel::SqlStrong;
    use crate::{Strong, Validator};
    use std::{marker::PhantomData, ops::Deref};

    /// Strongly typed [`String`]
    #[cfg_attr(
        feature = "diesel",
        derive(FromSqlRow, AsExpression),
        sql_type = "SqlStrong<Ctx>"
    )]
    pub struct StrongBuf<Ctx: Validator> {
        phantom: PhantomData<Ctx>,
        inner: String
    }

    impl<Ctx: Validator> StrongBuf<Ctx> {
        /// Constructs from [`String`].
        #[inline]
        pub fn validate(s: String) -> Result<Self, Ctx::Err> {
            Ctx::validate(&s)?;
            Ok(unsafe { Self::no_validate(s) })
        }

        /// Constructs from [`String`] without validation.
        /// ## Safety
        /// This function allows us to create invalid [`StrongBuf`].
        #[inline]
        pub unsafe fn no_validate(s: String) -> Self {
            Self {
                inner: s,
                phantom: PhantomData
            }
        }

        /// Converts to [`String`].
        #[inline]
        pub fn into_string(self) -> String { self.inner }

        #[inline]
        pub fn as_strong(&self) -> &Strong<Ctx> { self }
    }

    impl<Ctx> Deref for StrongBuf<Ctx>
    where
        Ctx: Validator
    {
        type Target = Strong<Ctx>;
        #[inline]
        fn deref(&self) -> &Strong<Ctx> { unsafe { Strong::no_validate(&self.inner) } }
    }
}

impl<Ctx: Validator> StrongBuf<Ctx> {
    /// Constructs from [`Vec<u8>`] without validation.
    /// ## Safety
    /// This function allows us to create invalid [`StrongBuf`].
    #[inline]
    pub unsafe fn from_utf8_unchecked(bytes: Vec<u8>) -> Self {
        Self::no_validate(String::from_utf8_unchecked(bytes))
    }

    // TODO: Should I implement String methods?
}

impl<Ctx> std::borrow::Borrow<Strong<Ctx>> for StrongBuf<Ctx>
where
    Ctx: Validator
{
    #[inline]
    fn borrow(&self) -> &Strong<Ctx> { self }
}

impl<Ctx> Clone for StrongBuf<Ctx>
where
    Ctx: Validator
{
    #[inline]
    fn clone(&self) -> Self { unsafe { Self::from_utf8_unchecked(self.as_bytes().to_owned()) } }
}