use semver::{Version, VersionReq};
use serde::Deserialize;
use std::{env, fs, path::Path};
#[derive(Deserialize)]
pub(crate) struct Config {
pub(crate) default: Option<String>,
pub(crate) version: Option<String>,
}
pub(crate) fn config() -> Config {
let cargo_manifest_dir = env::var("CARGO_MANIFEST_DIR")
.expect("environment variable `CARGO_MANIFEST_DIR` must be set");
let cfg_path = Path::new(&cargo_manifest_dir).join("hsbindgen.toml");
let cfg = fs::read_to_string(cfg_path).expect(
"fail to read content of `hsbindgen.toml` configuration file
n.b. you have to run the command `cargo-cabal` to generate it",
);
let cfg = toml::from_str(&cfg).expect("fail to parse TOML content of `hsbindgen.toml` file");
check_version(&cfg);
cfg
}
fn check_version(config: &Config) {
let req = VersionReq::parse("<=0.8").unwrap();
let version = config
.version
.as_ref()
.expect("a version field is required in `hsbindgen.toml`");
let version = Version::parse(version)
.expect("version field of `hsbindgen.toml` does not follow SemVer format");
assert!(
req.matches(&version),
"incompatible versions of `cargo-cabal`/`hs-bindgen` used, please update"
);
}