pub(crate) fn build_stamp() -> String {
let built = std::env::current_exe()
.ok()
.and_then(|p| std::fs::metadata(p).ok())
.and_then(|m| m.modified().ok())
.and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
.map(|d| {
let secs = d.as_secs();
let (days, rem) = (secs / 86_400, secs % 86_400);
let (h, m, s) = (rem / 3600, (rem % 3600) / 60, rem % 60);
let (y, mo, d) = civil_from_days(days as i64);
format!("{y:04}-{mo:02}-{d:02} {h:02}:{m:02}:{s:02}Z")
})
.unwrap_or_else(|| "unknown".into());
format!(
"gras {} [{}] built {built}",
env!("CARGO_PKG_VERSION"),
if cfg!(debug_assertions) {
"debug"
} else {
"release"
}
)
}
fn civil_from_days(z: i64) -> (i64, u32, u32) {
let z = z + 719_468;
let era = if z >= 0 { z } else { z - 146_096 } / 146_097;
let doe = (z - era * 146_097) as u64;
let yoe = (doe - doe / 1460 + doe / 36_524 - doe / 146_096) / 365;
let y = yoe as i64 + era * 400;
let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
let mp = (5 * doy + 2) / 153;
let d = (doy - (153 * mp + 2) / 5 + 1) as u32;
let m = if mp < 10 { mp + 3 } else { mp - 9 } as u32;
(if m <= 2 { y + 1 } else { y }, m, d)
}
const SCI_AT: f32 = 1.0e9;
const BAND_WIDE: f32 = 10.0;
const BAND_SMALL: f32 = 1.0;
fn fmt_span(v: f32, min: usize, max: usize) -> String {
if v.is_finite() && v.abs() >= SCI_AT {
return format!("{v:.3e}");
}
let mut s = format!("{v:.min$}");
let mut p = min;
while p < max && v != 0.0 && s.parse::<f32>() == Ok(0.0) {
p += 1;
s = format!("{v:.p$}");
}
s
}
fn fmt_band(v: f32) -> String {
if v.is_finite() && v.abs() >= SCI_AT {
return format!("{v:.3e}");
}
let a = v.abs();
if a >= BAND_WIDE {
fmt_span(v, 1, 2)
} else if a >= BAND_SMALL {
fmt_span(v, 2, 4)
} else {
fmt_span(v, 4, 6)
}
}
pub(crate) fn fmt_line(v: f32) -> String {
fmt_band(v)
}
pub(crate) fn fmt_cell(v: f32) -> String {
fmt_band(v)
}
pub(crate) fn fmt_units(n: i64) -> String {
const UNITS: [&str; 3] = ["K", "M", "B"];
let mut value = n as f64;
let mut idx = 0usize;
while value.abs() >= 1000.0 && idx < UNITS.len() {
value /= 1000.0;
idx += 1;
}
if idx == 0 {
return n.to_string();
}
if (value * 10.0).round().abs() >= 10_000.0 && idx < UNITS.len() {
value /= 1000.0;
idx += 1;
}
format!("{value:.1}{}", UNITS[idx - 1])
}
pub(crate) fn fmt_cfg(v: f32) -> String {
format!("{v:.2}")
}
pub(crate) fn fmt_line_opt(v: &Option<f32>) -> String {
v.as_ref()
.map_or_else(|| crate::utils::cfy::NO_VALUE.into(), |x| fmt_line(*x))
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ReadingStyle {
Line,
Cell,
Doc,
}
fn scalar(style: ReadingStyle) -> fn(f32) -> String {
match style {
ReadingStyle::Line => fmt_line,
ReadingStyle::Cell => fmt_cell,
ReadingStyle::Doc => fmt_cfg,
}
}
pub fn fmt_reading(mean: Option<f32>, sigma: Option<f32>, n: usize, style: ReadingStyle) -> String {
let f = scalar(style);
let Some(mean) = mean else {
return crate::utils::cfy::NO_VALUE.to_string();
};
match sigma {
Some(s) if n > 0 => format!(
"avg {} (spread {}; SE {} = spread/√{n}, n={n})",
f(mean),
f(s),
f(s / (n as f32).sqrt())
),
_ => format!("avg {} (n={n}, no spread yet)", f(mean)),
}
}
pub(crate) fn fmt_family_reading(
centre: Option<f32>,
spread: Option<f32>,
n: usize,
style: ReadingStyle,
family: crate::engine::config::MeanType,
) -> String {
use crate::engine::config::MeanType;
if family == MeanType::Arithmetic {
return fmt_reading(centre, spread, n, style);
}
let f = scalar(style);
let Some(centre) = centre else {
return crate::utils::cfy::NO_VALUE.to_string();
};
let (centre_noun, spread_noun) = match family {
MeanType::Arithmetic => unreachable!("delegated above"),
MeanType::Median => ("window median", "1.4826·MAD"),
MeanType::SignedLog => ("window g-avg", "g-spread"),
};
match spread {
Some(s) if n > 0 => format!(
"{centre_noun} {} ({spread_noun} {}, n={n})",
f(centre),
f(s)
),
_ => format!("{centre_noun} {} (n={n}, no spread yet)", f(centre)),
}
}
pub fn fmt_census(mean: f32, sigma: f32, style: ReadingStyle) -> String {
let f = scalar(style);
format!("avg {} (spread {})", f(mean), f(sigma))
}
pub(crate) fn csv_field(s: &str) -> String {
if s.contains(',') || s.contains('"') || s.contains('\n') || s.contains('\r') {
format!("\"{}\"", s.replace('"', "\"\""))
} else {
s.to_string()
}
}
pub(crate) fn csv_split_line(line: &str) -> Vec<&str> {
let mut out = Vec::new();
let bytes = line.as_bytes();
let mut start = 0usize;
let mut in_quotes = false;
for (i, &b) in bytes.iter().enumerate() {
match b {
b'"' => in_quotes = !in_quotes,
b',' if !in_quotes => {
out.push(&line[start..i]);
start = i + 1;
}
_ => {}
}
}
out.push(&line[start..]);
out
}
#[cfg(test)]
mod tests {
use super::{
ReadingStyle, csv_field, fmt_cell, fmt_census, fmt_cfg, fmt_line, fmt_line_opt,
fmt_reading, fmt_units,
};
use crate::engine::format::fmt_family_reading;
#[test]
fn band_above_ten_is_one_decimal() {
assert_eq!(fmt_line(117.55), "117.6");
assert_eq!(fmt_line(97.1), "97.1");
assert_eq!(fmt_line(20.5), "20.5");
assert_eq!(fmt_line(214.6), "214.6");
assert_eq!(fmt_line(-42.25), "-42.2");
assert_eq!(fmt_line(999_999.0), "999999.0");
assert_eq!(fmt_line(10.0), "10.0");
}
#[test]
fn band_one_up_to_ten_is_two_decimals() {
assert_eq!(fmt_line(1.0), "1.00");
assert_eq!(fmt_line(3.25), "3.25");
assert_eq!(fmt_line(9.99), "9.99");
assert_eq!(fmt_line(-5.5), "-5.50");
}
#[test]
fn band_below_one_is_four_decimals() {
assert_eq!(fmt_line(0.6348), "0.6348");
assert_eq!(fmt_line(0.02), "0.0200");
assert_eq!(fmt_line(-0.02), "-0.0200");
assert_eq!(fmt_line(0.5), "0.5000");
}
#[test]
fn band_floor_is_capped_and_true_zero_is_not_widened() {
assert_eq!(fmt_line(0.0), "0.0000");
assert_eq!(fmt_line(-0.0), "-0.0000");
assert_eq!(fmt_line(0.000_04), "0.00004");
assert_eq!(fmt_line(1e-8), "0.000000");
}
#[test]
fn band_uses_scientific_notation_only_at_scale() {
assert_eq!(fmt_line(1e9), "1.000e9");
assert_eq!(fmt_line(1.5e38), "1.500e38");
assert_eq!(fmt_line(-1.5e38), "-1.500e38");
assert_eq!(fmt_line(990_000_000.0), "990000000.0");
assert_eq!(
fmt_line(999_999_999.0),
"1.000e9",
"f32 rounds this up to 1e9"
);
}
#[test]
fn non_finite_passes_through_without_widening() {
assert_eq!(fmt_line(f32::NAN), "NaN");
assert_eq!(fmt_line(f32::INFINITY), "inf");
assert_eq!(fmt_line(f32::NEG_INFINITY), "-inf");
}
#[test]
fn cell_shares_the_line_band_rule_at_every_edge() {
for v in [
117.55, 97.1, 3.25, 1.0, 0.6348, 0.5, 0.000_04, 1e-8, 0.0, -42.25,
] {
assert_eq!(
fmt_cell(v),
fmt_line(v),
"{v} spelled differently in a cell"
);
}
assert_eq!(fmt_cell(0.5), "0.5000");
assert_eq!(fmt_cell(0.000_04), "0.00004");
assert_eq!(fmt_cell(1e-8), "0.000000");
assert_eq!(fmt_cell(1e9), "1.000e9");
assert_eq!(fmt_cell(1.5e38), "1.500e38");
}
#[test]
fn cfg_is_flat_two_decimals() {
assert_eq!(fmt_cfg(0.7), "0.70");
assert_eq!(fmt_cfg(0.0), "0.00");
assert_eq!(fmt_cfg(1.0), "1.00");
}
#[test]
fn units_suffix_thousands_and_promote_on_rounding() {
assert_eq!(fmt_units(0), "0");
assert_eq!(fmt_units(402), "402");
assert_eq!(fmt_units(999), "999");
assert_eq!(fmt_units(1_000), "1.0K");
assert_eq!(fmt_units(6_218), "6.2K");
assert_eq!(fmt_units(40_242), "40.2K");
assert_eq!(fmt_units(142_946), "142.9K");
assert_eq!(fmt_units(1_500_000), "1.5M");
assert_eq!(fmt_units(2_000_000_000), "2.0B");
assert_eq!(fmt_units(999_950), "1.0M");
assert_eq!(fmt_units(999_999_999), "1.0B");
assert_eq!(fmt_units(5_000_000_000_000), "5000.0B");
assert_eq!(fmt_units(-40_242), "-40.2K");
}
#[test]
fn optional_forms_render_unset_as_dash() {
assert_eq!(fmt_line_opt(&None), crate::utils::cfy::NO_VALUE);
assert_eq!(fmt_line_opt(&Some(0.02)), "0.0200");
}
#[test]
fn reading_states_spread_and_standard_error_together() {
assert_eq!(
fmt_reading(Some(100.0), Some(10.0), 4, ReadingStyle::Line),
"avg 100.0 (spread 10.0; SE 5.00 = spread/√4, n=4)"
);
assert_eq!(
fmt_reading(Some(100.0), Some(10.0), 4, ReadingStyle::Cell),
"avg 100.0 (spread 10.0; SE 5.00 = spread/√4, n=4)"
);
assert_eq!(
fmt_reading(Some(100.0), Some(10.0), 4, ReadingStyle::Doc),
"avg 100.00 (spread 10.00; SE 5.00 = spread/√4, n=4)"
);
}
#[test]
fn reading_does_not_invent_a_spread_for_one_sample() {
assert_eq!(
fmt_reading(Some(327.8), None, 1, ReadingStyle::Line),
"avg 327.8 (n=1, no spread yet)"
);
assert_eq!(
fmt_reading(None, None, 0, ReadingStyle::Line),
crate::utils::cfy::NO_VALUE
);
}
#[test]
fn a_family_reading_names_its_own_pair() {
use crate::engine::config::MeanType;
assert_eq!(
fmt_family_reading(
Some(100.0),
Some(10.0),
4,
ReadingStyle::Doc,
MeanType::Arithmetic
),
fmt_reading(Some(100.0), Some(10.0), 4, ReadingStyle::Doc)
);
let median = fmt_family_reading(
Some(0.84375),
Some(0.023166),
4,
ReadingStyle::Doc,
MeanType::Median,
);
assert_eq!(median, "window median 0.84 (1.4826·MAD 0.02, n=4)");
assert!(
!median.contains("SE"),
"a scaled MAD has no standard error: {median}"
);
assert_eq!(
fmt_family_reading(Some(0.5), None, 1, ReadingStyle::Doc, MeanType::SignedLog),
"window g-avg 0.50 (n=1, no spread yet)"
);
assert_eq!(
fmt_family_reading(None, None, 0, ReadingStyle::Line, MeanType::Median),
crate::utils::cfy::NO_VALUE
);
}
#[test]
fn census_omits_se_because_a_population_has_none() {
assert_eq!(
fmt_census(100.0, 10.0, ReadingStyle::Line),
"avg 100.0 (spread 10.0)"
);
assert_eq!(
fmt_census(100.0, 10.0, ReadingStyle::Doc),
"avg 100.00 (spread 10.00)"
);
}
#[test]
fn csv_field_quotes_only_when_needed() {
assert_eq!(csv_field("plain"), "plain");
assert_eq!(csv_field("a,b"), "\"a,b\"");
assert_eq!(csv_field("say \"hi\""), "\"say \"\"hi\"\"\"");
}
}