use crate::{Result, ScanConfig, ScanError};
pub const MAX_SAFE_INTEGER: u64 = (1 << 53) - 1;
const MAX_BUDGET: f64 = 4_294_967_295.0;
pub fn budget_ms_from_number(ms: f64) -> Result<Option<u64>> {
if ms.is_nan() || ms <= 0.0 || ms.is_infinite() {
return Ok(None);
}
let whole = ms.floor();
if whole > MAX_BUDGET {
return Err(ScanError::invalid_option(
"budget_ms",
format!(
"{ms} ms is above the maximum {} ms (0 = unbounded)",
ScanConfig::MAX_BUDGET_MS
),
));
}
#[expect(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
reason = "a whole number in 0..=MAX_BUDGET_MS, checked above"
)]
Ok(Some(whole as u64))
}
pub fn max_dimension_from_number(value: f64) -> Result<u32> {
let whole = whole_number("max_dimension", value, u64::from(u32::MAX))?;
u32::try_from(whole).map_err(|_| out_of_domain("max_dimension", value, u64::from(u32::MAX)))
}
pub fn max_pixels_from_number(value: f64) -> Result<u64> {
whole_number("max_pixels", value, MAX_SAFE_INTEGER)
}
fn whole_number(option: &str, value: f64, max: u64) -> Result<u64> {
#[expect(
clippy::cast_precision_loss,
reason = "max is u32::MAX or MAX_SAFE_INTEGER, both exact doubles"
)]
let ceiling = max as f64;
#[expect(clippy::float_cmp, reason = "an exact whole-number test")]
let whole = value.trunc() == value;
if !(value.is_finite() && whole && value >= 1.0 && value <= ceiling) {
return Err(out_of_domain(option, value, max));
}
#[expect(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
reason = "a whole number in 1..=max, checked above"
)]
Ok(value as u64)
}
fn out_of_domain(option: &str, value: f64, max: u64) -> ScanError {
ScanError::invalid_option(
option,
format!("{value} is not a whole number from 1 to {max}"),
)
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used)]
use super::*;
fn code(result: Result<impl std::fmt::Debug>) -> &'static str {
result.unwrap_err().code()
}
const NOT_A_LIMIT: [f64; 8] = [
0.0,
-0.0,
-1.0,
0.5,
1.5,
f64::NAN,
f64::INFINITY,
f64::NEG_INFINITY,
];
#[test]
fn the_double_ceilings_are_the_configured_ones() {
#[expect(clippy::cast_precision_loss, reason = "exact below 2^53")]
let max = ScanConfig::MAX_BUDGET_MS as f64;
assert!((max - MAX_BUDGET).abs() < f64::EPSILON);
assert_eq!(MAX_SAFE_INTEGER, 9_007_199_254_740_991);
}
#[test]
fn a_budget_number_maps_to_the_documented_domain() {
for ms in [
0.0,
-0.0,
-1.0,
-0.5,
f64::NAN,
f64::NEG_INFINITY,
f64::INFINITY,
] {
assert_eq!(budget_ms_from_number(ms).unwrap(), None, "{ms}");
}
for (ms, want) in [
(f64::MIN_POSITIVE, 0),
(0.5, 0),
(1.0, 1),
(1.9, 1),
(1500.0, 1500),
(4_294_967_295.0, 4_294_967_295),
(4_294_967_295.9, 4_294_967_295),
] {
assert_eq!(budget_ms_from_number(ms).unwrap(), Some(want), "{ms}");
}
for ms in [
4_294_967_296.0,
4_294_967_297.0,
9_007_199_254_740_993.0,
1e300,
f64::MAX,
] {
let err = budget_ms_from_number(ms).unwrap_err();
assert_eq!(err.code(), "QRS-006", "{ms}");
assert!(err.to_string().contains("budget_ms"), "{err}");
}
}
#[test]
fn a_dimension_number_is_a_whole_number_from_one_to_u32_max() {
for (value, want) in [(1.0, 1), (4096.0, 4096), (4_294_967_295.0, u32::MAX)] {
assert_eq!(max_dimension_from_number(value).unwrap(), want);
}
for value in NOT_A_LIMIT
.into_iter()
.chain([4096.7, 4_294_967_296.0, 1e300])
{
let err = max_dimension_from_number(value).unwrap_err();
assert_eq!(err.code(), "QRS-006", "{value}");
assert!(err.to_string().contains("max_dimension"), "{err}");
}
}
#[test]
fn a_pixel_number_is_a_whole_number_from_one_to_the_safe_integer() {
for (value, want) in [
(1.0, 1),
(16_000_000.0, 16_000_000),
(9_007_199_254_740_991.0, MAX_SAFE_INTEGER),
] {
assert_eq!(max_pixels_from_number(value).unwrap(), want);
}
for value in NOT_A_LIMIT
.into_iter()
.chain([9_007_199_254_740_992.0, 1e300])
{
let err = max_pixels_from_number(value).unwrap_err();
assert_eq!(err.code(), "QRS-006", "{value}");
assert!(err.to_string().contains("max_pixels"), "{err}");
}
assert_eq!(code(max_pixels_from_number(f64::MAX)), "QRS-006");
}
}