pub struct GenStruct<'a, P: Parent> { /* private fields */ }Expand description
Builder to generate a struct.
Defaults to a struct with named fields struct <Name> { <field>: <ty>, ... }
Implementations§
Source§impl<'a, P: Parent> GenStruct<'a, P>
impl<'a, P: Parent> GenStruct<'a, P>
Sourcepub fn make_zst(&mut self) -> &mut Self
pub fn make_zst(&mut self) -> &mut Self
Make the struct a zero-sized type (no fields)
Any fields will be ignored
generator
.generate_struct("Foo")
.make_zst()
.add_field("bar", "u16")
.add_field("baz", "String");Generates:
struct Foo;Sourcepub fn make_tuple(&mut self) -> &mut Self
pub fn make_tuple(&mut self) -> &mut Self
Make the struct fields unnamed
The names of any field will be ignored
generator
.generate_struct("Foo")
.make_tuple()
.add_field("bar", "u16")
.add_field("baz", "String");Generates:
struct Foo(u16, String);Sourcepub fn make_pub(&mut self) -> &mut Self
pub fn make_pub(&mut self) -> &mut Self
Make the struct pub. By default the struct will have no visibility modifier and will only be visible in the current scope.
Sourcepub fn inherit_generics(&mut self) -> &mut Self
pub fn inherit_generics(&mut self) -> &mut Self
Inherit the generic parameters of the parent type.
// given a derive on struct Bar<'a>
generator
.generate_struct("Foo")
.inherit_generics()
.add_field("bar", "&'a str");Generates:
// given a derive on struct Bar<'a>
struct Foo<'a> {
bar: &'a str
}Sourcepub fn with_generics(
&mut self,
generics: impl IntoIterator<Item = Generic>,
) -> &mut Self
pub fn with_generics( &mut self, generics: impl IntoIterator<Item = Generic>, ) -> &mut Self
Append generic parameters to the type.
generator
.generate_struct("Foo")
.with_generics([Lifetime { ident: Ident::new("a", Span::call_site()), constraint: vec![] }.into()])
.add_field("bar", "&'a str");Generates:
struct Foo<'a> {
bar: &'a str
}Sourcepub fn with_generic(&mut self, generic: Generic) -> &mut Self
pub fn with_generic(&mut self, generic: Generic) -> &mut Self
Add a generic parameter to the type.
generator
.generate_struct("Foo")
.with_generic(Lifetime { ident: Ident::new("a", Span::call_site()), constraint: vec![] }.into())
.add_field("bar", "&'a str");Generates:
struct Foo<'a> {
bar: &'a str
}Sourcepub fn with_derive(&mut self, derive: impl Into<Path>) -> &mut Self
pub fn with_derive(&mut self, derive: impl Into<Path>) -> &mut Self
Add a derive macro to the struct.
generator
.generate_struct("Foo")
.with_derive("Clone")
.with_derive("Default")
.with_derive(Path::from_iter(vec!["serde", "Deserialize"]));Generates:
#[derive(Clone, Default, serde::Deserialize)]
struct Foo { }Sourcepub fn with_derives<T: Into<Path>>(
&mut self,
derives: impl IntoIterator<Item = T>,
) -> &mut Self
pub fn with_derives<T: Into<Path>>( &mut self, derives: impl IntoIterator<Item = T>, ) -> &mut Self
Add derive macros to the struct.
generator
.generate_struct("Foo")
.with_derives([
"Clone".into(),
"Default".into(),
Path::from_iter(vec!["serde", "Deserialize"]),
]);Generates:
#[derive(Clone, Default, serde::Deserialize)]
struct Foo { }Sourcepub fn with_attribute(
&mut self,
name: impl AsRef<str>,
value: impl FnOnce(&mut StreamBuilder) -> Result,
) -> Result<&mut Self>
pub fn with_attribute( &mut self, name: impl AsRef<str>, value: impl FnOnce(&mut StreamBuilder) -> Result, ) -> Result<&mut Self>
Add an attribute to the struct. For #[derive(...)], use with_derive
instead.
generator
.generate_struct("Foo")
.with_attribute("serde", |b| {
b.push_parsed("(rename_all = \"camelCase\")")?;
Ok(())
})?;Generates:
#[serde(rename_all = "camelCase")]
struct Foo { }Sourcepub fn with_parsed_attribute(
&mut self,
attribute: impl AsRef<str>,
) -> Result<&mut Self>
pub fn with_parsed_attribute( &mut self, attribute: impl AsRef<str>, ) -> Result<&mut Self>
Add a parsed attribute to the struct. For #[derive(...)], use with_derive
instead.
generator
.generate_struct("Foo")
.with_parsed_attribute("serde(rename_all = \"camelCase\")")?;Generates:
#[serde(rename_all = "camelCase")]
struct Foo { }Sourcepub fn with_attribute_stream(
&mut self,
attribute: impl Into<TokenStream>,
) -> &mut Self
pub fn with_attribute_stream( &mut self, attribute: impl Into<TokenStream>, ) -> &mut Self
Add a token stream as an attribute to the struct. For #[derive(...)], use
with_derive instead.
let attribute = "serde(rename_all = \"camelCase\")".parse::<TokenStream>().unwrap();
generator
.generate_struct("Foo")
.with_attribute_stream(attribute);Generates:
#[serde(rename_all = "camelCase")]
struct Foo { }Sourcepub fn add_field(
&mut self,
name: impl Into<String>,
ty: impl Into<String>,
) -> FieldBuilder<'_, Self>
pub fn add_field( &mut self, name: impl Into<String>, ty: impl Into<String>, ) -> FieldBuilder<'_, Self>
Add a field to the struct.
Names are ignored when the Struct’s fields are unnamed
generator
.generate_struct("Foo")
.add_field("bar", "u16")
.add_field("baz", "String");Generates:
struct Foo {
bar: u16,
baz: String,
};Sourcepub fn impl_for(&mut self, name: impl Into<StringOrIdent>) -> ImplFor<'_, Self>
pub fn impl_for(&mut self, name: impl Into<StringOrIdent>) -> ImplFor<'_, Self>
Add an impl <name> for <struct>
Sourcepub fn impl(&mut self) -> Impl<'_, Self>
pub fn impl(&mut self) -> Impl<'_, Self>
Generate an impl <name> implementation. See Impl for more information.