Skip to content

Commit 5e0f293

Browse files
committed
Add and use DEFAULT_ROUNDING_MODE compile-time configuration
1 parent 78a11ef commit 5e0f293

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

build.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
#![allow(clippy::style)]
21

32
use std::env;
4-
use std::path::PathBuf;
3+
use std::path::{Path, PathBuf};
4+
55

66
fn main() {
77
let ac = autocfg::new();
@@ -15,11 +15,12 @@ fn main() {
1515

1616
let outdir: PathBuf = std::env::var_os("OUT_DIR").unwrap().into();
1717
write_default_precision_file(&outdir);
18+
write_default_rounding_mode(&outdir);
1819
}
1920

2021

2122
/// Create default_precision.rs, containg definition of constant DEFAULT_PRECISION loaded in src/lib.rs
22-
fn write_default_precision_file(outdir: &PathBuf) {
23+
fn write_default_precision_file(outdir: &Path) {
2324
let env_var = env::var("RUST_BIGDECIMAL_DEFAULT_PRECISION").unwrap_or_else(|_| "100".to_owned());
2425
println!("cargo:rerun-if-env-changed=RUST_BIGDECIMAL_DEFAULT_PRECISION");
2526

@@ -34,3 +35,14 @@ fn write_default_precision_file(outdir: &PathBuf) {
3435

3536
std::fs::write(rust_file_path, rust_file_contents).unwrap();
3637
}
38+
39+
/// Create default_rounding_mode.rs, using value of RUST_BIGDECIMAL_DEFAULT_ROUNDING_MODE environment variable
40+
fn write_default_rounding_mode(outdir: &Path) {
41+
let rounding_mode_name = env::var("RUST_BIGDECIMAL_DEFAULT_ROUNDING_MODE").unwrap_or_else(|_| "HalfEven".to_owned());
42+
println!("cargo:rerun-if-env-changed=RUST_BIGDECIMAL_DEFAULT_ROUNDING_MODE");
43+
44+
let rust_file_path = outdir.join("default_rounding_mode.rs");
45+
let rust_file_contents = format!("const DEFAULT_ROUNDING_MODE: RoundingMode = RoundingMode::{};", rounding_mode_name);
46+
47+
std::fs::write(rust_file_path, rust_file_contents).unwrap();
48+
}

src/context.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ use crate::{
1515

1616
// const DEFAULT_PRECISION: u64 = ${RUST_BIGDECIMAL_DEFAULT_PRECISION} or 100;
1717
include!(concat!(env!("OUT_DIR"), "/default_precision.rs"));
18+
// const DEFAULT_ROUNDING_MODE: RoundingMode = ${RUST_BIGDECIMAL_DEFAULT_ROUNDING_MODE} or HalfUp;
19+
include!(concat!(env!("OUT_DIR"), "/default_rounding_mode.rs"));
1820

1921
/// Mathematical Context
2022
///
@@ -111,7 +113,7 @@ impl stdlib::default::Default for Context {
111113
fn default() -> Self {
112114
Self {
113115
precision: NonZeroU64::new(DEFAULT_PRECISION).unwrap(),
114-
rounding: RoundingMode::HalfEven,
116+
rounding: DEFAULT_ROUNDING_MODE,
115117
}
116118
}
117119
}

0 commit comments

Comments
 (0)