Use template to create config.rs.

This commit is contained in:
Stephen Chung 2022-10-15 12:04:14 +08:00
parent 5035dcbf47
commit 9b226f321e
3 changed files with 22 additions and 15 deletions

View File

@ -1,26 +1,26 @@
use std::{env, fs::File, io::Write};
const WRITE_ERROR: &str = "cannot write to `config.rs`";
use std::{
env,
fs::File,
io::{Read, Write},
};
fn main() {
// Tell Cargo that if the given environment variable changes, to rerun this build script.
println!("cargo:rerun-if-env-changed=RHAI_AHASH_SEED");
let mut f = File::create("src/config.rs").expect("cannot create `config.rs`");
let mut contents = String::new();
f.write_fmt(format_args!(
"//! Configuration settings for this Rhai build
"
))
.expect(WRITE_ERROR);
File::open("build.template")
.expect("cannot open `build.template`")
.read_to_string(&mut contents)
.expect("cannot read from `build.template`");
let seed = env::var("RHAI_AHASH_SEED").map_or_else(|_| "None".into(), |s| format!("Some({s})"));
f.write_fmt(format_args!(
"pub const AHASH_SEED: Option<[u64; 4]> = {seed};\n"
))
.expect(WRITE_ERROR);
contents = contents.replace("{{AHASH_SEED}}", &seed);
f.flush().expect("cannot flush `config.rs`");
File::create("src/config.rs")
.expect("cannot create `config.rs`")
.write(contents.as_bytes())
.expect("cannot write to `config.rs`");
}

5
build.template Normal file
View File

@ -0,0 +1,5 @@
//! Configuration settings for this Rhai build
/// Fixed hashing seeds for stable hashing.
/// Set to [`None`] to disable stable hashing.
pub const AHASH_SEED: Option<[u64; 4]> = {{AHASH_SEED}};

View File

@ -1,3 +1,5 @@
//! Configuration settings for this Rhai build
/// Fixed hashing seeds for stable hashing.
/// Set to [`None`] to disable stable hashing.
pub const AHASH_SEED: Option<[u64; 4]> = None;