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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
use {Ident, Lifetime, LifetimeDef, Ty, TyParamBound, WhereBoundPredicate, WherePredicate,
     WhereRegionPredicate};
use aster::invoke::{Invoke, Identity};
use aster::lifetime::{IntoLifetime, IntoLifetimeDef, LifetimeDefBuilder};
use aster::path::IntoPath;
use aster::ty::TyBuilder;
use aster::ty_param::{TyParamBoundBuilder, PolyTraitRefBuilder, TraitTyParamBoundBuilder};

// ////////////////////////////////////////////////////////////////////////////

pub struct WherePredicateBuilder<F = Identity> {
    callback: F,
}

impl WherePredicateBuilder {
    pub fn new() -> Self {
        WherePredicateBuilder::with_callback(Identity)
    }
}

impl<F> WherePredicateBuilder<F>
    where F: Invoke<WherePredicate>
{
    pub fn with_callback(callback: F) -> Self {
        WherePredicateBuilder { callback: callback }
    }

    pub fn bound(self) -> TyBuilder<Self> {
        TyBuilder::with_callback(self)
    }

    pub fn lifetime<L>(self, lifetime: L) -> WhereRegionPredicateBuilder<F>
        where L: IntoLifetime
    {
        WhereRegionPredicateBuilder {
            callback: self.callback,
            lifetime: lifetime.into_lifetime(),
            bounds: Vec::new(),
        }
    }
}

impl<F> Invoke<Ty> for WherePredicateBuilder<F>
    where F: Invoke<WherePredicate>
{
    type Result = WhereBoundPredicateTyBuilder<F>;

    fn invoke(self, ty: Ty) -> Self::Result {
        WhereBoundPredicateTyBuilder {
            callback: self.callback,
            ty: ty,
            bound_lifetimes: Vec::new(),
        }
    }
}

// ////////////////////////////////////////////////////////////////////////////

pub struct WhereBoundPredicateBuilder<F> {
    callback: F,
}

impl<F> Invoke<Ty> for WhereBoundPredicateBuilder<F>
    where F: Invoke<WherePredicate>
{
    type Result = WhereBoundPredicateTyBuilder<F>;

    fn invoke(self, ty: Ty) -> Self::Result {
        WhereBoundPredicateTyBuilder {
            callback: self.callback,
            ty: ty,
            bound_lifetimes: Vec::new(),
        }
    }
}

// ////////////////////////////////////////////////////////////////////////////

pub struct WhereBoundPredicateTyBuilder<F> {
    callback: F,
    ty: Ty,
    bound_lifetimes: Vec<LifetimeDef>,
}

impl<F> WhereBoundPredicateTyBuilder<F>
    where F: Invoke<WherePredicate>
{
    pub fn with_for_lifetime<L>(mut self, lifetime: L) -> Self
        where L: IntoLifetimeDef
    {
        self.bound_lifetimes.push(lifetime.into_lifetime_def());
        self
    }

    pub fn for_lifetime<N>(self, name: N) -> LifetimeDefBuilder<Self>
        where N: Into<Ident>
    {
        LifetimeDefBuilder::with_callback(name, self)
    }

    pub fn with_bound(self, bound: TyParamBound) -> WhereBoundPredicateTyBoundsBuilder<F> {
        WhereBoundPredicateTyBoundsBuilder {
            callback: self.callback,
            ty: self.ty,
            bound_lifetimes: self.bound_lifetimes,
            bounds: vec![bound],
        }
    }

    pub fn bound(self) -> TyParamBoundBuilder<WhereBoundPredicateTyBoundsBuilder<F>> {
        let builder = WhereBoundPredicateTyBoundsBuilder {
            callback: self.callback,
            ty: self.ty,
            bound_lifetimes: self.bound_lifetimes,
            bounds: vec![],
        };
        TyParamBoundBuilder::with_callback(builder)
    }

    pub fn trait_<P>
        (self,
         path: P)
         -> PolyTraitRefBuilder<TraitTyParamBoundBuilder<WhereBoundPredicateTyBoundsBuilder<F>>>
        where P: IntoPath
    {
        self.bound().trait_(path)
    }

    pub fn lifetime<L>(self, lifetime: L) -> WhereBoundPredicateTyBoundsBuilder<F>
        where L: IntoLifetime
    {
        self.bound().lifetime(lifetime)
    }
}

impl<F> Invoke<LifetimeDef> for WhereBoundPredicateTyBuilder<F>
    where F: Invoke<WherePredicate>
{
    type Result = Self;

    fn invoke(self, lifetime: LifetimeDef) -> Self {
        self.with_for_lifetime(lifetime)
    }
}

impl<F> Invoke<TyParamBound> for WhereBoundPredicateTyBuilder<F>
    where F: Invoke<WherePredicate>
{
    type Result = WhereBoundPredicateTyBoundsBuilder<F>;

    fn invoke(self, bound: TyParamBound) -> Self::Result {
        self.with_bound(bound)
    }
}

// ////////////////////////////////////////////////////////////////////////////

pub struct WhereBoundPredicateTyBoundsBuilder<F> {
    callback: F,
    ty: Ty,
    bound_lifetimes: Vec<LifetimeDef>,
    bounds: Vec<TyParamBound>,
}

impl<F> WhereBoundPredicateTyBoundsBuilder<F>
    where F: Invoke<WherePredicate>
{
    pub fn with_for_lifetime<L>(mut self, lifetime: L) -> Self
        where L: IntoLifetimeDef
    {
        self.bound_lifetimes.push(lifetime.into_lifetime_def());
        self
    }

    pub fn for_lifetime<N>(self, name: N) -> LifetimeDefBuilder<Self>
        where N: Into<Ident>
    {
        LifetimeDefBuilder::with_callback(name, self)
    }

    pub fn with_bound(mut self, bound: TyParamBound) -> Self {
        self.bounds.push(bound);
        self
    }

    pub fn bound(self) -> TyParamBoundBuilder<Self> {
        TyParamBoundBuilder::with_callback(self)
    }

    pub fn trait_<P>(self, path: P) -> PolyTraitRefBuilder<TraitTyParamBoundBuilder<Self>>
        where P: IntoPath
    {
        self.bound().trait_(path)
    }

    pub fn lifetime<L>(self, lifetime: L) -> Self
        where L: IntoLifetime
    {
        self.bound().lifetime(lifetime)
    }

    pub fn build(self) -> F::Result {
        let predicate = WhereBoundPredicate {
            bound_lifetimes: self.bound_lifetimes,
            bounded_ty: self.ty,
            bounds: self.bounds,
        };

        self.callback.invoke(WherePredicate::BoundPredicate(predicate))
    }
}

impl<F> Invoke<LifetimeDef> for WhereBoundPredicateTyBoundsBuilder<F>
    where F: Invoke<WherePredicate>
{
    type Result = Self;

    fn invoke(self, lifetime: LifetimeDef) -> Self {
        self.with_for_lifetime(lifetime)
    }
}

impl<F> Invoke<TyParamBound> for WhereBoundPredicateTyBoundsBuilder<F>
    where F: Invoke<WherePredicate>
{
    type Result = Self;

    fn invoke(self, bound: TyParamBound) -> Self {
        self.with_bound(bound)
    }
}

// ////////////////////////////////////////////////////////////////////////////

pub struct WhereRegionPredicateBuilder<F> {
    callback: F,
    lifetime: Lifetime,
    bounds: Vec<Lifetime>,
}

impl<F> WhereRegionPredicateBuilder<F>
    where F: Invoke<WherePredicate>
{
    pub fn bound<L>(mut self, lifetime: L) -> Self
        where L: IntoLifetime
    {
        self.bounds.push(lifetime.into_lifetime());
        self
    }

    pub fn build(self) -> F::Result {
        let predicate = WhereRegionPredicate {
            lifetime: self.lifetime,
            bounds: self.bounds,
        };

        self.callback.invoke(WherePredicate::RegionPredicate(predicate))
    }
}