@@ -10,7 +10,7 @@ extern crate syntax;
10
10
11
11
use std:: char;
12
12
13
- #[ derive( Debug , PartialEq , Eq ) ]
13
+ #[ derive( Debug , PartialEq , Eq , Clone ) ]
14
14
pub enum Ty {
15
15
String ,
16
16
Void ,
@@ -24,6 +24,7 @@ pub enum Ty {
24
24
List ( Box < Ty > ) ,
25
25
Set ( Box < Ty > ) ,
26
26
Map ( Box < Ty > , Box < Ty > ) ,
27
+ Option ( Box < Ty > ) ,
27
28
// User-defined type.
28
29
Ident ( String )
29
30
}
@@ -57,6 +58,10 @@ impl Ty {
57
58
& Ty :: I32 => quote_ty ! ( cx, i32 ) ,
58
59
& Ty :: I64 => quote_ty ! ( cx, i64 ) ,
59
60
& Ty :: Double => quote_ty ! ( cx, f64 ) ,
61
+ & Ty :: Option ( ref t) => {
62
+ let inner = t. to_ast ( cx) ;
63
+ quote_ty ! ( cx, Option <$inner>)
64
+ } ,
60
65
& Ty :: List ( ref s) => {
61
66
let inner = s. to_ast ( cx) ;
62
67
quote_ty ! ( cx, Vec <$inner>)
@@ -114,20 +119,13 @@ impl Ast for Service {
114
119
let mut inputs = vec ! [
115
120
ast:: Arg :: new_self( span, ast:: Mutability :: Immutable , self_ident. clone( ) )
116
121
] ;
117
- let ty = cx. ty_ident ( span, token:: str_to_ident ( match & * method. ty {
118
- "void" => "()" ,
119
- "string" => "String" ,
120
- ty => ty
121
- } ) ) ;
122
+ let ty = method. ty . to_ast ( cx) ;
122
123
123
124
for arg in method. args . iter ( ) {
124
125
let arg_ident = token:: str_to_ident ( & arg. ident ) ;
126
+ let arg_ty = arg. ty . to_ast ( cx) ;
125
127
inputs. push (
126
- cx. arg ( span, arg_ident, cx. ty_ident ( span, token:: str_to_ident ( match & * method. ty {
127
- "void" => "()" ,
128
- "string" => "String" ,
129
- ty => ty
130
- } ) ) )
128
+ cx. arg ( span, arg_ident, arg_ty)
131
129
) ;
132
130
}
133
131
@@ -179,7 +177,7 @@ impl Ast for Service {
179
177
#[ derive( Debug , PartialEq , Eq ) ]
180
178
pub struct ServiceMethod {
181
179
ident : String ,
182
- ty : String ,
180
+ ty : Ty ,
183
181
attr : FieldAttribute ,
184
182
args : Vec < StructField >
185
183
}
@@ -257,20 +255,20 @@ impl Ast for Struct {
257
255
258
256
for node in self . fields . iter ( ) {
259
257
let span = cx. call_site ( ) ;
260
- let mut ty = map_ty ( & node. ty ) ;
258
+ let mut ty = node. ty . clone ( ) ;
261
259
262
260
match node. attr {
263
261
FieldAttribute :: Required => { } ,
264
262
// XXX: We need to map the inner `node.ty` to a proper Rust type.
265
- FieldAttribute :: Optional => ty = map_ty ( & format ! ( "Option<{}>" , node . ty) ) ,
263
+ FieldAttribute :: Optional => ty = Ty :: Option ( Box :: new ( ty) ) ,
266
264
_ => panic ! ( "Oneway is not supported for struct fields." )
267
265
}
268
266
269
267
let field = ast:: StructField {
270
268
node : ast:: StructField_ {
271
269
kind : ast:: StructFieldKind :: NamedField ( token:: str_to_ident ( & node. ident ) , ast:: Visibility :: Public ) ,
272
270
id : ast:: DUMMY_NODE_ID ,
273
- ty : cx . ty_ident ( span , ty ) ,
271
+ ty : ty . to_ast ( cx ) ,
274
272
attrs : Vec :: new ( )
275
273
} ,
276
274
span : span
@@ -322,7 +320,7 @@ pub enum FieldAttribute {
322
320
pub struct StructField {
323
321
seq : i16 ,
324
322
attr : FieldAttribute ,
325
- ty : String ,
323
+ ty : Ty ,
326
324
ident : String
327
325
}
328
326
@@ -472,7 +470,7 @@ impl<'a> Parser<'a> {
472
470
return Err ( Error :: MissingFieldAttribute ) ;
473
471
} ;
474
472
475
- let ty = self . parse_ident ( ) ?;
473
+ let ty = self . parse_ty ( ) ?;
476
474
let ident = self . parse_ident ( ) ?;
477
475
478
476
Ok ( StructField {
@@ -576,7 +574,7 @@ impl<'a> Parser<'a> {
576
574
FieldAttribute :: Required
577
575
} ;
578
576
579
- let method_ty = self . parse_ident ( ) ?;
577
+ let method_ty = self . parse_ty ( ) ?;
580
578
let method_ident = self . parse_ident ( ) ?;
581
579
let mut method_fields = Vec :: new ( ) ;
582
580
@@ -589,7 +587,7 @@ impl<'a> Parser<'a> {
589
587
590
588
let seq = self . parse_number ( ) ?;
591
589
self . expect ( & Token :: Colon ) ?;
592
- let field_ty = self . parse_ident ( ) ?;
590
+ let field_ty = self . parse_ty ( ) ?;
593
591
let field_ident = self . parse_ident ( ) ?;
594
592
595
593
method_fields. push ( StructField {
@@ -1186,7 +1184,7 @@ mod tests {
1186
1184
assert_eq ! ( & * def. ident, "Flock" ) ;
1187
1185
assert_eq ! ( def. methods. len( ) , 1 ) ;
1188
1186
assert_eq ! ( & * def. methods[ 0 ] . ident, "ping" ) ;
1189
- assert_eq ! ( & * def. methods[ 0 ] . ty, "void" ) ;
1187
+ assert_eq ! ( def. methods[ 0 ] . ty, Ty :: Void ) ;
1190
1188
assert_eq ! ( def. methods[ 0 ] . attr, FieldAttribute :: Required ) ;
1191
1189
assert_eq ! ( def. methods[ 0 ] . args. len( ) , 0 ) ;
1192
1190
}
@@ -1200,13 +1198,13 @@ mod tests {
1200
1198
assert_eq ! ( & * def. ident, "Beans" ) ;
1201
1199
assert_eq ! ( def. methods. len( ) , 1 ) ;
1202
1200
assert_eq ! ( & * def. methods[ 0 ] . ident, "poutine" ) ;
1203
- assert_eq ! ( & * def. methods[ 0 ] . ty, "void" ) ;
1201
+ assert_eq ! ( def. methods[ 0 ] . ty, Ty :: Void ) ;
1204
1202
assert_eq ! ( def. methods[ 0 ] . attr, FieldAttribute :: Required ) ;
1205
1203
assert_eq ! ( def. methods[ 0 ] . args. len( ) , 1 ) ;
1206
1204
assert_eq ! ( def. methods[ 0 ] . args[ 0 ] , StructField {
1207
1205
seq: 1 ,
1208
1206
attr: FieldAttribute :: Required ,
1209
- ty: "string" . to_string ( ) ,
1207
+ ty: Ty :: String ,
1210
1208
ident: "firstName" . to_string( )
1211
1209
} ) ;
1212
1210
}
@@ -1220,7 +1218,7 @@ mod tests {
1220
1218
assert_eq ! ( & * def. ident, "Flock" ) ;
1221
1219
assert_eq ! ( def. methods. len( ) , 1 ) ;
1222
1220
assert_eq ! ( & * def. methods[ 0 ] . ident, "ping" ) ;
1223
- assert_eq ! ( & * def. methods[ 0 ] . ty, "void" ) ;
1221
+ assert ! ( def. methods[ 0 ] . ty == Ty :: Void ) ;
1224
1222
assert_eq ! ( def. methods[ 0 ] . attr, FieldAttribute :: Oneway ) ;
1225
1223
assert_eq ! ( def. methods[ 0 ] . args. len( ) , 0 ) ;
1226
1224
}
@@ -1264,7 +1262,7 @@ mod tests {
1264
1262
let mut p = Parser :: new ( "1: optional i32 foobar" ) ;
1265
1263
let def = p. parse_struct_field ( ) . unwrap ( ) ;
1266
1264
assert_eq ! ( & * def. ident, "foobar" ) ;
1267
- assert_eq ! ( & * def. ty, "i32" ) ;
1265
+ assert_eq ! ( def. ty, Ty :: I32 ) ;
1268
1266
assert_eq ! ( def. seq, 1 ) ;
1269
1267
assert_eq ! ( def. attr, FieldAttribute :: Optional ) ;
1270
1268
}
@@ -1274,7 +1272,7 @@ mod tests {
1274
1272
let mut p = Parser :: new ( "1: required i32 foobar" ) ;
1275
1273
let def = p. parse_struct_field ( ) . unwrap ( ) ;
1276
1274
assert_eq ! ( & * def. ident, "foobar" ) ;
1277
- assert_eq ! ( & * def. ty, "i32" ) ;
1275
+ assert_eq ! ( def. ty, Ty :: I32 ) ;
1278
1276
assert_eq ! ( def. seq, 1 ) ;
1279
1277
assert_eq ! ( def. attr, FieldAttribute :: Required ) ;
1280
1278
}
0 commit comments