Skip to content

Commit

Permalink
Tweak API
Browse files Browse the repository at this point in the history
  • Loading branch information
NightEule5 committed Jun 12, 2024
1 parent fa84b16 commit 0f29d1f
Show file tree
Hide file tree
Showing 3 changed files with 259 additions and 212 deletions.
117 changes: 54 additions & 63 deletions src/generate/gen_enum.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{
AttributeContainer, Field, FieldContainer, Impl, ImplFor, Parent, StreamBuilder, StringOrIdent,
AttributeContainer, Field, FieldBuilder, Impl, ImplFor, Parent, StreamBuilder, StringOrIdent,
};
use crate::parse::{Generic, Generics, Visibility};
use crate::prelude::{Delimiter, Ident, Span};
Expand All @@ -21,9 +21,9 @@ use crate::Result;
/// .add_field("baz", "String");
/// enumgen
/// .add_value("Unnamed")
/// .make_tuple()
/// .add_field("", "u16")
/// .add_field("baz", "String")
/// .make_tuple();
/// .add_field("baz", "String");
/// }
/// # generator.assert_eq("enum Foo { ZST , Named { bar : u16 , baz : String , } , Unnamed (u16 , String ,) , }");
/// # Ok::<_, virtue::Error>(())
Expand All @@ -47,7 +47,7 @@ pub struct GenEnum<'a, P: Parent> {
generics: Option<Generics>,
values: Vec<EnumValue>,
derives: Vec<StringOrIdent>,
attributes: Vec<(String, StreamBuilder)>,
attributes: Vec<StreamBuilder>,
additional: Vec<StreamBuilder>,
}

Expand Down Expand Up @@ -133,10 +133,11 @@ impl<'a, P: Parent> GenEnum<'a, P> {
/// #[serde(untagged)]
/// enum Foo { }
/// ```
pub fn with_attribute<T>(&mut self, name: impl Into<String>, value: T) -> Result<&mut Self>
where
T: FnOnce(&mut StreamBuilder) -> Result,
{
pub fn with_attribute(
&mut self,
name: impl AsRef<str>,
value: impl FnOnce(&mut StreamBuilder) -> Result,
) -> Result<&mut Self> {
AttributeContainer::with_attribute(self, name, value)
}

Expand Down Expand Up @@ -263,7 +264,7 @@ impl<P: Parent> AttributeContainer for GenEnum<'_, P> {
&mut self.derives
}

fn attributes(&mut self) -> &mut Vec<(String, StreamBuilder)> {
fn attributes(&mut self) -> &mut Vec<StreamBuilder> {
&mut self.attributes
}
}
Expand Down Expand Up @@ -358,7 +359,7 @@ pub struct EnumValue {
name: Ident,
fields: Vec<Field>,
value_type: ValueType,
attributes: Vec<(String, StreamBuilder)>,
attributes: Vec<StreamBuilder>,
}

impl EnumValue {
Expand Down Expand Up @@ -386,6 +387,7 @@ impl EnumValue {
self.value_type = ValueType::Unnamed;
self
}

/// Add an attribute to the variant.
///
/// ```
Expand All @@ -409,76 +411,71 @@ impl EnumValue {
/// Bar { }
/// }
/// ```
pub fn with_attribute<T>(&mut self, name: impl Into<String>, value: T) -> Result<&mut Self>
where
T: FnOnce(&mut StreamBuilder) -> Result,
{
pub fn with_attribute(
&mut self,
name: impl AsRef<str>,
value: impl FnOnce(&mut StreamBuilder) -> Result,
) -> Result<&mut Self> {
AttributeContainer::with_attribute(self, name, value)
}

/// Add a *private* field to the struct. For adding a public field, see `add_pub_field`
/// Add a parsed attribute to the variant.
///
/// Names are ignored when the Struct's fields are unnamed
pub fn add_field(&mut self, name: impl Into<String>, ty: impl Into<String>) -> &mut Self {
self.fields
.push(Field::new(name.into(), Visibility::Default, ty.into()));
self
/// ```
/// # use virtue::prelude::Generator;
/// # let mut generator = Generator::with_name("Bar");
/// generator
/// .generate_enum("Foo")
/// .add_value("Bar")
/// .with_parsed_attribute("serde(rename_all = \"camelCase\")")?;
/// # generator.assert_eq("enum Foo { # [serde (rename_all = \"camelCase\")] Bar { } , }");
/// # Ok::<_, virtue::Error>(())
/// ```
///
/// Generates:
/// ```ignore
/// enum Foo {
/// #[serde(rename_all = "camelCase")]
/// Bar { }
/// }
/// ```
pub fn with_parsed_attribute(&mut self, attribute: impl AsRef<str>) -> Result<&mut Self> {
AttributeContainer::with_parsed_attribute(self, attribute)
}

/// Add a *private* field with an attribute to the variant.
/// Add a field to the enum value.
///
/// Names are ignored when the variant's fields are unnamed
/// Names are ignored when the enum value's fields are unnamed
///
/// ```
/// # use virtue::prelude::Generator;
/// # let mut generator = Generator::with_name("Bar");
/// # let mut generator = Generator::with_name("Fooz");
/// generator
/// .generate_enum("Foo")
/// .add_value("Bar")
/// .add_field_with_attribute("bar", "u16", "serde", |b| {
/// b.push_parsed("(default)")?;
/// Ok(())
/// })?;
/// # generator.assert_eq("enum Foo { Bar { # [serde (default)] bar : u16 , } , }");
/// .add_field("bar", "u16")
/// .add_field("baz", "String");
/// # generator.assert_eq("enum Foo { Bar { bar : u16 , baz : String , } , }");
/// # Ok::<_, virtue::Error>(())
/// ```
///
/// Generates:
/// ```ignore
/// ```
/// enum Foo {
/// Bar {
/// #[serde(default)]
/// bar: u16
/// bar: u16,
/// baz: String
/// }
/// }
/// };
/// ```
pub fn add_field_with_attribute<T>(
pub fn add_field(
&mut self,
name: impl Into<String>,
ty: impl Into<String>,
attribute_name: impl Into<String>,
attribute_value: T,
) -> Result<&mut Self>
where
T: FnOnce(&mut StreamBuilder) -> Result,
{
FieldContainer::add_field_with_attribute(
self,
name,
ty,
Visibility::Default,
attribute_name,
attribute_value,
)
}

/// Add a *public* field to the struct. For adding a public field, see `add_field`
///
/// Names are ignored when the Struct's fields are unnamed
pub fn add_pub_field(&mut self, name: impl Into<String>, ty: impl Into<String>) -> &mut Self {
self.fields
.push(Field::new(name.into(), Visibility::Pub, ty.into()));
self
) -> FieldBuilder<Self> {
let mut fields = FieldBuilder::from(&mut self.fields);
fields.add_field(name, ty);
fields
}
}

Expand All @@ -487,17 +484,11 @@ impl AttributeContainer for EnumValue {
unreachable!("enum variants cannot have derives")
}

fn attributes(&mut self) -> &mut Vec<(String, StreamBuilder)> {
fn attributes(&mut self) -> &mut Vec<StreamBuilder> {
&mut self.attributes
}
}

impl FieldContainer for EnumValue {
fn fields(&mut self) -> &mut Vec<Field> {
&mut self.fields
}
}

enum ValueType {
Named,
Unnamed,
Expand Down
Loading

0 comments on commit 0f29d1f

Please sign in to comment.