diff --git a/src/lib.rs b/src/lib.rs index 11d4d21..f3a0a99 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -137,6 +137,9 @@ pub struct Field { /// Field documentation documentation: String, + + /// Field annotation + annotation: String, } /// Defines an associated type. @@ -1097,6 +1100,7 @@ impl Field { name: name.into(), ty: ty.into(), documentation: String::new(), + annotation: String::new(), } } @@ -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 ===== @@ -1133,6 +1142,7 @@ impl Fields { name: name.to_string(), ty: ty.into(), documentation: String::new(), + annotation: String::new(), }) } @@ -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")?; @@ -1238,6 +1251,7 @@ impl Impl { name: name.to_string(), ty: ty.into(), documentation: String::new(), + annotation: String::new(), }); self @@ -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 diff --git a/tests/codegen.rs b/tests/codegen.rs index bde215d..7ea0d65 100644 --- a/tests/codegen.rs +++ b/tests/codegen.rs @@ -47,11 +47,21 @@ 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_); @@ -59,6 +69,11 @@ fn single_struct_documented_field() { 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..]);