use std::{
env,
fs,
io::Write,
path::PathBuf,
};
use toml::Value;
fn main() {
println!("cargo:rerun-if-changed=Cargo.toml");
let manifest_dir =
PathBuf::from(env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR not set"));
let manifest_path = manifest_dir.join("Cargo.toml");
let content =
fs::read_to_string(&manifest_path).expect("Unable to read Cargo.toml");
let parsed_toml: Value =
content.parse().expect("Failed to parse Cargo.toml as TOML");
let version = parsed_toml
.get("package")
.and_then(|pkg| pkg.get("version"))
.and_then(Value::as_str)
.unwrap_or("0.0.0");
let out_path = manifest_dir.join("src").join("version.rs");
let mut file = fs::File::create(&out_path)
.expect("Unable to create src/version.rs");
writeln!(
file,
"#[allow(dead_code)]\npub const VERSION: &str = \"{}\";",
version
)
.expect("Unable to write version.rs");
}