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 implementstd::error::Error + Send + Sync + 'staticfor 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§
Required Methods§
Sourcefn file_extension(&self) -> &str
fn file_extension(&self) -> &str
Provided Methods§
Sourcefn metadata(&self) -> GeneratorMetadata
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.