provekit_nargo_cli 1.0.0-beta.20-alpha.1

Noir's package manager
Documentation
// This module is treated as an entrypoint as so any functions are considered dead code,
// despite being used in other integration tests.
#![allow(dead_code)]

use std::path::Path;

use nargo::parse_all;
use noirc_artifacts::program::CompiledProgram;
use noirc_driver::{
    CompilationResult, CompileOptions, CrateId, compile_main, file_manager_with_stdlib,
    prepare_crate,
};
use noirc_frontend::hir::Context;

/// Prepare a code snippet.
fn prepare_snippet(source: String) -> (Context<'static, 'static>, CrateId) {
    let root = Path::new("");
    let file_name = Path::new("main.nr");
    let mut file_manager = file_manager_with_stdlib(root);
    file_manager.add_file_with_source(file_name, source).expect(
        "Adding source buffer to file manager should never fail when file manager is empty",
    );
    let parsed_files = parse_all(&file_manager);

    let mut context = Context::new(file_manager, parsed_files);
    let root_crate_id = prepare_crate(&mut context, file_name);

    (context, root_crate_id)
}

/// Compile the main function in a code snippet.
///
/// Use `force_brillig` to test it as an unconstrained function without having to change the code.
/// This is useful for methods that use the `runtime::is_unconstrained()` method to change their behavior.
pub(crate) fn prepare_and_compile_snippet(
    source: String,
    force_brillig: bool,
) -> CompilationResult<CompiledProgram> {
    let (mut context, root_crate_id) = prepare_snippet(source);
    let options = CompileOptions { force_brillig, ..Default::default() };
    compile_main(&mut context, root_crate_id, &options, None)
}