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
use syntax::ast;
use syntax::codemap::{DUMMY_SP, Span};
use syntax::ptr::P;
use invoke::{Invoke, Identity};
use ident::ToIdent;
use path::{PathBuilder, PathSegmentBuilder};
use ty::TyBuilder;
pub struct QPathBuilder<F=Identity> {
callback: F,
span: Span,
}
impl QPathBuilder {
pub fn new() -> Self {
QPathBuilder::new_with_callback(Identity)
}
}
impl<F> QPathBuilder<F>
where F: Invoke<(ast::QSelf, ast::Path)>,
{
pub fn new_with_callback(callback: F) -> Self {
QPathBuilder {
callback: callback,
span: DUMMY_SP,
}
}
pub fn span(mut self, span: Span) -> Self {
self.span = span;
self
}
pub fn ty(self) -> TyBuilder<Self> {
TyBuilder::new_with_callback(self)
}
pub fn build(self, qself: ast::QSelf, path: ast::Path) -> F::Result {
self.callback.invoke((qself, path))
}
}
impl<F> Invoke<P<ast::Ty>> for QPathBuilder<F>
where F: Invoke<(ast::QSelf, ast::Path)>,
{
type Result = QPathTyBuilder<F>;
fn invoke(self, ty: P<ast::Ty>) -> QPathTyBuilder<F> {
QPathTyBuilder {
builder: self,
ty: ty,
}
}
}
pub struct QPathTyBuilder<F> {
builder: QPathBuilder<F>,
ty: P<ast::Ty>,
}
impl<F> QPathTyBuilder<F>
where F: Invoke<(ast::QSelf, ast::Path)>,
{
pub fn as_(self) -> PathBuilder<Self> {
PathBuilder::new_with_callback(self)
}
pub fn id<T>(self, id: T) -> F::Result
where T: ToIdent,
{
let path = ast::Path {
span: self.builder.span,
global: false,
segments: vec![],
};
self.as_().build(path).id(id)
}
pub fn segment<T>(self, id: T) -> PathSegmentBuilder<QPathQSelfBuilder<F>>
where T: ToIdent,
{
let path = ast::Path {
span: self.builder.span,
global: false,
segments: vec![],
};
self.as_().build(path).segment(id)
}
}
impl<F> Invoke<ast::Path> for QPathTyBuilder<F>
where F: Invoke<(ast::QSelf, ast::Path)>,
{
type Result = QPathQSelfBuilder<F>;
fn invoke(self, path: ast::Path) -> QPathQSelfBuilder<F> {
QPathQSelfBuilder {
builder: self.builder,
qself: ast::QSelf {
ty: self.ty,
position: path.segments.len(),
},
path: path,
}
}
}
pub struct QPathQSelfBuilder<F> {
builder: QPathBuilder<F>,
qself: ast::QSelf,
path: ast::Path,
}
impl<F> QPathQSelfBuilder<F>
where F: Invoke<(ast::QSelf, ast::Path)>,
{
pub fn id<T>(self, id: T) -> F::Result
where T: ToIdent,
{
self.segment(id).build()
}
pub fn segment<T>(self, id: T) -> PathSegmentBuilder<QPathQSelfBuilder<F>>
where T: ToIdent,
{
PathSegmentBuilder::new_with_callback(id, self)
}
}
impl<F> Invoke<ast::PathSegment> for QPathQSelfBuilder<F>
where F: Invoke<(ast::QSelf, ast::Path)>,
{
type Result = F::Result;
fn invoke(mut self, segment: ast::PathSegment) -> F::Result {
self.path.segments.push(segment);
self.builder.build(self.qself, self.path)
}
}