Rust API

compile_from_ast

Compile an existing AST through rewrite and generation.

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

Compiles an existing ast through rewrite and generation.

Because the stages run inline, options, start, and end fire exactly once.

Parameters

NameTypeDescription
astAstThe AST to compile.
optionsCompileFromAstOptionsThe compile options.
pluginsVec<SharedPluginable>The plugins to run.

CompileFromAstOptions

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 compiled; 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. Does not change the input AST grammar.
source_typeOption<SourceType>inferredThe module kind recorded in the resolved step options and visible to plugins. Does not change the input AST module kind.
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 run.

The explicit values are visible to the options hook; after resolution the values (explicit or inferred) are visible to start and later hooks through the resolved options and PluginContext.module. When language is omitted, the resolved language falls back to the input AST grammar, then the file extension. compile_from_ast does not reparse the AST; the rewrite stages return the input AST or a plugin replacement, and builtin generation emits from that AST's own Program.source_type.

Returns

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

Example

use telarel::{
    parse,
    compile_from_ast,
    ParseOptions,
    CompileFromAstOptions,
    AstResult,
    CodeResult,
};

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

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

println!("{}", result.code); // const x = 1;

Last updated on

Copyright © 2026, Alpheus

On this page