use std::env;
use std::fs;
use std::path::PathBuf;
fn main() {
let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
let cargo_toml_path = PathBuf::from(manifest_dir).join("Cargo.toml");
let content = fs::read_to_string(&cargo_toml_path).expect("Failed to read Cargo.toml");
let toml_value: toml::Value = toml::from_str(&content).expect("Failed to parse Cargo.toml");
let libs_version = extract_endpoint_libs_version(&toml_value)
.expect("Failed to find endpoint-libs in Cargo.toml dependencies");
let libs_requirement = format!("^{}", libs_version);
println!(
"cargo:rustc-env=ENDPOINT_LIBS_REQUIREMENT={}",
libs_requirement
);
}
fn extract_endpoint_libs_version(toml: &toml::Value) -> Option<String> {
let deps = toml.get("dependencies")?;
let endpoint_libs = deps.get("endpoint-libs")?;
if let Some(version_str) = endpoint_libs.as_str() {
return Some(version_str.to_string());
}
if let Some(table) = endpoint_libs.as_table() {
if let Some(version) = table.get("version").and_then(|v| v.as_str()) {
return Some(version.to_string());
}
if table.contains_key("git") {
eprintln!("Warning: endpoint-libs is a git dependency without explicit version.");
eprintln!("Using placeholder version '0.0.0'. Update build.rs to fetch from git.");
return Some("0.0.0".to_string());
}
}
None
}