use assembly::{ast::ModuleAst, Library, LibraryNamespace, MaslLibrary, Version};
use std::{collections::BTreeMap, env, fs, io, path::Path};
mod md_renderer;
use md_renderer::MarkdownRenderer;
const ASM_DIR_PATH: &str = "./asm";
const ASL_DIR_PATH: &str = "./assets";
const DOC_DIR_PATH: &str = "./docs";
type ModuleMap = BTreeMap<String, ModuleAst>;
#[cfg(not(feature = "docs-rs"))]
fn main() -> io::Result<()> {
            println!("cargo:rerun-if-changed=asm");
    println!("cargo:rerun-if-changed=../assembly/src");
    let namespace = LibraryNamespace::try_from("std".to_string()).expect("invalid base namespace");
    let version = Version::try_from(env!("CARGO_PKG_VERSION")).expect("invalid cargo version");
    let locations = true;     let stdlib = MaslLibrary::read_from_dir(ASM_DIR_PATH, namespace, locations, version)?;
    let docs = stdlib
        .modules()
        .map(|module| (module.path.to_string(), module.ast.clone()))
        .collect();
        let build_dir = env::var("OUT_DIR").unwrap();
    stdlib.write_to_dir(Path::new(&build_dir).join(ASL_DIR_PATH))?;
        build_stdlib_docs(&docs, DOC_DIR_PATH)?;
    Ok(())
}
trait Renderer {
        fn render(stdlib: &ModuleMap, output_dir: &str);
}
pub fn build_stdlib_docs(module_map: &ModuleMap, output_dir: &str) -> io::Result<()> {
            for entry in fs::read_dir(output_dir)? {
        let entry = entry?;
        let metadata = entry.metadata()?;
        if metadata.is_dir() {
            fs::remove_dir_all(entry.path())?;
        } else {
            assert!(metadata.is_file());
            fs::remove_file(entry.path())?;
        }
    }
            MarkdownRenderer::render(module_map, output_dir);
    Ok(())
}