diff --git a/Cargo.toml b/Cargo.toml index 5e884e79f..17508ee47 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "aster" -version = "0.2.1" +version = "0.2.2" authors = ["Erick Tryzelaar "] license = "MIT/Apache-2.0" description = "A libsyntax ast builder" diff --git a/src/item.rs b/src/item.rs index ee527229f..35453409a 100644 --- a/src/item.rs +++ b/src/item.rs @@ -362,7 +362,7 @@ impl ItemStructBuilder StructDefBuilder::new_with_callback(self).span(span).with_field(field) } - pub fn field(self, id: T) -> TyBuilder>> + pub fn field(self, id: T) -> StructFieldBuilder> where T: ToIdent, { let span = self.builder.span; diff --git a/src/struct_def.rs b/src/struct_def.rs index 689799e76..8ffb9d415 100644 --- a/src/struct_def.rs +++ b/src/struct_def.rs @@ -42,12 +42,11 @@ impl StructDefBuilder self } - pub fn field(self, id: T) -> TyBuilder> + pub fn field(self, id: T) -> StructFieldBuilder where T: ToIdent, { let span = self.span; - let builder = StructFieldBuilder::named_with_callback(id, self).span(span); - TyBuilder::new_with_callback(builder) + StructFieldBuilder::named_with_callback(id, self).span(span) } pub fn build(self) -> F::Result { diff --git a/src/variant.rs b/src/variant.rs index 088bb032a..a785d56c6 100644 --- a/src/variant.rs +++ b/src/variant.rs @@ -159,7 +159,7 @@ impl VariantStructBuilder StructDefBuilder::new_with_callback(self).span(span).with_field(field) } - pub fn field(self, id: T) -> TyBuilder>> + pub fn field(self, id: T) -> StructFieldBuilder> where T: ToIdent, { let span = self.builder.span; diff --git a/tests/test_item.rs b/tests/test_item.rs index e2fdf799a..7e0fedb6c 100644 --- a/tests/test_item.rs +++ b/tests/test_item.rs @@ -110,8 +110,8 @@ fn test_empty_struct() { fn test_struct() { let builder = AstBuilder::new(); let struct_ = builder.item().struct_("Struct") - .field("x").isize() - .field("y").isize() + .field("x").ty().isize() + .field("y").ty().isize() .build(); assert_eq!( @@ -122,8 +122,8 @@ fn test_struct() { id: ast::DUMMY_NODE_ID, node: ast::ItemStruct( builder.struct_def() - .field("x").isize() - .field("y").isize() + .field("x").ty().isize() + .field("y").ty().isize() .build(), builder.generics().build(), ), @@ -220,11 +220,11 @@ fn test_enum() { .ty().isize() .build() .struct_("E") - .field("a").isize() + .field("a").ty().isize() .build() .struct_("F") - .field("a").isize() - .field("b").isize() + .field("a").ty().isize() + .field("b").ty().isize() .build() .build(); @@ -247,11 +247,11 @@ fn test_enum() { .ty().isize() .build(), builder.variant("E").struct_() - .field("a").isize() + .field("a").ty().isize() .build(), builder.variant("F").struct_() - .field("a").isize() - .field("b").isize() + .field("a").ty().isize() + .field("b").ty().isize() .build(), ], }, diff --git a/tests/test_struct_def.rs b/tests/test_struct_def.rs index a090ae3b8..196a8d63c 100644 --- a/tests/test_struct_def.rs +++ b/tests/test_struct_def.rs @@ -4,7 +4,7 @@ extern crate aster; extern crate syntax; use syntax::ast; -use syntax::codemap::{DUMMY_SP, Spanned}; +use syntax::codemap::{DUMMY_SP, Spanned, respan}; use syntax::ptr::P; use aster::AstBuilder; @@ -27,8 +27,8 @@ fn test_empty() { fn test_fields() { let builder = AstBuilder::new(); let struct_def = builder.struct_def() - .field("x").isize() - .field("y").isize() + .field("x").ty().isize() + .field("y").ty().isize() .build(); assert_eq!( @@ -64,3 +64,63 @@ fn test_fields() { }) ); } + +#[test] +fn test_attrs() { + let builder = AstBuilder::new(); + let struct_def = builder.struct_def() + .field("x") + .attr().doc("/// doc string") + .attr().automatically_derived() + .ty().isize() + .build(); + + assert_eq!( + struct_def, + P(ast::StructDef { + fields: vec![ + Spanned { + span: DUMMY_SP, + node: ast::StructField_ { + kind: ast::NamedField( + builder.id("x"), + ast::Inherited, + ), + id: ast::DUMMY_NODE_ID, + ty: builder.ty().isize(), + attrs: vec![ + respan( + DUMMY_SP, + ast::Attribute_ { + id: ast::AttrId(0), + style: ast::AttrOuter, + value: P(respan( + DUMMY_SP, + ast::MetaNameValue( + builder.interned_string("doc"), + (*builder.lit().str("/// doc string")).clone(), + ), + )), + is_sugared_doc: true, + } + ), + respan( + DUMMY_SP, + ast::Attribute_ { + id: ast::AttrId(1), + style: ast::AttrOuter, + value: P(respan( + DUMMY_SP, + ast::MetaWord(builder.interned_string("automatically_derived")), + )), + is_sugared_doc: false, + } + ), + ], + }, + }, + ], + ctor_id: None, + }) + ); +} diff --git a/tests/test_variant.rs b/tests/test_variant.rs index d655cb63d..84a7e4ed2 100644 --- a/tests/test_variant.rs +++ b/tests/test_variant.rs @@ -67,8 +67,8 @@ fn test_tuple_variant() { fn test_struct_variant() { let builder = AstBuilder::new(); let variant = builder.variant("A").struct_() - .field("a").isize() - .field("b").isize() + .field("a").ty().isize() + .field("b").ty().isize() .build(); assert_eq!( @@ -80,8 +80,8 @@ fn test_struct_variant() { attrs: vec![], kind: ast::StructVariantKind( builder.struct_def() - .field("a").isize() - .field("b").isize() + .field("a").ty().isize() + .field("b").ty().isize() .build() ), id: ast::DUMMY_NODE_ID,