Skip to content

Commit 6e17dc2

Browse files
committed
Let Field.annotation be a Vec<String> instead of String for multi-line annotation
1 parent 95b6673 commit 6e17dc2

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

src/lib.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ pub struct Field {
139139
documentation: String,
140140

141141
/// Field annotation
142-
annotation: String,
142+
annotation: Vec<String>,
143143
}
144144

145145
/// Defines an associated type.
@@ -1100,7 +1100,7 @@ impl Field {
11001100
name: name.into(),
11011101
ty: ty.into(),
11021102
documentation: String::new(),
1103-
annotation: String::new(),
1103+
annotation: Vec::new(),
11041104
}
11051105
}
11061106

@@ -1111,8 +1111,8 @@ impl Field {
11111111
}
11121112

11131113
/// Set field's annotation.
1114-
pub fn annotation(&mut self, annotation: &str) -> &mut Self {
1115-
self.annotation = annotation.into();
1114+
pub fn annotation(&mut self, annotation: Vec<&str>) -> &mut Self {
1115+
self.annotation = annotation.iter().map(|ann| ann.to_string()).collect();
11161116
self
11171117
}
11181118
}
@@ -1142,7 +1142,7 @@ impl Fields {
11421142
name: name.to_string(),
11431143
ty: ty.into(),
11441144
documentation: String::new(),
1145-
annotation: String::new(),
1145+
annotation: Vec::new(),
11461146
})
11471147
}
11481148

@@ -1173,7 +1173,9 @@ impl Fields {
11731173
write!(fmt, "/// {}\n", f.documentation)?;
11741174
}
11751175
if !f.annotation.is_empty() {
1176-
write!(fmt, "{}\n", f.annotation)?;
1176+
for ann in &f.annotation {
1177+
write!(fmt, "{}\n", ann)?;
1178+
}
11771179
}
11781180
write!(fmt, "{}: ", f.name)?;
11791181
f.ty.fmt(fmt)?;
@@ -1251,7 +1253,7 @@ impl Impl {
12511253
name: name.to_string(),
12521254
ty: ty.into(),
12531255
documentation: String::new(),
1254-
annotation: String::new(),
1256+
annotation: Vec::new(),
12551257
});
12561258

12571259
self
@@ -1400,7 +1402,7 @@ impl Function {
14001402
// and `annotation` does not make sense for function arguments.
14011403
// Simply use empty strings.
14021404
documentation: String::new(),
1403-
annotation: String::new(),
1405+
annotation: Vec::new(),
14041406
});
14051407

14061408
self

tests/codegen.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ 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")]"#;
5150

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

@@ -56,11 +55,14 @@ fn single_struct_documented_field() {
5655
struct_.push_field(field1);
5756

5857
let mut field2 = Field::new("two", "usize");
59-
field2.annotation(anot);
58+
field2.annotation(vec![r#"#[serde(rename = "bar")]"#]);
6059
struct_.push_field(field2);
6160

6261
let mut field3 = Field::new("three", "usize");
63-
field3.doc(doc).annotation(anot);
62+
field3.doc(doc).annotation(vec![
63+
r#"#[serde(skip_serializing)]"#,
64+
r#"#[serde(skip_deserializing)]"#,
65+
]);
6466
struct_.push_field(field3);
6567

6668
scope.push_struct(struct_);
@@ -72,7 +74,8 @@ struct Foo {
7274
#[serde(rename = "bar")]
7375
two: usize,
7476
/// Field's documentation
75-
#[serde(rename = "bar")]
77+
#[serde(skip_serializing)]
78+
#[serde(skip_deserializing)]
7679
three: usize,
7780
}"#;
7881

0 commit comments

Comments
 (0)