Skip to main content

StructGen

Struct StructGen 

Source
pub struct StructGen { /* private fields */ }
Expand description

Main builder for generating Rust structs

§Examples

use unistructgen_core::api::{StructGen, FieldType};

let code = StructGen::new()
    .name("User")
    .doc("Represents a user in the system")
    .field("id", FieldType::I64)
    .field("username", FieldType::String)
    .field_optional("email", FieldType::String)
    .field_with(|f| f
        .doc("User's age")
        .range(0.0, 150.0)
    , "age", FieldType::I32)
    .with_serde()
    .with_debug()
    .with_clone()
    .generate()?;

Implementations§

Source§

impl StructGen

Source

pub fn new() -> Self

Create a new struct generator

Source

pub fn name(self, name: impl Into<String>) -> Self

Set struct name

Source

pub fn doc(self, doc: impl Into<String>) -> Self

Add documentation comment

Source

pub fn field(self, name: impl Into<String>, ty: FieldType) -> Self

Add a simple field

Source

pub fn field_optional(self, name: impl Into<String>, ty: FieldType) -> Self

Add an optional field

Source

pub fn field_with<F>( self, builder_fn: F, name: impl Into<String>, ty: FieldType, ) -> Self

Add a field with full builder customization

Source

pub fn field_raw(self, field: IRField) -> Self

Add a pre-built field

Source

pub fn derive(self, derive: impl Into<String>) -> Self

Add a custom derive

Source

pub fn with_serde(self) -> Self

Add serde derives (Serialize, Deserialize)

Source

pub fn with_debug(self) -> Self

Add Debug derive

Source

pub fn with_clone(self) -> Self

Add Clone derive

Source

pub fn with_default(self) -> Self

Add Default derive

Source

pub fn with_partial_eq(self) -> Self

Add PartialEq derive

Source

pub fn with_eq(self) -> Self

Add Eq derive (also adds PartialEq if missing)

Source

pub fn with_hash(self) -> Self

Add Hash derive

Source

pub fn with_common_derives(self) -> Self

Add all common derives (Debug, Clone, PartialEq, Eq, Hash, Default)

Source

pub fn attr(self, attr: impl Into<String>) -> Self

Add custom attribute

Source

pub fn nested_struct(self, nested: StructGen) -> Self

Add a nested struct type

Source

pub fn nested_enum(self, nested: EnumGen) -> Self

Add a nested enum type

Source

pub fn build_ir_struct(&self) -> IRStruct

Build the IR struct (without generating code)

Source

pub fn into_ir_struct(self) -> IRStruct

Consume and build the IR struct

Source

pub fn build_ir_module(self) -> IRModule

Build the IR module containing this struct and nested types

Source

pub fn generate(self) -> ApiResult<String>

Generate Rust code string

Source

pub fn generate_with_options(self, options: RenderOptions) -> ApiResult<String>

Generate with custom render options

Trait Implementations§

Source§

impl Clone for StructGen

Source§

fn clone(&self) -> StructGen

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for StructGen

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for StructGen

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.