bq27441 0.1.0

Blocking and async driver for the BQ27441 battery fuel gauge with I2C support
Documentation
#![doc = "Build script that generates the BQ27441 register API from the YAML manifest."]

use std::path::PathBuf;
use std::str::FromStr;
use std::{env, fs, io};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    println!("cargo:rerun-if-changed=build.rs");
    println!("cargo:rerun-if-changed=src/bq27441.yaml");

    let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR must be set"));
    let manifest_path = manifest_dir.join("src/bq27441.yaml");
    let manifest = fs::read_to_string(&manifest_path)?;

    let generated = device_driver_generation::transform_yaml(&manifest, "Bq27441Device");

    let tokens = proc_macro2::TokenStream::from_str(&generated)
        .map_err(|err| io::Error::other(format!("Failed to parse generated device tokens: {err}")))?;

    let syntax_tree = syn::parse2::<syn::File>(tokens)
        .map_err(|err| io::Error::other(format!("Failed to build syntax tree for generated device: {err}")))?;

    let formatted = prettyplease::unparse(&syntax_tree);

    let mut output = String::new();
    output.push_str("// @generated by build.rs\n");
    output.push_str("// Do not edit this file manually.\n");
    output.push_str("// Source manifest: ");
    output.push_str(&manifest_path.display().to_string());
    output.push_str("\n\n");
    output.push_str(&formatted);

    let out_dir = PathBuf::from(env::var("OUT_DIR").expect("OUT_DIR must be set"));
    fs::write(out_dir.join("bq27441_device.rs"), output)?;

    Ok(())
}