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
use syntax::ast;
use syntax::codemap::{DUMMY_SP, Span, respan};
use syntax::ptr::P;

use ident::ToIdent;
use invoke::{Invoke, Identity};
use ty::TyBuilder;

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

pub struct FnDeclBuilder<F=Identity> {
    callback: F,
    span: Span,
    args: Vec<ast::Arg>,
    variadic: bool,
}

impl FnDeclBuilder {
    pub fn new() -> FnDeclBuilder {
        FnDeclBuilder::new_with_callback(Identity)
    }
}

impl<F> FnDeclBuilder<F>
    where F: Invoke<P<ast::FnDecl>>,
{
    pub fn new_with_callback(callback: F) -> Self {
        FnDeclBuilder {
            callback: callback,
            span: DUMMY_SP,
            args: Vec::new(),
            variadic: false,
        }
    }

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

    pub fn variadic(mut self) -> Self {
        self.variadic = true;
        self
    }

    pub fn with_arg(mut self, arg: ast::Arg) -> Self {
        self.args.push(arg);
        self
    }

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

    pub fn arg<I>(self, id: I) -> ArgBuilder<Self>
        where I: ToIdent,
    {
        ArgBuilder::new_with_callback(id, self)
    }

    pub fn no_return(self) -> F::Result {
        let ret_ty = ast::FunctionRetTy::NoReturn(self.span);
        self.build(ret_ty)
    }

    pub fn default_return(self) -> F::Result {
        let ret_ty = ast::FunctionRetTy::DefaultReturn(self.span);
        self.build(ret_ty)
    }

    pub fn build_return(self, ty: P<ast::Ty>) -> F::Result {
        self.build(ast::FunctionRetTy::Return(ty))
    }

    pub fn return_(self) -> TyBuilder<Self> {
        TyBuilder::new_with_callback(self)
    }

    pub fn build(self, output: ast::FunctionRetTy) -> F::Result {
        self.callback.invoke(P(ast::FnDecl {
            inputs: self.args,
            output: output,
            variadic: self.variadic,
        }))
    }
}

impl<F> Invoke<ast::Arg> for FnDeclBuilder<F>
    where F: Invoke<P<ast::FnDecl>>
{
    type Result = Self;

    fn invoke(self, arg: ast::Arg) -> Self {
        self.with_arg(arg)
    }
}

impl<F> Invoke<P<ast::Ty>> for FnDeclBuilder<F>
    where F: Invoke<P<ast::FnDecl>>,
{
    type Result = F::Result;

    fn invoke(self, ty: P<ast::Ty>) -> F::Result {
        self.build_return(ty)
    }
}

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

pub struct ArgBuilder<F=Identity> {
    callback: F,
    span: Span,
    id: ast::Ident,
}

impl ArgBuilder {
    pub fn new<I>(id: I) -> Self where I: ToIdent {
        ArgBuilder::new_with_callback(id, Identity)
    }
}

impl<F> ArgBuilder<F>
    where F: Invoke<ast::Arg>,
{
    pub fn new_with_callback<I>(id: I, callback: F) -> ArgBuilder<F>
        where I: ToIdent,
    {
        ArgBuilder {
            callback: callback,
            span: DUMMY_SP,
            id: id.to_ident(),
        }
    }

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

    pub fn build_ty(self, ty: P<ast::Ty>) -> F::Result {
        let path = respan(self.span, self.id);

        self.callback.invoke(ast::Arg {
            id: ast::DUMMY_NODE_ID,
            ty: ty,
            pat: P(ast::Pat {
                id: ast::DUMMY_NODE_ID,
                node: ast::PatIdent(
                    ast::BindByValue(ast::Mutability::MutImmutable),
                    path,
                    None,
                ),
                span: self.span,
            }),
        })
    }

    pub fn ty(self) -> TyBuilder<ArgTyBuilder<F>> {
        TyBuilder::new_with_callback(ArgTyBuilder(self))
    }
}

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

pub struct ArgTyBuilder<F>(ArgBuilder<F>);

impl<F: Invoke<ast::Arg>> Invoke<P<ast::Ty>> for ArgTyBuilder<F>
{
    type Result = F::Result;

    fn invoke(self, ty: P<ast::Ty>) -> F::Result {
        self.0.build_ty(ty)
    }
}