-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
2,987 additions
and
1,408 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,48 @@ | ||
#![allow(clippy::style)] | ||
|
||
extern crate autocfg; | ||
|
||
use std::env; | ||
use std::path::PathBuf; | ||
use std::path::{Path, PathBuf}; | ||
|
||
|
||
fn main() -> std::io::Result<()> { | ||
fn main() { | ||
let ac = autocfg::new(); | ||
ac.emit_rustc_version(1, 70); | ||
|
||
let outdir = match std::env::var_os("OUT_DIR") { | ||
None => return Ok(()), | ||
Some(outdir) => outdir, | ||
}; | ||
let outdir_path = PathBuf::from(outdir); | ||
// Option::zip | ||
ac.emit_rustc_version(1, 46); | ||
|
||
// Remove this comment if enabled proptests | ||
// ::PROPERTY-TESTS:: autocfg::emit("property_tests"); | ||
|
||
write_default_precision(&outdir_path, "default_precision.rs")?; | ||
Ok(()) | ||
let outdir: PathBuf = std::env::var_os("OUT_DIR").unwrap().into(); | ||
write_default_precision_file(&outdir); | ||
write_default_rounding_mode(&outdir); | ||
} | ||
|
||
/// Create default_precision.rs, containg definition of constant DEFAULT_PRECISION | ||
fn write_default_precision(outdir_path: &PathBuf, filename: &str) -> std::io::Result<()> | ||
{ | ||
|
||
let default_prec = env::var("RUST_BIGDECIMAL_DEFAULT_PRECISION") | ||
.map(|s| s.parse::<std::num::NonZeroU32>().expect("$RUST_BIGDECIMAL_DEFAULT_PRECISION must be an integer > 0")) | ||
.map(|nz_num| nz_num.into()) | ||
.unwrap_or(100u32); | ||
/// Create default_precision.rs, containing definition of constant DEFAULT_PRECISION loaded in src/lib.rs | ||
fn write_default_precision_file(outdir: &Path) { | ||
let env_var = env::var("RUST_BIGDECIMAL_DEFAULT_PRECISION").unwrap_or_else(|_| "100".to_owned()); | ||
println!("cargo:rerun-if-env-changed=RUST_BIGDECIMAL_DEFAULT_PRECISION"); | ||
|
||
let rust_file_path = outdir.join("default_precision.rs"); | ||
|
||
let default_precision_rs_path = outdir_path.join(filename); | ||
let default_prec: u32 = env_var | ||
.parse::<std::num::NonZeroU32>() | ||
.expect("$RUST_BIGDECIMAL_DEFAULT_PRECISION must be an integer > 0") | ||
.into(); | ||
|
||
let default_precision = format!("const DEFAULT_PRECISION: u64 = {};", default_prec); | ||
let rust_file_contents = format!("const DEFAULT_PRECISION: u64 = {};", default_prec); | ||
|
||
std::fs::write(rust_file_path, rust_file_contents).unwrap(); | ||
} | ||
|
||
// Rewriting the file if it already exists with the same contents | ||
// would force a rebuild. | ||
match std::fs::read_to_string(&default_precision_rs_path) { | ||
Ok(existing_contents) if existing_contents == default_precision => {}, | ||
_ => { | ||
std::fs::write(&default_precision_rs_path, default_precision) | ||
.expect("Could not write big decimal default-precision file"); | ||
} | ||
}; | ||
/// Create default_rounding_mode.rs, using value of RUST_BIGDECIMAL_DEFAULT_ROUNDING_MODE environment variable | ||
fn write_default_rounding_mode(outdir: &Path) { | ||
let rounding_mode_name = env::var("RUST_BIGDECIMAL_DEFAULT_ROUNDING_MODE").unwrap_or_else(|_| "HalfEven".to_owned()); | ||
println!("cargo:rerun-if-env-changed=RUST_BIGDECIMAL_DEFAULT_ROUNDING_MODE"); | ||
|
||
println!("cargo:rerun-if-changed={}", default_precision_rs_path.display()); | ||
println!("cargo:rerun-if-env-changed={}", "RUST_BIGDECIMAL_DEFAULT_PRECISION"); | ||
let rust_file_path = outdir.join("default_rounding_mode.rs"); | ||
let rust_file_contents = format!("const DEFAULT_ROUNDING_MODE: RoundingMode = RoundingMode::{};", rounding_mode_name); | ||
|
||
Ok(()) | ||
std::fs::write(rust_file_path, rust_file_contents).unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.