Skip to content

Commit 444dfd1

Browse files
committed
Add annotations for structs, enums and variants. carllerche#34
1 parent db73ea6 commit 444dfd1

3 files changed

Lines changed: 24 additions & 0 deletions

File tree

src/enum.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ impl Enum {
7272
self
7373
}
7474

75+
/// Add an arbitrary macro.
76+
pub fn r#macro(&mut self, r#macro: &str) -> &mut Self {
77+
self.type_def.r#macro(r#macro);
78+
self
79+
}
80+
7581
/// Push a variant to the enum, returning a mutable reference to it.
7682
pub fn new_variant(&mut self, name: &str) -> &mut Variant {
7783
self.push_variant(Variant::new(name));

src/struct.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ impl Struct {
8585
self
8686
}
8787

88+
/// Add an arbitrary macro.
89+
pub fn r#macro(&mut self, r#macro: &str) -> &mut Self {
90+
self.type_def.r#macro(r#macro);
91+
self
92+
}
93+
8894
/// Push a named field to the struct.
8995
///
9096
/// A struct can either set named fields with this function or tuple fields

src/variant.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::r#type::Type;
1010
pub struct Variant {
1111
name: String,
1212
fields: Fields,
13+
annotations: Vec<String>,
1314
}
1415

1516
impl Variant {
@@ -18,6 +19,7 @@ impl Variant {
1819
Variant {
1920
name: name.to_string(),
2021
fields: Fields::Empty,
22+
annotations: Vec::new(),
2123
}
2224
}
2325

@@ -36,8 +38,18 @@ impl Variant {
3638
self
3739
}
3840

41+
/// Add an anotation to the variant.
42+
pub fn annotation(&mut self, annotation: &str) -> &mut Self {
43+
self.annotations.push(annotation.to_string());
44+
self
45+
}
46+
3947
/// Formats the variant using the given formatter.
4048
pub fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
49+
for a in &self.annotations {
50+
write!(fmt, "{}", a)?;
51+
write!(fmt, "\n")?;
52+
}
4153
write!(fmt, "{}", self.name)?;
4254
self.fields.fmt(fmt)?;
4355
write!(fmt, ",\n")?;

0 commit comments

Comments
 (0)