@@ -131,12 +131,18 @@ enum Fields {
131
131
132
132
/// Defines a struct field.
133
133
#[ derive( Debug , Clone ) ]
134
- struct Field {
134
+ pub struct Field {
135
135
/// Field name
136
136
name : String ,
137
137
138
138
/// Field type
139
139
ty : Type ,
140
+
141
+ /// Field documentation
142
+ documentation : Vec < String > ,
143
+
144
+ /// Field annotation
145
+ annotation : Vec < String > ,
140
146
}
141
147
142
148
/// Defines an associated type.
@@ -712,6 +718,16 @@ impl Struct {
712
718
self
713
719
}
714
720
721
+ /// Push a named field to the struct.
722
+ ///
723
+ /// A struct can either set named fields with this function or tuple fields
724
+ /// with `push_tuple_field`, but not both.
725
+ pub fn push_field ( & mut self , field : Field ) -> & mut Self
726
+ {
727
+ self . fields . push_named ( field) ;
728
+ self
729
+ }
730
+
715
731
/// Add a named field to the struct.
716
732
///
717
733
/// A struct can either set named fields with this function or tuple fields
@@ -1243,31 +1259,63 @@ impl AssociatedType {
1243
1259
}
1244
1260
}
1245
1261
1262
+ // ===== impl Field =====
1263
+
1264
+ impl Field {
1265
+ /// Return a field definition with the provided name and type
1266
+ pub fn new < T > ( name : & str , ty : T ) -> Self
1267
+ where T : Into < Type > ,
1268
+ {
1269
+ Field {
1270
+ name : name. into ( ) ,
1271
+ ty : ty. into ( ) ,
1272
+ documentation : Vec :: new ( ) ,
1273
+ annotation : Vec :: new ( ) ,
1274
+ }
1275
+ }
1276
+
1277
+ /// Set field's documentation.
1278
+ pub fn doc ( & mut self , documentation : Vec < & str > ) -> & mut Self {
1279
+ self . documentation = documentation. iter ( ) . map ( |doc| doc. to_string ( ) ) . collect ( ) ;
1280
+ self
1281
+ }
1282
+
1283
+ /// Set field's annotation.
1284
+ pub fn annotation ( & mut self , annotation : Vec < & str > ) -> & mut Self {
1285
+ self . annotation = annotation. iter ( ) . map ( |ann| ann. to_string ( ) ) . collect ( ) ;
1286
+ self
1287
+ }
1288
+ }
1289
+
1246
1290
// ===== impl Fields =====
1247
1291
1248
1292
impl Fields {
1249
- fn named < T > ( & mut self , name : & str , ty : T ) -> & mut Self
1250
- where T : Into < Type > ,
1293
+ fn push_named ( & mut self , field : Field ) -> & mut Self
1251
1294
{
1252
1295
match * self {
1253
1296
Fields :: Empty => {
1254
- * self = Fields :: Named ( vec ! [ Field {
1255
- name: name. to_string( ) ,
1256
- ty: ty. into( ) ,
1257
- } ] ) ;
1297
+ * self = Fields :: Named ( vec ! [ field] ) ;
1258
1298
}
1259
1299
Fields :: Named ( ref mut fields) => {
1260
- fields. push ( Field {
1261
- name : name. to_string ( ) ,
1262
- ty : ty. into ( ) ,
1263
- } ) ;
1300
+ fields. push ( field) ;
1264
1301
}
1265
1302
_ => panic ! ( "field list is named" ) ,
1266
1303
}
1267
1304
1268
1305
self
1269
1306
}
1270
1307
1308
+ fn named < T > ( & mut self , name : & str , ty : T ) -> & mut Self
1309
+ where T : Into < Type > ,
1310
+ {
1311
+ self . push_named ( Field {
1312
+ name : name. to_string ( ) ,
1313
+ ty : ty. into ( ) ,
1314
+ documentation : Vec :: new ( ) ,
1315
+ annotation : Vec :: new ( ) ,
1316
+ } )
1317
+ }
1318
+
1271
1319
fn tuple < T > ( & mut self , ty : T ) -> & mut Self
1272
1320
where T : Into < Type > ,
1273
1321
{
@@ -1291,6 +1339,16 @@ impl Fields {
1291
1339
1292
1340
fmt. block ( |fmt| {
1293
1341
for f in fields {
1342
+ if !f. documentation . is_empty ( ) {
1343
+ for doc in & f. documentation {
1344
+ write ! ( fmt, "/// {}\n " , doc) ?;
1345
+ }
1346
+ }
1347
+ if !f. annotation . is_empty ( ) {
1348
+ for ann in & f. annotation {
1349
+ write ! ( fmt, "{}\n " , ann) ?;
1350
+ }
1351
+ }
1294
1352
write ! ( fmt, "{}: " , f. name) ?;
1295
1353
f. ty . fmt ( fmt) ?;
1296
1354
write ! ( fmt, ",\n " ) ?;
@@ -1366,6 +1424,8 @@ impl Impl {
1366
1424
self . assoc_tys . push ( Field {
1367
1425
name : name. to_string ( ) ,
1368
1426
ty : ty. into ( ) ,
1427
+ documentation : Vec :: new ( ) ,
1428
+ annotation : Vec :: new ( ) ,
1369
1429
} ) ;
1370
1430
1371
1431
self
@@ -1517,6 +1577,11 @@ impl Function {
1517
1577
self . args . push ( Field {
1518
1578
name : name. to_string ( ) ,
1519
1579
ty : ty. into ( ) ,
1580
+ // While a `Field` is used here, both `documentation`
1581
+ // and `annotation` does not make sense for function arguments.
1582
+ // Simply use empty strings.
1583
+ documentation : Vec :: new ( ) ,
1584
+ annotation : Vec :: new ( ) ,
1520
1585
} ) ;
1521
1586
1522
1587
self
0 commit comments