Loading src/variant.rs +41 −0 Original line number Diff line number Diff line use std::fmt::{self, Write}; use crate::Field; use crate::fields::Fields; use crate::formatter::Formatter; Loading @@ -10,6 +11,8 @@ use crate::r#type::Type; pub struct Variant { name: String, fields: Fields, /// Documentation for the variant. documentation: String, /// Annotations for field e.g., `#[serde(rename = "variant")]`. annotations: Vec<String>, discriminant: Option<String>, Loading @@ -21,12 +24,25 @@ impl Variant { Variant { name: name.to_string(), fields: Fields::Empty, documentation: String::new(), annotations: Vec::new(), discriminant: None, } } /// Add a named field to the variant. /// /// An enum variant can either set named fields with this function or tuple fields /// with [`tuple`](Self::tuple), but not both. pub fn push_named(&mut self, field: Field) -> &mut Self { self.fields.push_named(field); self } /// Add a named field to the variant. /// /// An enum variant can either set named fields with this function or tuple fields /// with [`tuple`](Self::tuple), but not both. pub fn named<T>(&mut self, name: impl ToString, ty: T) -> &mut Self where T: Into<Type>, Loading @@ -35,12 +51,32 @@ impl Variant { self } /// Create a named field for the enum. /// /// An enum variant can either set named fields with this function or tuple fields /// with [`tuple`](Self::tuple), but not both. pub fn new_named<T>(&mut self, name: impl ToString, ty: T) -> &mut Field where T: Into<Type>, { self.fields.new_named(name, ty) } /// Add a tuple field to the variant. /// /// An enum variant can either be a tuple with this function or have named fields /// with [`named`](Self::named), but not both. pub fn tuple(&mut self, ty: impl ToString) -> &mut Self { self.fields.tuple(ty); self } /// Set the variant documentation. pub fn doc(&mut self, documentation: impl ToString) -> &mut Self { self.documentation = documentation.to_string(); self } /// Add an anotation to the variant. pub fn annotation(&mut self, annotation: impl Into<String>) -> &mut Self { self.annotations.push(annotation.into()); Loading @@ -55,6 +91,11 @@ impl Variant { /// Formats the variant using the given formatter. pub fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result { if !self.documentation.is_empty() { for doc in self.documentation.lines() { writeln!(fmt, "/// {}", doc)?; } } for a in &self.annotations { write!(fmt, "{}", a)?; writeln!(fmt)?; Loading tests/codegen.rs +33 −0 Original line number Diff line number Diff line Loading @@ -464,6 +464,39 @@ enum IpAddrKind { assert_eq!(scope.to_string(), &expect[1..]); } #[test] fn enum_variant_with_doc() { let mut scope = Scope::new(); let enu = scope.new_enum("Foo"); enu.new_variant("Bar") .doc("Documentation for Bar variant") .new_named("bo", "bool") .doc("Documentation for the named bo field"); enu.new_variant("Baz") .doc("Documentation for Baz variant\nWith multiple lines"); enu.new_variant("Bazinga") .doc("Documentation for Bazinga variant") .tuple("String"); let expect = r#" enum Foo { /// Documentation for Bar variant Bar { /// Documentation for the named bo field bo: bool, } , /// Documentation for Baz variant /// With multiple lines Baz, /// Documentation for Bazinga variant Bazinga(String), }"#; assert_eq!(scope.to_string(), &expect[1..]); } #[test] fn enum_with_discriminants() { let mut scope = Scope::new(); Loading Loading
src/variant.rs +41 −0 Original line number Diff line number Diff line use std::fmt::{self, Write}; use crate::Field; use crate::fields::Fields; use crate::formatter::Formatter; Loading @@ -10,6 +11,8 @@ use crate::r#type::Type; pub struct Variant { name: String, fields: Fields, /// Documentation for the variant. documentation: String, /// Annotations for field e.g., `#[serde(rename = "variant")]`. annotations: Vec<String>, discriminant: Option<String>, Loading @@ -21,12 +24,25 @@ impl Variant { Variant { name: name.to_string(), fields: Fields::Empty, documentation: String::new(), annotations: Vec::new(), discriminant: None, } } /// Add a named field to the variant. /// /// An enum variant can either set named fields with this function or tuple fields /// with [`tuple`](Self::tuple), but not both. pub fn push_named(&mut self, field: Field) -> &mut Self { self.fields.push_named(field); self } /// Add a named field to the variant. /// /// An enum variant can either set named fields with this function or tuple fields /// with [`tuple`](Self::tuple), but not both. pub fn named<T>(&mut self, name: impl ToString, ty: T) -> &mut Self where T: Into<Type>, Loading @@ -35,12 +51,32 @@ impl Variant { self } /// Create a named field for the enum. /// /// An enum variant can either set named fields with this function or tuple fields /// with [`tuple`](Self::tuple), but not both. pub fn new_named<T>(&mut self, name: impl ToString, ty: T) -> &mut Field where T: Into<Type>, { self.fields.new_named(name, ty) } /// Add a tuple field to the variant. /// /// An enum variant can either be a tuple with this function or have named fields /// with [`named`](Self::named), but not both. pub fn tuple(&mut self, ty: impl ToString) -> &mut Self { self.fields.tuple(ty); self } /// Set the variant documentation. pub fn doc(&mut self, documentation: impl ToString) -> &mut Self { self.documentation = documentation.to_string(); self } /// Add an anotation to the variant. pub fn annotation(&mut self, annotation: impl Into<String>) -> &mut Self { self.annotations.push(annotation.into()); Loading @@ -55,6 +91,11 @@ impl Variant { /// Formats the variant using the given formatter. pub fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result { if !self.documentation.is_empty() { for doc in self.documentation.lines() { writeln!(fmt, "/// {}", doc)?; } } for a in &self.annotations { write!(fmt, "{}", a)?; writeln!(fmt)?; Loading
tests/codegen.rs +33 −0 Original line number Diff line number Diff line Loading @@ -464,6 +464,39 @@ enum IpAddrKind { assert_eq!(scope.to_string(), &expect[1..]); } #[test] fn enum_variant_with_doc() { let mut scope = Scope::new(); let enu = scope.new_enum("Foo"); enu.new_variant("Bar") .doc("Documentation for Bar variant") .new_named("bo", "bool") .doc("Documentation for the named bo field"); enu.new_variant("Baz") .doc("Documentation for Baz variant\nWith multiple lines"); enu.new_variant("Bazinga") .doc("Documentation for Bazinga variant") .tuple("String"); let expect = r#" enum Foo { /// Documentation for Bar variant Bar { /// Documentation for the named bo field bo: bool, } , /// Documentation for Baz variant /// With multiple lines Baz, /// Documentation for Bazinga variant Bazinga(String), }"#; assert_eq!(scope.to_string(), &expect[1..]); } #[test] fn enum_with_discriminants() { let mut scope = Scope::new(); Loading