vb6parse 1.0.0

vb6parse is a library for parsing and analyzing VB6 code, from projects, to controls, to modules, and forms.
Documentation
use image::EncodableLayout;
use vb6parse::files::ModuleFile;
use vb6parse::io::SourceFile;

#[test]
fn artificial_life_module_load() {
    let file_path = "./tests/data/vb6-code/Artificial-life/Declarations.bas";
    let module_file_bytes = std::fs::read(file_path).unwrap();

    let module_source_file =
        match SourceFile::decode_with_replacement(file_path, module_file_bytes.as_bytes()) {
            Ok(source_file) => source_file,
            Err(e) => {
                e.print();
                panic!("failed to decode module '{file_path}'.");
            }
        };

    let result = ModuleFile::parse(&module_source_file);

    let (module_file_opt, failures) = result.unpack();
    let Some(module_file) = module_file_opt else {
        for failure in &failures {
            failure.eprint();
        }
        panic!("Failed to parse '{file_path}' module file");
    };

    let mut settings = insta::Settings::clone_current();
    settings.set_snapshot_path("../snapshots/tests/module");
    settings.set_prepend_module_to_snapshot(false);
    let _guard = settings.bind_to_scope();
    insta::assert_yaml_snapshot!(module_file);
}

#[test]
fn game_physics_basic_module_load() {
    let file_path = "./tests/data/vb6-code/Game-physics-basic/Physics_Logic.bas";

    let module_file_bytes = std::fs::read(file_path).unwrap();

    let module_source_file =
        match SourceFile::decode_with_replacement(file_path, module_file_bytes.as_bytes()) {
            Ok(source_file) => source_file,
            Err(e) => {
                e.print();
                panic!("failed to decode module '{file_path}'.");
            }
        };

    let result = ModuleFile::parse(&module_source_file);

    let (module_file_opt, failures) = result.unpack();
    let Some(module_file) = module_file_opt else {
        for failure in &failures {
            failure.eprint();
        }
        panic!("Failed to parse '{file_path}' module file");
    };

    let mut settings = insta::Settings::clone_current();
    settings.set_snapshot_path("../snapshots/tests/module");
    settings.set_prepend_module_to_snapshot(false);
    let _guard = settings.bind_to_scope();
    insta::assert_yaml_snapshot!(module_file);
}

#[test]
fn histograms_advanced_module_load() {
    let file_path = "./tests/data/vb6-code/Histograms-advanced/mod_PublicVars.bas";
    let module_file_bytes = std::fs::read(file_path).unwrap();

    let module_source_file =
        match SourceFile::decode_with_replacement(file_path, module_file_bytes.as_bytes()) {
            Ok(source_file) => source_file,
            Err(e) => {
                e.print();
                panic!("failed to decode module '{file_path}'.");
            }
        };

    let result = ModuleFile::parse(&module_source_file);

    let (module_file_opt, failures) = result.unpack();
    let Some(module_file) = module_file_opt else {
        for failure in &failures {
            failure.eprint();
        }
        panic!("Failed to parse '{file_path}' module file");
    };

    let mut settings = insta::Settings::clone_current();
    settings.set_snapshot_path("../snapshots/tests/module");
    settings.set_prepend_module_to_snapshot(false);
    let _guard = settings.bind_to_scope();
    insta::assert_yaml_snapshot!(module_file);
}

#[test]
fn histograms_basic_module_load() {
    let file_path = "./tests/data/vb6-code/Histograms-basic/mod_PublicVars.bas";
    let module_file_bytes = std::fs::read(file_path).unwrap();

    let module_source_file =
        match SourceFile::decode_with_replacement(file_path, module_file_bytes.as_bytes()) {
            Ok(source_file) => source_file,
            Err(e) => {
                e.print();
                panic!("failed to decode module '{file_path}'.");
            }
        };

    let result = ModuleFile::parse(&module_source_file);

    let (module_file_opt, failures) = result.unpack();
    let Some(module_file) = module_file_opt else {
        for failure in &failures {
            failure.eprint();
        }
        panic!("Failed to parse '{file_path}' module file");
    };

    let mut settings = insta::Settings::clone_current();
    settings.set_snapshot_path("../snapshots/tests/module");
    settings.set_prepend_module_to_snapshot(false);
    let _guard = settings.bind_to_scope();
    insta::assert_yaml_snapshot!(module_file);
}

#[test]
fn levels_effect_module_load() {
    let file_path = "./tests/data/vb6-code/Levels-effect/mod_PublicVars.bas";
    let module_file_bytes = std::fs::read(file_path).unwrap();

    let module_source_file =
        match SourceFile::decode_with_replacement(file_path, module_file_bytes.as_bytes()) {
            Ok(source_file) => source_file,
            Err(e) => {
                e.print();
                panic!("failed to decode module '{file_path}'.");
            }
        };

    let result = ModuleFile::parse(&module_source_file);

    let (module_file_opt, failures) = result.unpack();
    let Some(module_file) = module_file_opt else {
        for failure in &failures {
            failure.eprint();
        }
        panic!("Failed to parse '{file_path}' module file");
    };

    let mut settings = insta::Settings::clone_current();
    settings.set_snapshot_path("../snapshots/tests/module");
    settings.set_prepend_module_to_snapshot(false);
    let _guard = settings.bind_to_scope();
    insta::assert_yaml_snapshot!(module_file);
}

#[test]
fn map_editor_2d_module_load() {
    let subs_file_path = "./tests/data/vb6-code/Map-editor-2D/Subs.bas";

    let subs_module_file_bytes = std::fs::read(subs_file_path).unwrap();

    let subs_module_source_file = match SourceFile::decode_with_replacement(
        subs_file_path,
        subs_module_file_bytes.as_bytes(),
    ) {
        Ok(source_file) => source_file,
        Err(e) => {
            e.print();
            panic!("failed to decode module '{subs_file_path}'.");
        }
    };

    let subs_result = ModuleFile::parse(&subs_module_source_file);

    let (subs_module_file_opt, subs_failures) = subs_result.unpack();
    let Some(subs_module_file) = subs_module_file_opt else {
        for failure in &subs_failures {
            failure.eprint();
        }
        panic!("Failed to parse '{subs_file_path}' module file");
    };

    let declaration_file_path = "./tests/data/vb6-code/Map-editor-2D/Declarations.bas";

    let declaration_module_file_bytes = std::fs::read(declaration_file_path).unwrap();

    let declaration_module_source_file = match SourceFile::decode_with_replacement(
        declaration_file_path,
        declaration_module_file_bytes.as_bytes(),
    ) {
        Ok(source_file) => source_file,
        Err(e) => {
            e.print();
            panic!("failed to decode module '{declaration_file_path}'.");
        }
    };

    let declaration_result = ModuleFile::parse(&declaration_module_source_file);

    let (declaration_module_file_opt, declaration_failures) = declaration_result.unpack();
    let Some(declaration_module_file) = declaration_module_file_opt else {
        for failure in &declaration_failures {
            failure.eprint();
        }
        panic!("Failed to parse '{declaration_file_path}' module file");
    };

    let mut settings = insta::Settings::clone_current();
    settings.set_snapshot_path("../snapshots/tests/module");
    settings.set_prepend_module_to_snapshot(false);
    let _guard = settings.bind_to_scope();
    insta::assert_yaml_snapshot!(subs_module_file);
    insta::assert_yaml_snapshot!(declaration_module_file);
}