Rust API

generate

Generate code from an AST.

pub async fn generate(
    ast: Ast,
    options: GenerateOptions,
    plugins: Vec<SharedPluginable>,
) -> Result<CodeResult, CompileError>

Generates code from ast.

The builtin generator is the terminal fallback after every generate hook defers, and the resolved output source-map mode is applied to the winning result.

Parameters

NameTypeDescription
astAstThe AST to generate from.
optionsGenerateOptionsThe generate options.
pluginsVec<SharedPluginable>The plugins to run.

GenerateOptions

Every field is optional and resolved against defaults before use.

FieldTypeDefaultDescription
cwdOption<PathBuf>process working dirFilesystem root used to resolve relative paths.
fileOption<String>index.tsThe file being generated; used for option resolution and source-map defaults. Does not change the AST grammar.
languageOption<Language>AST language, then fileThe grammar recorded in the resolved step options and visible to plugins. Builtin generation uses the AST grammar.
outputOption<OutputOptions>fileOutput code settings. output.file derives the default source-map URL.
source_mapOption<SourceMapOptions>offThe source-map inputs and outputs requested for the step.

language is resolved into the step options: an explicit value wins, otherwise the AST grammar, then file. The explicit value is visible to the options hook, and the resolved value is visible to start and later hooks through the resolved options and PluginContext.module. Builtin code generation does not use it: it emits from the AST's own Program.source_type. Module kind remains owned by Program.source_type: there is no GenerateOptions.source_type.

Returns

Result<CodeResult, CompileError>, where CodeResult is { code: String, map: Option<SourceMap> }.

Example

use telarel::{generate, parse, GenerateOptions, ParseOptions, AstResult, CodeResult};

let parsed: AstResult = parse(
    String::from("const x: number = 1;"),
    ParseOptions::default(),
    vec![],
).await?;

let result: CodeResult = generate(parsed.ast, GenerateOptions::default(), vec![]).await?;

println!("{}", result.code);

Last updated on

Copyright © 2026, Alpheus

On this page