Skip to main content

CodeGenerator

Trait CodeGenerator 

Source
pub trait CodeGenerator {
    type Error: StdError + Send + Sync + 'static;

    // Required methods
    fn generate(&self, module: &IRModule) -> Result<String, Self::Error>;
    fn language(&self) -> &'static str;
    fn file_extension(&self) -> &str;

    // Provided methods
    fn validate(&self, _module: &IRModule) -> Result<(), Self::Error> { ... }
    fn format(&self, code: String) -> Result<String, Self::Error> { ... }
    fn metadata(&self) -> GeneratorMetadata { ... }
}
Expand description

Core trait for all code generators

Implement this trait to add support for generating code in new languages. The generator is responsible for converting an IR module into syntactically correct code in the target language.

§Type Parameters

  • Error - The error type produced by this generator. Must implement std::error::Error + Send + Sync + 'static for composability.

§Examples

use unistructgen_core::{CodeGenerator, IRModule};

struct MyGenerator {
    options: MyOptions,
}

impl CodeGenerator for MyGenerator {
    type Error = MyGeneratorError;

    fn generate(&self, module: &IRModule) -> Result<String, Self::Error> {
        // Generate code from IR
        todo!()
    }

    fn language(&self) -> &'static str {
        "MyLanguage"
    }

    fn file_extension(&self) -> &str {
        "my"
    }
}

Required Associated Types§

Source

type Error: StdError + Send + Sync + 'static

The error type this generator produces

Required Methods§

Source

fn generate(&self, module: &IRModule) -> Result<String, Self::Error>

Generate code from an IR module

§Arguments
  • module - The IR module to generate code from
§Returns

Returns Ok(String) containing the generated code on success.

§Errors

Returns Self::Error if code generation fails. The error should contain detailed information about what went wrong.

Source

fn language(&self) -> &'static str

Get the target language name

Used for diagnostics and user-facing messages.

§Examples
assert_eq!(rust_gen.language(), "Rust");
assert_eq!(typescript_gen.language(), "TypeScript");
Source

fn file_extension(&self) -> &str

Get the file extension for generated code

Used when writing generated code to files.

§Examples
assert_eq!(rust_gen.file_extension(), "rs");
assert_eq!(typescript_gen.file_extension(), "ts");

Provided Methods§

Source

fn validate(&self, _module: &IRModule) -> Result<(), Self::Error>

Validate IR before generation (optional)

Provides a way to check if the IR is compatible with this generator before attempting full code generation. Default implementation always returns Ok(()).

§Arguments
  • module - The IR module to validate
§Returns

Returns Ok(()) if IR is valid, Err otherwise.

Source

fn format(&self, code: String) -> Result<String, Self::Error>

Format generated code (optional)

Apply language-specific formatting to the generated code. Default implementation returns the code unchanged.

§Arguments
  • code - The generated code to format
§Returns

Returns formatted code on success.

Source

fn metadata(&self) -> GeneratorMetadata

Get metadata about the generator (optional)

Returns additional information about the generator’s capabilities, version, etc. Default implementation returns empty metadata.

Implementors§