Documentation
use crate::info::*;
use quote::{format_ident, quote};
use rust_embed::RustEmbed;
use serde::Deserialize;
use std::io::Write;

fn periph_to_context(periph: &Peripheral) -> tera::Context {
    let mut context = tera::Context::new();
    context.insert("peripheral", periph);
    context
}

#[derive(RustEmbed)]
#[folder = "$CARGO_MANIFEST_DIR/src/templates/cpp/"]
struct Templates;

fn generate_periph(periph: &Peripheral) -> String {
    let mut tera = tera::Tera::default();
    tera.add_raw_template(
        "periph_header.hpp",
        std::str::from_utf8(&Templates::get("periph_header.hpp.tera").unwrap()).unwrap(),
    )
    .unwrap();
    tera.render("periph_header.hpp", &periph_to_context(periph))
        .unwrap()
}

#[derive(Debug, Deserialize)]
pub struct CppConfig {
    path: String,
}

pub fn generate_cpp(config: &CppConfig, periph: &Peripheral) {
    //std::fs::create_dir(&config.path).unwrap();
    //std::fs::create_dir(&format!("{}/src", config.path)).unwrap();
    let periph = generate_periph(periph);

    let mut file = std::fs::File::create(&format!("periph_header.hpp")).unwrap();
    file.write_all(periph.as_bytes()).unwrap();

    /*let mut file = std::fs::File::create(&format!("{}/Cargo.toml", config.path)).unwrap();
    let cargo_toml = generate_cargo();
    file.write_all(cargo_toml.as_bytes()).unwrap();

    let mut file = std::fs::File::create(&format!("{}/src/lib.rs", config.path)).unwrap();
    let lib_code = generate_periph(periph);
    file.write_all(lib_code.as_bytes()).unwrap();*/
}