Skip to content

Commit de0d171

Browse files
committed
Add a annotation field to Field and with_annotation() fn
1 parent 3981c19 commit de0d171

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/lib.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ pub struct Field {
137137

138138
/// Field documentation
139139
documentation: String,
140+
141+
/// Field annotation
142+
annotation: String,
140143
}
141144

142145
/// Defines an associated type.
@@ -1097,6 +1100,7 @@ impl Field {
10971100
name: name.into(),
10981101
ty: ty.into(),
10991102
documentation: String::new(),
1103+
annotation: String::new(),
11001104
}
11011105
}
11021106

@@ -1106,6 +1110,11 @@ impl Field {
11061110
self
11071111
}
11081112

1113+
/// Set field's annotation.
1114+
pub fn with_annotation(&mut self, annotation: &str) -> &mut Self {
1115+
self.annotation = annotation.into();
1116+
self
1117+
}
11091118
}
11101119

11111120
// ===== impl Fields =====
@@ -1133,6 +1142,7 @@ impl Fields {
11331142
name: name.to_string(),
11341143
ty: ty.into(),
11351144
documentation: String::new(),
1145+
annotation: String::new(),
11361146
})
11371147
}
11381148

@@ -1162,6 +1172,9 @@ impl Fields {
11621172
if !f.documentation.is_empty() {
11631173
write!(fmt, "/// {}\n", f.documentation)?;
11641174
}
1175+
if !f.annotation.is_empty() {
1176+
write!(fmt, "{}\n", f.annotation)?;
1177+
}
11651178
write!(fmt, "{}: ", f.name)?;
11661179
f.ty.fmt(fmt)?;
11671180
write!(fmt, ",\n")?;
@@ -1238,6 +1251,7 @@ impl Impl {
12381251
name: name.to_string(),
12391252
ty: ty.into(),
12401253
documentation: String::new(),
1254+
annotation: String::new(),
12411255
});
12421256

12431257
self
@@ -1382,7 +1396,11 @@ impl Function {
13821396
self.args.push(Field {
13831397
name: name.to_string(),
13841398
ty: ty.into(),
1399+
// While a `Field` is used here, both `documentation`
1400+
// and `annotation` does not make sense for function arguments.
1401+
// Simply use empty strings.
13851402
documentation: String::new(),
1403+
annotation: String::new(),
13861404
});
13871405

13881406
self

tests/codegen.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,33 @@ fn single_struct_documented_field() {
4747
let mut scope = Scope::new();
4848

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

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

5354
let mut field1 = Field::new("one", "usize");
5455
field1.with_documentation(doc);
56+
struct_.push_field(field1);
57+
58+
let mut field2 = Field::new("two", "usize");
59+
field2.with_annotation(anot);
60+
struct_.push_field(field2);
61+
62+
let mut field3 = Field::new("three", "usize");
63+
field3.with_documentation(doc).with_annotation(anot);
64+
struct_.push_field(field3);
5565

5666
scope.push_struct(struct_);
5767

5868
let expect = r#"
5969
struct Foo {
6070
/// Field's documentation
6171
one: usize,
72+
#[serde(rename = "bar")]
73+
two: usize,
74+
/// Field's documentation
75+
#[serde(rename = "bar")]
76+
three: usize,
6277
}"#;
6378

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

0 commit comments

Comments
 (0)