rune

Function prepare

Source
pub fn prepare(sources: &mut Sources) -> Build<'_, DefaultStorage>
Expand description

Entry point to building a collection Sources of Rune into a default executable Unit.

This returns a Build instance using a default configuration for a build that can be customized.

By default, if any error is encountered during compilation the error type BuildError doesn’t provide any diagnostics on what went wrong. To get rich diagnostics you should instead associated a Diagnostics type through Build::with_diagnostics and examine it before handling any [Err(BuildError)] produced.

Uses the Source::name when generating diagnostics to reference the file.

§Examples

Note: these must be built with the emit feature enabled (default) to give access to rune::termcolor.

use rune::termcolor::{ColorChoice, StandardStream};
use rune::{Context, Source, Vm};
use std::sync::Arc;

let context = Context::with_default_modules()?;
let runtime = Arc::new(context.runtime()?);

let mut sources = rune::Sources::new();

sources.insert(Source::memory(r#"
pub fn main() {
    println!("Hello World");
}
"#)?)?;

let mut diagnostics = rune::Diagnostics::new();

let result = rune::prepare(&mut sources)
    .with_context(&context)
    .with_diagnostics(&mut diagnostics)
    .build();

if !diagnostics.is_empty() {
    let mut writer = StandardStream::stderr(ColorChoice::Always);
    diagnostics.emit(&mut writer, &sources)?;
}

let unit = result?;
let unit = Arc::new(unit);
let vm = Vm::new(runtime, unit);