Skip to content

Commit

Permalink
Add a annotation field to Field and with_annotation() fn
Browse files Browse the repository at this point in the history
  • Loading branch information
nbigaouette committed Jan 6, 2018
1 parent 3981c19 commit de0d171
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ pub struct Field {

/// Field documentation
documentation: String,

/// Field annotation
annotation: String,
}

/// Defines an associated type.
Expand Down Expand Up @@ -1097,6 +1100,7 @@ impl Field {
name: name.into(),
ty: ty.into(),
documentation: String::new(),
annotation: String::new(),
}
}

Expand All @@ -1106,6 +1110,11 @@ impl Field {
self
}

/// Set field's annotation.
pub fn with_annotation(&mut self, annotation: &str) -> &mut Self {
self.annotation = annotation.into();
self
}
}

// ===== impl Fields =====
Expand Down Expand Up @@ -1133,6 +1142,7 @@ impl Fields {
name: name.to_string(),
ty: ty.into(),
documentation: String::new(),
annotation: String::new(),
})
}

Expand Down Expand Up @@ -1162,6 +1172,9 @@ impl Fields {
if !f.documentation.is_empty() {
write!(fmt, "/// {}\n", f.documentation)?;
}
if !f.annotation.is_empty() {
write!(fmt, "{}\n", f.annotation)?;
}
write!(fmt, "{}: ", f.name)?;
f.ty.fmt(fmt)?;
write!(fmt, ",\n")?;
Expand Down Expand Up @@ -1238,6 +1251,7 @@ impl Impl {
name: name.to_string(),
ty: ty.into(),
documentation: String::new(),
annotation: String::new(),
});

self
Expand Down Expand Up @@ -1382,7 +1396,11 @@ impl Function {
self.args.push(Field {
name: name.to_string(),
ty: ty.into(),
// While a `Field` is used here, both `documentation`
// and `annotation` does not make sense for function arguments.
// Simply use empty strings.
documentation: String::new(),
annotation: String::new(),
});

self
Expand Down
15 changes: 15 additions & 0 deletions tests/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,33 @@ fn single_struct_documented_field() {
let mut scope = Scope::new();

let doc = "Field's documentation";
let anot = r#"#[serde(rename = "bar")]"#;

let mut struct_ = Struct::new("Foo");

let mut field1 = Field::new("one", "usize");
field1.with_documentation(doc);
struct_.push_field(field1);

let mut field2 = Field::new("two", "usize");
field2.with_annotation(anot);
struct_.push_field(field2);

let mut field3 = Field::new("three", "usize");
field3.with_documentation(doc).with_annotation(anot);
struct_.push_field(field3);

scope.push_struct(struct_);

let expect = r#"
struct Foo {
/// Field's documentation
one: usize,
#[serde(rename = "bar")]
two: usize,
/// Field's documentation
#[serde(rename = "bar")]
three: usize,
}"#;

assert_eq!(scope.to_string(), &expect[1..]);
Expand Down

0 comments on commit de0d171

Please sign in to comment.