Skip to content
This repository has been archived by the owner on May 6, 2020. It is now read-only.

Commit

Permalink
Fix adding attributes to fields
Browse files Browse the repository at this point in the history
  • Loading branch information
erickt committed Apr 20, 2015
1 parent 48008b3 commit e840337
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]

name = "aster"
version = "0.2.1"
version = "0.2.2"
authors = ["Erick Tryzelaar <[email protected]>"]
license = "MIT/Apache-2.0"
description = "A libsyntax ast builder"
Expand Down
2 changes: 1 addition & 1 deletion src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ impl<F> ItemStructBuilder<F>
StructDefBuilder::new_with_callback(self).span(span).with_field(field)
}

pub fn field<T>(self, id: T) -> TyBuilder<StructFieldBuilder<StructDefBuilder<Self>>>
pub fn field<T>(self, id: T) -> StructFieldBuilder<StructDefBuilder<Self>>
where T: ToIdent,
{
let span = self.builder.span;
Expand Down
5 changes: 2 additions & 3 deletions src/struct_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,11 @@ impl<F> StructDefBuilder<F>
self
}

pub fn field<T>(self, id: T) -> TyBuilder<StructFieldBuilder<Self>>
pub fn field<T>(self, id: T) -> StructFieldBuilder<Self>
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 {
Expand Down
2 changes: 1 addition & 1 deletion src/variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ impl<F> VariantStructBuilder<F>
StructDefBuilder::new_with_callback(self).span(span).with_field(field)
}

pub fn field<T>(self, id: T) -> TyBuilder<StructFieldBuilder<StructDefBuilder<Self>>>
pub fn field<T>(self, id: T) -> StructFieldBuilder<StructDefBuilder<Self>>
where T: ToIdent,
{
let span = self.builder.span;
Expand Down
20 changes: 10 additions & 10 deletions tests/test_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand All @@ -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(),
),
Expand Down Expand Up @@ -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();

Expand All @@ -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(),
],
},
Expand Down
66 changes: 63 additions & 3 deletions tests/test_struct_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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!(
Expand Down Expand Up @@ -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,
})
);
}
8 changes: 4 additions & 4 deletions tests/test_variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand All @@ -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,
Expand Down

0 comments on commit e840337

Please sign in to comment.