virtue::generate

Struct FieldBuilder

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

A builder for struct or enum variant fields.

Implementations§

Source§

impl<P> FieldBuilder<'_, P>

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 field.

generator
    .generate_struct("Foo")
    .add_field("foo", "u16")
    .make_pub()
    .with_attribute("serde", |b| {
        b.push_parsed("(default)")?;
        Ok(())
    })?;
generator
    .generate_enum("Bar")
    .add_value("Baz")
    .add_field("baz", "bool")
    .with_attribute("serde", |b| {
        b.push_parsed("(default)")?;
        Ok(())
    })?;
enum Bar { Baz { # [serde (default)] baz : bool , } , }");

Generates:

struct Foo {
    #[serde(default)]
    pub bar: u16
}

enum Bar {
    Baz {
        #[serde(default)]
        baz: bool
    }
}
Source

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

Add a parsed attribute to the field.

generator
    .generate_struct("Foo")
    .add_field("foo", "u16")
    .make_pub()
    .with_parsed_attribute("serde(default)")?;
generator
    .generate_enum("Bar")
    .add_value("Baz")
    .add_field("baz", "bool")
    .with_parsed_attribute("serde(default)")?;
enum Bar { Baz { # [serde (default)] baz : bool , } , }");

Generates:

struct Foo {
    #[serde(default)]
    pub bar: u16
}

enum Bar {
    Baz {
        #[serde(default)]
        baz: bool
    }
}
Source

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

Add a token stream as an attribute to the field.

let attribute = "serde(default)".parse::<TokenStream>().unwrap();
generator
    .generate_struct("Foo")
    .add_field("foo", "u16")
    .make_pub()
    .with_attribute_stream(attribute);

Generates:

struct Foo {
    #[serde(default)]
    pub bar: u16
}
Source

pub fn add_field( &mut self, name: impl Into<String>, ty: impl Into<String>, ) -> &mut Self

Add a field to the parent type.

generator
    .generate_struct("Foo")
    .add_field("foo", "u16")
    .add_field("bar", "bool");

Generates:

struct Foo {
    foo: u16,
    bar: bool
}
Source§

impl<'a, P: Parent> FieldBuilder<'_, GenStruct<'a, P>>

Source

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

Make the field public.

Trait Implementations§

Source§

impl<'a, P> From<&'a mut Vec<Field>> for FieldBuilder<'a, P>

Source§

fn from(fields: &'a mut Vec<Field>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

impl<'a, P> Unpin for FieldBuilder<'a, P>
where P: Unpin,

§

impl<'a, P> !UnwindSafe for FieldBuilder<'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.