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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use better_any::TidAble;

use crate::{
    add_generics::WithGenerics, assertable::Assertable, generatable::Generatable,
    into_template::TypeEq, maybe_borrowed::MaybeBorrowed, raw_assert::r#trait::RawAssertable,
    raw_assert::RawAssert,
};

/// One of the Assert types provided by the crate, at
/// this point in time also the only one.
///
/// This is backed by some template that the the
/// type is checked against.
///
/// Every template must implement [`Generatable`]
/// so that it can be turned into tokens that actually
/// check if the type matches the template.
///
/// Further information about the structure of this crate
/// can be found in the module documentation.
pub struct Assert<'a, T, A>
where
    T: Generatable<'a, A>,
    A: 'a + TidAble<'a>,
{
    pub template: MaybeBorrowed<'a, T>,
    pub assert: MaybeBorrowed<'a, A>,
}

pub trait InsertIntoTemplate<'a, T, A>
where
    T: Generatable<'a, A> + 'a,
    A: 'a + TidAble<'a>,
{
    /// The type of the Assert generated by the Template
    /// `Self`
    type Output: RawAssertable<'a>;

    /// Test some type against the template `Self` and turn
    /// it into [`Self::Output`]. So that the type may
    /// either be borrowed or owned, any type that
    /// can be turned into `MaybeBorrowed<T::Assert>`
    fn test<B>(self, ty: impl Into<MaybeBorrowed<'a, A>>) -> Self::Output
    where
        B: TypeEq<This = A>;
}

impl<'a, T, A> InsertIntoTemplate<'a, T, A> for T
where
    T: Generatable<'a, A> + 'a + Ord + Eq,
    A: Ord + Eq + 'a + TidAble<'a>,
{
    type Output = Assert<'a, T, A>;

    fn test<B>(self, ty: impl Into<MaybeBorrowed<'a, A>>) -> Assert<'a, T, A>
    where
        B: TypeEq<This = A>,
    {
        Assert {
            template: MaybeBorrowed::Owned(self),
            assert: ty.into(),
        }
    }
}

impl<'a, T, A> InsertIntoTemplate<'a, T, A> for &'a T
where
    T: Generatable<'a, A> + 'a + Ord + Eq,
    A: Ord + Eq + 'a + TidAble<'a>,
{
    type Output = Assert<'a, T, A>;

    fn test<B>(self, ty: impl Into<MaybeBorrowed<'a, A>>) -> Assert<'a, T, A>
    where
        B: TypeEq<This = A>,
    {
        Assert {
            template: MaybeBorrowed::Borrowed(self),
            assert: ty.into(),
        }
    }
}

impl<'a, T, A> InsertIntoTemplate<'a, T, A> for WithGenerics<'a, T>
where
    T: Generatable<'a, A> + Eq + Ord,
    A: Eq + Ord + 'a + TidAble<'a>,
{
    type Output = RawAssert<'a, T, A>;

    fn test<B>(self, ty: impl Into<MaybeBorrowed<'a, A>>) -> RawAssert<'a, T, A>
    where
        B: TypeEq<This = A>,
    {
        RawAssert {
            template: self.data.into(),
            generics: self.generics,
            assert: ty.into(),
        }
    }
}

impl<'a, T, A> InsertIntoTemplate<'a, T, A> for WithGenerics<'a, MaybeBorrowed<'a, T>>
where
    T: Generatable<'a, A> + Eq + Ord,
    A: Eq + Ord + 'a + TidAble<'a>,
{
    type Output = WithGenerics<'a, Assert<'a, T, A>>;

    fn test<B>(self, ty: impl Into<MaybeBorrowed<'a, A>>) -> WithGenerics<'a, Assert<'a, T, A>>
    where
        B: TypeEq<This = A>,
    {
        WithGenerics {
            data: Assert {
                template: self.data,
                assert: ty.into(),
            },
            generics: self.generics,
        }
    }
}

impl<'a, T, A> Assertable<'a> for Assert<'a, T, A>
where
    T: Generatable<'a, A> + Eq + Ord,
    A: Eq + Ord + TidAble<'a>,
{
    type Output = RawAssert<'a, T, A>;

    fn do_assert(self) -> Self::Output {
        RawAssert {
            template: self.template,
            generics: None,
            assert: self.assert,
        }
    }
}