Skip to main content

GenEnum

Struct GenEnum 

Source
pub struct GenEnum<'a, P: Parent> { /* private fields */ }
Expand description

Builder to generate an enum <Name> { <value> { ... }, ... }

{
    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")
        .make_tuple()
        .add_field("", "u16")
        .add_field("baz", "String");
}

Generates:

enum Foo {
    ZST,
    Named {
        bar: u16,
        baz: String,
    },
    Unnamed(u16, String),
};

Implementations§

Source§

impl<'a, P: Parent> GenEnum<'a, P>

Source

pub fn make_pub(&mut self) -> &mut Self

Make the enum pub. By default the struct will have no visibility modifier and will only be visible in the current scope.

Source

pub fn with_derive(&mut self, derive: impl Into<Path>) -> &mut Self

Add a derive macro to the enum.

generator
    .generate_enum("Foo")
    .with_derive("Clone")
    .with_derive("Default")
    .with_derive(Path::from_iter(vec!["serde", "Deserialize"]));

Generates:

#[derive(Clone, Default, serde::Deserialize)]
enum Foo { }
Source

pub fn with_derives<T: Into<Path>>( &mut self, derives: impl IntoIterator<Item = T>, ) -> &mut Self

Add derive macros to the enum.

generator
    .generate_enum("Foo")
    .with_derives([
        "Clone".into(),
        "Default".into(),
        Path::from_iter(vec!["serde", "Deserialize"]),
    ]);

Generates:

#[derive(Clone, Default, serde::Deserialize)]
enum Foo { }
Source

pub fn with_attribute( &mut self, name: impl AsRef<str>, value: impl FnOnce(&mut StreamBuilder) -> Result, ) -> Result<&mut Self>

Add an attribute to the enum. For #[derive(...)], use with_derive instead.

generator
    .generate_enum("Foo")
    .with_attribute("serde", |b| {
        b.push_parsed("(untagged)")?;
        Ok(())
    })?;

Generates:

#[serde(untagged)]
enum Foo { }
Source

pub fn with_parsed_attribute( &mut self, attribute: impl AsRef<str>, ) -> Result<&mut Self>

Add a parsed attribute to the enum. For #[derive(...)], use with_derive instead.


generator
    .generate_enum("Foo")
    .with_parsed_attribute("serde(untagged)")?;

Generates:

#[serde(untagged)]
enum Foo { }
Source

pub fn with_attribute_stream( &mut self, attribute: impl Into<TokenStream>, ) -> &mut Self

Add a token stream as an attribute to the enum. For #[derive(...)], use with_derive instead.


let attribute = "serde(untagged)".parse::<TokenStream>().unwrap();
generator
    .generate_enum("Foo")
    .with_attribute_stream(attribute);

Generates:

#[serde(untagged)]
enum Foo { }
Source

pub fn inherit_generics(&mut self) -> &mut Self

Inherit the generic parameters of the parent type.

// given a derive on enum Bar<'a>
generator
    .generate_enum("Foo")
    .inherit_generics()
    .add_value("Bar")
    .make_tuple()
    .add_field("bar", "&'a str");

Generates:

// given a derive on enum Bar<'a>
enum Foo<'a> {
    Bar(&'a str)
}
Source

pub fn with_generics( &mut self, generics: impl IntoIterator<Item = Generic>, ) -> &mut Self

Append generic parameters to the type.

generator
    .generate_enum("Foo")
    .with_generics([Lifetime { ident: Ident::new("a", Span::call_site()), constraint: vec![] }.into()])
    .add_value("Bar")
    .make_tuple()
    .add_field("bar", "&'a str");

Generates:

enum Foo<'a> {
    Bar(&'a str)
}
Source

pub fn with_generic(&mut self, generic: Generic) -> &mut Self

Add a generic parameter to the type.

generator
    .generate_enum("Foo")
    .with_generic(Lifetime { ident: Ident::new("a", Span::call_site()), constraint: vec![] }.into())
    .add_value("Bar")
    .make_tuple()
    .add_field("bar", "&'a str");

Generates:

enum Foo<'a> {
    Bar(&'a str)
}
Source

pub fn add_value(&mut self, name: impl Into<String>) -> &mut EnumValue

Add an enum value

Returns a builder for the value that’s similar to GenStruct

Source

pub fn impl_for(&mut self, name: impl Into<StringOrIdent>) -> ImplFor<'_, Self>

Add an impl <name> for <enum>

Source

pub fn impl(&mut self) -> Impl<'_, Self>

Generate an impl <name> implementation. See Impl for more information.

Source

pub fn generate_impl(&mut self) -> Impl<'_, Self>

Generate an impl <name> implementation. See Impl for more information.

Alias for impl which doesn’t need a r# prefix.

Trait Implementations§

Source§

impl<'a, P: Parent> Drop for GenEnum<'a, P>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<'a, P: Parent> Parent for GenEnum<'a, P>

Auto Trait Implementations§

§

impl<'a, P> Freeze for GenEnum<'a, P>

§

impl<'a, P> RefUnwindSafe for GenEnum<'a, P>
where P: RefUnwindSafe,

§

impl<'a, P> !Send for GenEnum<'a, P>

§

impl<'a, P> !Sync for GenEnum<'a, P>

§

impl<'a, P> Unpin for GenEnum<'a, P>

§

impl<'a, P> UnsafeUnpin for GenEnum<'a, P>

§

impl<'a, P> !UnwindSafe for GenEnum<'a, P>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.