Skip to main content

RustCodeGenerator

Struct RustCodeGenerator 

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

Main code generator for transforming ELO AST to Rust code

Provides methods for generating Rust code from ELO expressions, including literal values, field access, operators, and more.

§Example

use elo_rust::RustCodeGenerator;

let generator = RustCodeGenerator::new();
let int_literal = generator.generate_literal_integer(42).unwrap();

Implementations§

Source§

impl RustCodeGenerator

Source

pub fn new() -> Self

Create a new code generator instance with empty type context

Source

pub fn with_context(type_context: TypeContext) -> Self

Create a new code generator with a populated type context

§Arguments
  • type_context - Pre-configured type context with custom types
Source

pub fn is_valid(&self) -> bool

Check if the generator is in a valid state

Source

pub fn has_type(&self, type_name: &str) -> bool

Check if a type is registered in the context

Source

pub fn get_field_type( &self, type_name: &str, field_name: &str, ) -> Option<&RustType>

Get the type of a field in a registered type

Source

pub fn generate_function_signature( &self, name: &str, input_type: &str, ) -> Result<TokenStream, String>

Generate function signature for a validator

§Arguments
  • name - The name of the validation function
  • input_type - The type being validated
§Returns

A TokenStream representing the function signature

Source

pub fn generate_literal_integer( &self, value: i64, ) -> Result<TokenStream, String>

Generate code for an integer literal

Source

pub fn generate_literal_string( &self, value: &str, ) -> Result<TokenStream, String>

Generate code for a string literal

Source

pub fn generate_literal_bool(&self, value: bool) -> Result<TokenStream, String>

Generate code for a boolean literal

Source

pub fn generate_field_access( &self, receiver: &str, field: &str, ) -> Result<TokenStream, String>

Generate code for field access (e.g., user.age)

This generates the Rust code for accessing a field on a value. The receiver should be a valid Rust identifier (e.g., “user”, “input”).

§Arguments
  • receiver - The expression being accessed (e.g., “user”)
  • field - The field name (e.g., “age”)
§Returns

A TokenStream representing receiver.field

§Example
let gen = RustCodeGenerator::new();
let tokens = gen.generate_field_access("user", "age")?;
// Generates: user.age
Source

pub fn generate_comment(&self, _text: &str) -> Result<TokenStream, String>

Generate a single-line comment

Note: Comments are handled at the token manipulation level, not in token streams. This method is provided for future extensibility.

Source

pub fn generate_doc_comment(&self, _text: &str) -> Result<TokenStream, String>

Generate a documentation comment

Note: Doc comments are handled at the token manipulation level, not in token streams. This method is provided for future extensibility.

Source

pub fn generate_validator( &self, name: &str, _elo_expr: &str, input_type: &str, ) -> Result<TokenStream, String>

Generate a complete validator function from an ELO expression

§Arguments
  • name - The name of the validator function
  • _elo_expr - The ELO validation expression
  • input_type - The type being validated
§Returns

A TokenStream representing the complete validator function

Source

pub fn generate_validator_impl( &self, struct_name: &str, validator_fn_name: &str, input_type: &str, ) -> Result<TokenStream, String>

Generate validator implementation for a type

§Arguments
  • struct_name - The name of the struct implementing the validator
  • validator_fn_name - The name of the validation function
  • input_type - The type being validated
§Returns

A TokenStream representing the impl block

Trait Implementations§

Source§

impl Clone for RustCodeGenerator

Source§

fn clone(&self) -> RustCodeGenerator

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 RustCodeGenerator

Source§

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

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

impl Default for RustCodeGenerator

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.