diff --git a/src/generate/gen_enum.rs b/src/generate/gen_enum.rs index 08d4817..62a0a44 100644 --- a/src/generate/gen_enum.rs +++ b/src/generate/gen_enum.rs @@ -3,7 +3,41 @@ use crate::parse::Visibility; use crate::prelude::{Delimiter, Ident, Span}; use crate::Result; -/// Builder to generate a `struct { : , ... }` +/// Builder to generate an `enum { { ... }, ... }` +/// +/// ``` +/// # use virtue::prelude::Generator; +/// # let mut generator = Generator::with_name("Fooz"); +/// { +/// let mut enumgen = generator.generate_enum("Foo"); +/// enumgen +/// .add_value("ZST") +/// .make_zst(); +/// enumgen +/// .add_value("Named") +/// .add_field("bar", "u16") +/// .add_field("baz", "String"); +/// enumgen +/// .add_value("Unnamed") +/// .add_field("", "u16") +/// .add_field("baz", "String") +/// .make_fields_unnamed(); +/// } +/// # generator.assert_eq("enum Foo { ZST , Named { bar : u16 , baz : String , } , Unnamed (u16 , String ,) , }"); +/// # Ok::<_, virtue::Error>(()) +/// ``` +/// +/// Generates: +/// ``` +/// enum Foo { +/// ZST, +/// Named { +/// bar: u16, +/// baz: String, +/// }, +/// Unnamed(u16, String), +/// }; +/// ``` pub struct GenEnum<'a, P: Parent> { parent: &'a mut P, name: Ident, diff --git a/src/generate/gen_struct.rs b/src/generate/gen_struct.rs index c5e8e6c..1559472 100644 --- a/src/generate/gen_struct.rs +++ b/src/generate/gen_struct.rs @@ -2,7 +2,8 @@ use super::{Impl, ImplFor, Parent, StreamBuilder, StringOrIdent}; use crate::parse::Visibility; use crate::prelude::{Delimiter, Ident, Span}; -/// Builder to generate a `struct { : , ... }` +/// Builder to generate a struct. +/// Defaults to a struct with named fields `struct { : , ... }` pub struct GenStruct<'a, P: Parent> { parent: &'a mut P, name: Ident, @@ -27,6 +28,23 @@ impl<'a, P: Parent> GenStruct<'a, P> { /// Make the struct a zero-sized type (no fields) /// /// Any fields will be ignored + /// + /// ``` + /// # use virtue::prelude::Generator; + /// # let mut generator = Generator::with_name("Fooz"); + /// generator + /// .generate_struct("Foo") + /// .make_zst() + /// .add_field("bar", "u16") + /// .add_field("baz", "String"); + /// # generator.assert_eq("struct Foo ;"); + /// # Ok::<_, virtue::Error>(()) + /// ``` + /// + /// Generates: + /// ``` + /// struct Foo; + /// ``` pub fn make_zst(&mut self) -> &mut Self { self.struct_type = StructType::Zst; self @@ -35,6 +53,23 @@ impl<'a, P: Parent> GenStruct<'a, P> { /// Make the struct fields unnamed /// /// The names of any field will be ignored + /// + /// ``` + /// # use virtue::prelude::Generator; + /// # let mut generator = Generator::with_name("Fooz"); + /// generator + /// .generate_struct("Foo") + /// .make_fields_unnamed() + /// .add_field("bar", "u16") + /// .add_field("baz", "String"); + /// # generator.assert_eq("struct Foo (u16 , String ,) ;"); + /// # Ok::<_, virtue::Error>(()) + /// ``` + /// + /// Generates: + /// ``` + /// struct Foo(u16, String); + /// ``` pub fn make_fields_unnamed(&mut self) -> &mut Self { self.struct_type = StructType::Unnamed; self @@ -49,6 +84,25 @@ impl<'a, P: Parent> GenStruct<'a, P> { /// Add a *private* field to the struct. For adding a public field, see `add_pub_field` /// /// Names are ignored when the Struct's fields are unnamed + /// + /// ``` + /// # use virtue::prelude::Generator; + /// # let mut generator = Generator::with_name("Fooz"); + /// generator + /// .generate_struct("Foo") + /// .add_field("bar", "u16") + /// .add_field("baz", "String"); + /// # generator.assert_eq("struct Foo { bar : u16 , baz : String , }"); + /// # Ok::<_, virtue::Error>(()) + /// ``` + /// + /// Generates: + /// ``` + /// struct Foo { + /// bar: u16, + /// baz: String, + /// }; + /// ``` pub fn add_field(&mut self, name: impl Into, ty: impl Into) -> &mut Self { self.fields.push(StructField { name: name.into(), diff --git a/src/generate/generate_mod.rs b/src/generate/generate_mod.rs index 9b8d4e5..859706d 100644 --- a/src/generate/generate_mod.rs +++ b/src/generate/generate_mod.rs @@ -1,4 +1,4 @@ -use super::{GenStruct, GenEnum, Impl, Parent, StreamBuilder}; +use super::{GenEnum, GenStruct, Impl, Parent, StreamBuilder}; use crate::{ parse::Visibility, prelude::{Delimiter, Ident, Span}, diff --git a/src/generate/generator.rs b/src/generate/generator.rs index b42e03f..4ddbfd9 100644 --- a/src/generate/generator.rs +++ b/src/generate/generator.rs @@ -1,4 +1,4 @@ -use super::{GenerateMod, Impl, ImplFor, StreamBuilder, StringOrIdent, GenStruct, GenEnum}; +use super::{GenEnum, GenStruct, GenerateMod, Impl, ImplFor, StreamBuilder, StringOrIdent}; use crate::parse::{GenericConstraints, Generics}; use crate::prelude::{Ident, TokenStream};