fn load_path(
    out: &mut StandardStream,
    args: &Args,
    options: &rune::Options,
    path: &Path,
) -> Result<(
    Arc<Unit>,
    runestick::Context,
    Arc<runestick::RuntimeContext>,
    rune::Sources,
    Vec<(runestick::Hash, runestick::CompileMeta)>,
)> {
    let shared = args.shared();
    let context = shared.context()?;

    let bytecode_path = path.with_extension("rnc");

    let source = runestick::Source::from_path(path)
        .with_context(|| format!("reading file: {}", path.display()))?;

    let runtime = Arc::new(context.runtime());
    let mut sources = rune::Sources::new();

    sources.insert(source);

    let use_cache = options.bytecode && should_cache_be_used(path, &bytecode_path)?;

    // TODO: how do we deal with tests discovery for bytecode loading
    let maybe_unit = if use_cache {
        let f = fs::File::open(&bytecode_path)?;

        match bincode::deserialize_from::<_, Unit>(f) {
            Ok(unit) => {
                log::trace!("using cache: {}", bytecode_path.display());
                Some(Arc::new(unit))
            }
            Err(e) => {
                log::error!("failed to deserialize: {}: {}", bytecode_path.display(), e);
                None
            }
        }
    } else {
        None
    };

    let (unit, tests) = match maybe_unit {
        Some(unit) => (unit, Default::default()),
        None => {
            log::trace!("building file: {}", path.display());

            let mut diagnostics = if shared.warnings {
                rune::Diagnostics::new()
            } else {
                rune::Diagnostics::without_warnings()
            };

            let test_finder = Rc::new(tests::TestVisitor::default());
            let source_loader = Rc::new(rune::FileSourceLoader::new());

            let result = rune::load_sources_with_visitor(
                &context,
                options,
                &mut sources,
                &mut diagnostics,
                test_finder.clone(),
                source_loader.clone(),
            );

            diagnostics.emit_diagnostics(out, &sources)?;
            let unit = result?;

            if options.bytecode {
                log::trace!("serializing cache: {}", bytecode_path.display());
                let f = fs::File::create(&bytecode_path)?;
                bincode::serialize_into(f, &unit)?;
            }

            let test_finder = match Rc::try_unwrap(test_finder) {
                Ok(test_finder) => test_finder,
                Err(..) => panic!("test finder should be uniquely held"),
            };

            (Arc::new(unit), test_finder.into_test_functions())
        }
    };

    Ok((unit, context, runtime, sources, tests))
}
/// Load context and code for a given path
Load context and code for a given path
Load context and code for a given path
fn load_path(
    out: &mut StandardStream,
    args: &Args,
    options: &rune::Options,
    path: &Path,
) -> Result<(
    Arc<Unit>,
    runestick::Context,
    Arc<runestick::RuntimeContext>,
    rune::Sources,
    Vec<(runestick::Hash, runestick::CompileMeta)>,
)> {
    let shared = args.shared();
    let context = shared.context()?;
    let bytecode_path = path.with_extension("rnc");
    let source = runestick::Source::from_path(path)
        .with_context(|| format!("reading file: {}", path.display()))?;
    let runtime = Arc::new(context.runtime());
    let mut sources = rune::Sources::new();
    sources.insert(source);
    let use_cache = options.bytecode && should_cache_be_used(path, &bytecode_path)?;
    // TODO: how do we deal with tests discovery for bytecode loading
    let maybe_unit = if use_cache {
        let f = fs::File::open(&bytecode_path)?;
        match bincode::deserialize_from::<_, Unit>(f) {
            Ok(unit) => {
                log::trace!("using cache: {}", bytecode_path.display());
                Some(Arc::new(unit))
            }
            Err(e) => {
                log::error!("failed to deserialize: {}: {}", bytecode_path.display(), e);
                None
            }
        }
    } else {
        None
    };
    let (unit, tests) = match maybe_unit {
        Some(unit) => (unit, Default::default()),
        None => {
            log::trace!("building file: {}", path.display());
            let mut diagnostics = if shared.warnings {
                rune::Diagnostics::new()
            } else {
                rune::Diagnostics::without_warnings()
            };
            let test_finder = Rc::new(tests::TestVisitor::default());
            let source_loader = Rc::new(rune::FileSourceLoader::new());
            let result = rune::load_sources_with_visitor(
                &context,
                options,
                &mut sources,
                &mut diagnostics,
                test_finder.clone(),
                source_loader.clone(),
            );
            diagnostics.emit_diagnostics(out, &sources)?;
            let unit = result?;
            if options.bytecode {
                log::trace!("serializing cache: {}", bytecode_path.display());
                let f = fs::File::create(&bytecode_path)?;
                bincode::serialize_into(f, &unit)?;
            }
            let test_finder = match Rc::try_unwrap(test_finder) {
                Ok(test_finder) => test_finder,
                Err(..) => panic!("test finder should be uniquely held"),
            };
            (Arc::new(unit), test_finder.into_test_functions())
        }
    };
    Ok((unit, context, runtime, sources, tests))
}
fn load_path ( out : & mut StandardStream , args : & Args , options : & rune :: Options , path : & Path , ) -> Result < ( Arc < Unit > , runestick :: Context , Arc < runestick :: RuntimeContext > , rune :: Sources , Vec < ( runestick :: Hash , runestick :: CompileMeta ) > , ) > { let shared = args . shared ( ) ; let context = shared . context ( ) ? ; let bytecode_path = path . with_extension ( " " ) ; let source = runestick :: Source :: from_path ( path ) . with_context ( | | format ! ( " " path display ( ) ) ) ? ; let runtime = Arc :: new ( context . runtime ( ) ) ; let mut sources = rune :: Sources :: new ( ) ; sources . insert ( source ) ; let use_cache = options . bytecode && should_cache_be_used ( path , & bytecode_path ) ? ; // TODO: how do we deal with tests discovery for bytecode loading let maybe_unit = if use_cache { let f = fs :: File :: open ( & bytecode_path ) ? ; match bincode :: deserialize_from :: < _ , Unit > ( f ) { Ok ( unit ) => { log :: trace ! ( " " bytecode_path display ( ) ) ; Some ( Arc :: new ( unit ) ) } Err ( e ) => { log :: error ! ( " " bytecode_path display ( ) e ) ; None } } } else { None } ; let ( unit , tests ) = match maybe_unit { Some ( unit ) => ( unit , Default :: default ( ) ) , None => { log :: trace ! ( " " path display ( ) ) ; let mut diagnostics = if shared . warnings { rune :: Diagnostics :: new ( ) } else { rune :: Diagnostics :: without_warnings ( ) } ; let test_finder = Rc :: new ( tests :: TestVisitor :: default ( ) ) ; let source_loader = Rc :: new ( rune :: FileSourceLoader :: new ( ) ) ; let result = rune :: load_sources_with_visitor ( & context , options , & mut sources , & mut diagnostics , test_finder . clone ( ) , source_loader . clone ( ) , ) ; diagnostics . emit_diagnostics ( out , & sources ) ? ; let unit = result ? ; if options . bytecode { log :: trace ! ( " " bytecode_path display ( ) ) ; let f = fs :: File :: create ( & bytecode_path ) ? ; bincode :: serialize_into ( f , & unit ) ? ; } let test_finder = match Rc :: try_unwrap ( test_finder ) { Ok ( test_finder ) => test_finder , Err ( .. ) => panic ! ( " " ) , } ; ( Arc :: new ( unit ) , test_finder . into_test_functions ( ) ) } } ; Ok ( ( unit , context , runtime , sources , tests ) ) }
Load context and code for a given path
Load context and code for a given path
out
&mut StandardStream
args
&Args
options
&rune::Options
path
&Path
out
&mut StandardStream
args
&Args
options
&rune::Options
path
&Path