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
#![cfg_attr(feature = "unstable", allow(should_implement_trait))]

use std::iter::IntoIterator;

use syntax::ast;
use syntax::codemap::{DUMMY_SP, Span};
use syntax::ptr::P;

use attr::AttrBuilder;
use expr::ExprBuilder;
use invoke::{Invoke, Identity};
use pat::PatBuilder;

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

pub struct ArmBuilder<F=Identity> {
    callback: F,
    span: Span,
    attrs: Vec<ast::Attribute>,
}

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

impl<F> ArmBuilder<F>
    where F: Invoke<ast::Arm>,
{
    pub fn with_callback(callback: F) -> Self {
        ArmBuilder {
            callback: callback,
            span: DUMMY_SP,
            attrs: Vec::new(),
        }
    }

    pub fn span(mut self, span: Span) -> Self {
        self.span = span;
        self
    }

    pub fn with_attrs<I>(mut self, iter: I) -> Self
        where I: IntoIterator<Item=ast::Attribute>,
    {
        self.attrs.extend(iter);
        self
    }

    pub fn with_attr(mut self, attr: ast::Attribute) -> Self {
        self.attrs.push(attr);
        self
    }

    pub fn attr(self) -> AttrBuilder<Self> {
        AttrBuilder::with_callback(self)
    }

    pub fn with_pats<I>(self, iter: I) -> ArmPatBuilder<F>
        where I: IntoIterator<Item=P<ast::Pat>>,
    {
        ArmPatBuilder {
            callback: self.callback,
            span: self.span,
            attrs: self.attrs,
            pats: iter.into_iter().collect(),
        }
    }

    pub fn with_pat(self, pat: P<ast::Pat>) -> ArmPatBuilder<F> {
        ArmPatBuilder {
            callback: self.callback,
            span: self.span,
            attrs: self.attrs,
            pats: vec![pat],
        }
    }

    pub fn pat(self) -> PatBuilder<Self> {
        PatBuilder::with_callback(self)
    }

    /*
    pub fn with_guard(self, guard: Option<P<ast::Expr>>) -> ExprBuilder<ArmBodyBuilder<F>> {
        ExprBuilder::with_callback(ArmBodyBuilder {
            builder: self,
            guard: guard,
        })
    }

    pub fn guard(self) -> ExprBuilder<Self> {
        ExprBuilder::with_callback(self)
    }

    pub fn body(self) -> ExprBuilder<ArmBodyBuilder<F>> {
        self.with_guard(None)
    }

    pub fn build_arm_(self,
                      guard: Option<P<ast::Expr>>,
                      body: P<ast::Expr>) -> F::Result {
        self.callback.invoke(ast::Arm {
            attrs: self.attrs,
            pats: self.pats,
            guard: guard,
            body: body,
        })
    }
    */
}

impl<F> Invoke<ast::Attribute> for ArmBuilder<F>
    where F: Invoke<ast::Arm>,
{
    type Result = Self;

    fn invoke(self, attr: ast::Attribute) -> Self {
        self.with_attr(attr)
    }
}

impl<F> Invoke<P<ast::Pat>> for ArmBuilder<F>
    where F: Invoke<ast::Arm>,
{
    type Result = ArmPatBuilder<F>;

    fn invoke(self, pat: P<ast::Pat>) -> ArmPatBuilder<F> {
        self.with_pat(pat)
    }
}

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

pub struct ArmPatBuilder<F> {
    callback: F,
    span: Span,
    attrs: Vec<ast::Attribute>,
    pats: Vec<P<ast::Pat>>,
}

impl<F> ArmPatBuilder<F>
    where F: Invoke<ast::Arm>,
{
    pub fn with_pats<I>(mut self, iter: I) -> Self
        where I: IntoIterator<Item=P<ast::Pat>>,
    {
        self.pats.extend(iter);
        self
    }

    pub fn with_pat(mut self, pat: P<ast::Pat>) -> Self {
        self.pats.push(pat);
        self
    }

    pub fn pat(self) -> PatBuilder<Self> {
        let span = self.span;
        PatBuilder::with_callback(self).span(span)
    }

    pub fn with_guard(self, guard: Option<P<ast::Expr>>) -> ArmBodyBuilder<F> {
        ArmBodyBuilder {
            builder: self,
            guard: guard,
        }
    }

    pub fn guard(self) -> ExprBuilder<Self> {
        let span = self.span;
        ExprBuilder::with_callback(self).span(span)
    }

    pub fn body(self) -> ExprBuilder<ArmBodyBuilder<F>> {
        ArmBodyBuilder {
            builder: self,
            guard: None,
        }.body()
    }

    pub fn build_arm_(self,
                      guard: Option<P<ast::Expr>>,
                      body: P<ast::Expr>) -> F::Result {
        self.callback.invoke(ast::Arm {
            attrs: self.attrs,
            pats: self.pats,
            guard: guard,
            body: body,
        })
    }
}

impl<F> Invoke<P<ast::Pat>> for ArmPatBuilder<F>
    where F: Invoke<ast::Arm>,
{
    type Result = Self;

    fn invoke(self, pat: P<ast::Pat>) -> Self {
        self.with_pat(pat)
    }
}

impl<F> Invoke<P<ast::Expr>> for ArmPatBuilder<F>
    where F: Invoke<ast::Arm>,
{
    type Result = ArmBodyBuilder<F>;

    fn invoke(self, guard: P<ast::Expr>) -> ArmBodyBuilder<F> {
        self.with_guard(Some(guard))
    }
}

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

pub struct ArmBodyBuilder<F> {
    builder: ArmPatBuilder<F>,
    guard: Option<P<ast::Expr>>,
}

impl<F> ArmBodyBuilder<F>
    where F: Invoke<ast::Arm>,
{
    pub fn body(self) -> ExprBuilder<ArmBodyBuilder<F>> {
        ExprBuilder::with_callback(self)
    }

    pub fn build(self, body: P<ast::Expr>) -> F::Result {
        self.builder.build_arm_(self.guard, body)
    }
}

impl<F> Invoke<P<ast::Expr>> for ArmBodyBuilder<F>
    where F: Invoke<ast::Arm>,
{
    type Result = F::Result;

    fn invoke(self, body: P<ast::Expr>) -> F::Result {
        self.build(body)
    }
}