[][src]Function rune::load_source

pub fn load_source(
    context: &Context,
    options: &Options,
    code_source: Source,
    warnings: &mut Warnings
) -> Result<Unit, LoadError>

Load and compile the given source.

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

Examples

Note: these must be built with the diagnostics feature enabled to give access to rune::termcolor.

use rune::termcolor::{ColorChoice, StandardStream};
use rune::EmitDiagnostics as _;
use runestick::Source;

use std::path::Path;
use std::sync::Arc;
use std::error::Error;

let context = Arc::new(rune::default_context()?);
let mut options = rune::Options::default();
let mut warnings = rune::Warnings::new();

let source = Source::new("entry", r#"
fn main() {
    println("Hello World");
}
"#);

let unit = match rune::load_source(&*context, &options, source, &mut warnings) {
    Ok(unit) => unit,
    Err(error) => {
        let mut writer = StandardStream::stderr(ColorChoice::Always);
        error.emit_diagnostics(&mut writer)?;
        return Ok(());
    }
};

let unit = Arc::new(unit);
let vm = runestick::Vm::new(context.clone(), unit.clone());

if !warnings.is_empty() {
    let mut writer = StandardStream::stderr(ColorChoice::Always);
    rune::emit_warning_diagnostics(&mut writer, &warnings, &*unit)?;
}