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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
use std::marker::PhantomData;
use crate::attribute::BaseAttribute;
use crate::error::ConstraintError;
use crate::has::{HasEntityWithType, ShedNext};
use crate::private::PrivateConstraintT;
use crate::store::EntityProof;

pub type EntityTag = &'static str;
pub type ConstraintResult<T> = Result<T, ConstraintError>;
pub trait ConstraintEntity = Send + Sync;


pub struct ConstraintChain<const STAG: EntityTag, const RTAG: EntityTag, Attr, Next> {
    next: Next,
    attr_phantom: PhantomData<Attr>
}

impl<const STAG: EntityTag, const RTAG: EntityTag, Attr, Next> ConstraintChain<STAG, RTAG, Attr, Next>
    where
        Attr: BaseAttribute,
        Next: ConstraintT
{
    pub(crate) fn new(next: Next) -> Self {
        Self {
            next,
            attr_phantom: PhantomData
        }
    }
}

pub trait ConstraintT: PrivateConstraintT + Sized {
    fn add_entity<T, const ETAG: EntityTag>(self, entity: T) -> ConstraintResult<EntityProof<ETAG, T, Self>>
        where
            T: ConstraintEntity + 'static;

    fn get_entity<T, const TAG: EntityTag>(&self) -> &T
        where
            T: ConstraintEntity + 'static,
            Self: HasEntityWithType<TAG, T>;

    fn get_entity_mut<T, const TAG: EntityTag>(&mut self) -> &mut T
        where
            T: ConstraintEntity + 'static,
            Self: HasEntityWithType<TAG, T>;

    fn try_get_entity<T, const TAG: EntityTag>(&self) -> ConstraintResult<&T>
        where
            T: ConstraintEntity + 'static;

    fn try_get_entity_mut<T, const TAG: EntityTag>(&mut self) -> ConstraintResult<&mut T>
        where
            T: ConstraintEntity + 'static;
}

impl<const STAG: EntityTag, const RTAG: EntityTag, Attr, Next> PrivateConstraintT for ConstraintChain<STAG, RTAG, Attr, Next>
    where
        Attr: BaseAttribute,
        Next: ConstraintT,
{
    fn _private_add_entity<T, const ETAG: EntityTag>(&mut self, entity: T) -> ConstraintResult<()>
        where
            T: ConstraintEntity + 'static
    {
        self.next._private_add_entity::<T, ETAG>(entity)
    }

    fn _private_get_entity_ref<T, const ETAG: EntityTag>(&self) -> &T
        where
            T: ConstraintEntity + 'static
    {
        self.next._private_get_entity_ref::<T, ETAG>()
    }

    fn _private_get_entity_mut<T, const ETAG: EntityTag>(&mut self) -> &mut T
        where
            T: ConstraintEntity + 'static
    {
        self.next._private_get_entity_mut::<T, ETAG>()
    }

    fn _private_get_entity<T, const ETAG: EntityTag>(&mut self) -> T
        where
            T: ConstraintEntity + 'static
    {
        self.next._private_get_entity::<T, ETAG>()
    }

    fn _private_try_get_entity_ref<T, const ETAG: EntityTag>(&self) -> ConstraintResult<&T>
        where
            T: ConstraintEntity + 'static
    {
        self.next._private_try_get_entity_ref::<T, ETAG>()
    }

    fn _private_try_get_entity_mut<T, const ETAG: EntityTag>(&mut self) -> ConstraintResult<&mut T>
        where
            T: ConstraintEntity + 'static
    {
        self.next._private_try_get_entity_mut::<T, ETAG>()
    }
}

impl<
    const STAG: EntityTag,
    const RTAG: EntityTag,
    Attr,
    Next
> ConstraintT for ConstraintChain<STAG, RTAG, Attr, Next>
    where
        Attr: BaseAttribute,
        Next: ConstraintT,
{
    fn add_entity<T, const ETAG: EntityTag>(mut self, entity: T) -> ConstraintResult<EntityProof<ETAG, T, Self>>
        where
            T: ConstraintEntity + 'static,
    {
        self._private_add_entity::<T, ETAG>(entity)
            .map(|_| EntityProof::<_, _, _>::new(self))
    }

    fn get_entity<T, const TAG: EntityTag>(&self) -> &T
        where
            T: ConstraintEntity + 'static,
            Self: HasEntityWithType<TAG, T>
    {
        self._private_get_entity_ref::<T, TAG>()
    }

    fn get_entity_mut<T, const TAG: EntityTag>(&mut self) -> &mut T
        where
            T: ConstraintEntity + 'static,
            Self: HasEntityWithType<TAG, T>
    {
        self._private_get_entity_mut::<T, TAG>()
    }

    fn try_get_entity<T, const TAG: EntityTag>(&self) -> ConstraintResult<&T>
        where
            T: ConstraintEntity + 'static
    {
        self._private_try_get_entity_ref::<T, TAG>()
    }

    fn try_get_entity_mut<T, const TAG: EntityTag>(&mut self) -> ConstraintResult<&mut T>
        where
            T: ConstraintEntity + 'static
    {
        self._private_try_get_entity_mut::<T, TAG>()
    }
}

impl<
    Next,
    ChainNext,
    EntityType,
    Attr,
    const STAG: EntityTag,
    const RTAG: EntityTag,
    const ETAG: EntityTag
> ShedNext<ETAG, EntityType, Next> for ConstraintChain<STAG, RTAG, Attr, ChainNext>
    where
        Attr: BaseAttribute,
        ChainNext: ShedNext<ETAG, EntityType, Next> + ConstraintT,
        EntityType: ConstraintEntity + 'static,
{
    fn shed(self) -> (EntityType, Next) {
        self.next.shed()
    }
}