#[inline]
#[must_use]
#[expect(
clippy::cast_possible_truncation,
reason = "Intentional for parsing, value range validated"
)]
fn clamp_precision_with_log(len: usize, context: &str, input: &str) -> u8 {
if len > u8::MAX as usize {
log::debug!(
"{} precision clamped from {} to {} for input: {}",
context,
len,
u8::MAX,
input
);
}
len.min(u8::MAX as usize) as u8
}
#[inline]
#[must_use]
#[expect(
clippy::cast_possible_truncation,
reason = "value is clamped to u8::MAX before the cast"
)]
fn parse_scientific_exponent(exponent_str: &str, strict: bool) -> Option<u8> {
if let Ok(exp) = exponent_str.parse::<u64>() {
Some(exp.min(u64::from(u8::MAX)) as u8)
} else {
assert!(
!(exponent_str.is_empty() && strict),
"Invalid scientific notation format: missing exponent after 'e-'"
);
if exponent_str.is_empty() {
return None;
}
if exponent_str.chars().all(|c| c.is_ascii_digit()) {
Some(u8::MAX)
} else if strict {
panic!("Invalid scientific notation exponent '{exponent_str}': must be a valid number")
} else {
None
}
}
}
#[must_use]
pub fn precision_from_str(s: &str) -> u8 {
let s = s.trim().to_ascii_lowercase();
if s.contains("e-") {
let exponent_str = s
.split("e-")
.nth(1)
.expect("Invalid scientific notation format: missing exponent after 'e-'");
return parse_scientific_exponent(exponent_str, true)
.expect("parse_scientific_exponent should return Some in strict mode");
}
if let Some((_, decimal_part)) = s.split_once('.') {
clamp_precision_with_log(decimal_part.len(), "Decimal", &s)
} else {
0
}
}
#[must_use]
pub fn min_increment_precision_from_str(s: &str) -> u8 {
let s = s.trim().to_ascii_lowercase();
if let Some(pos) = s.find('e')
&& s[pos + 1..].starts_with('-')
{
let exponent_str = &s[pos + 2..];
return parse_scientific_exponent(exponent_str, false).unwrap_or(0);
}
if let Some(dot_pos) = s.find('.') {
let decimal_part = &s[dot_pos + 1..];
if decimal_part.chars().any(|c| c != '0') {
let trimmed_len = decimal_part.trim_end_matches('0').len();
return clamp_precision_with_log(trimmed_len, "Minimum increment", &s);
}
clamp_precision_with_log(decimal_part.len(), "Decimal", &s)
} else {
0
}
}
pub fn bytes_to_usize(bytes: &[u8]) -> anyhow::Result<usize> {
if bytes.len() >= std::mem::size_of::<usize>() {
let mut buffer = [0u8; std::mem::size_of::<usize>()];
buffer.copy_from_slice(&bytes[..std::mem::size_of::<usize>()]);
Ok(usize::from_le_bytes(buffer))
} else {
anyhow::bail!("Not enough bytes to represent a `usize`");
}
}
#[cfg(test)]
mod tests {
use rstest::rstest;
use super::*;
#[rstest]
#[case("", 0)]
#[case("0", 0)]
#[case("1.0", 1)]
#[case("1.00", 2)]
#[case("1.23456789", 8)]
#[case("123456.789101112", 9)]
#[case("0.000000001", 9)]
#[case("1e-1", 1)]
#[case("1e-2", 2)]
#[case("1e-3", 3)]
#[case("1e8", 0)]
#[case("-1.23", 2)]
#[case("-1e-2", 2)]
#[case("1E-2", 2)]
#[case(" 1.23", 2)]
#[case("1.23 ", 2)]
fn test_precision_from_str(#[case] s: &str, #[case] expected: u8) {
let result = precision_from_str(s);
assert_eq!(result, expected);
}
#[rstest]
#[case("", 0)]
#[case("0", 0)]
#[case("1.0", 1)]
#[case("1.00", 2)]
#[case("1.23456789", 8)]
#[case("123456.789101112", 9)]
#[case("0.000000001", 9)]
#[case("1e-1", 1)]
#[case("1e-2", 2)]
#[case("1e-3", 3)]
#[case("1e8", 0)]
#[case("-1.23", 2)]
#[case("-1e-2", 2)]
#[case("1E-2", 2)]
#[case(" 1.23", 2)]
#[case("1.23 ", 2)]
#[case("1.010", 2)]
#[case("1.00100", 3)]
#[case("0.0001000", 4)]
#[case("1.000000000", 9)]
fn test_min_increment_precision_from_str(#[case] s: &str, #[case] expected: u8) {
let result = min_increment_precision_from_str(s);
assert_eq!(result, expected);
}
#[rstest]
fn test_bytes_to_usize_empty() {
let payload: Vec<u8> = vec![];
let result = bytes_to_usize(&payload);
assert!(result.is_err());
assert_eq!(
result.err().unwrap().to_string(),
"Not enough bytes to represent a `usize`"
);
}
#[rstest]
fn test_bytes_to_usize_invalid() {
let payload: Vec<u8> = vec![0x01, 0x02, 0x03];
let result = bytes_to_usize(&payload);
assert!(result.is_err());
assert_eq!(
result.err().unwrap().to_string(),
"Not enough bytes to represent a `usize`"
);
}
#[rstest]
fn test_bytes_to_usize_valid() {
let payload: Vec<u8> = vec![0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08];
let result = bytes_to_usize(&payload).unwrap();
assert_eq!(result, 0x0807_0605_0403_0201);
assert_eq!(result, 578_437_695_752_307_201);
}
#[rstest]
fn test_precision_from_str_large_exponent_clamped() {
let result = precision_from_str("1e-999");
assert_eq!(result, 255);
}
#[rstest]
fn test_precision_from_str_very_large_exponent_clamped() {
let result = precision_from_str("1e-300");
assert_eq!(result, 255);
let result = precision_from_str("1e-1000000");
assert_eq!(result, 255);
}
#[rstest]
#[should_panic(expected = "Invalid scientific notation exponent")]
fn test_precision_from_str_invalid_exponent_not_numeric() {
let _ = precision_from_str("1e-abc");
}
#[rstest]
#[should_panic(expected = "missing exponent after 'e-'")]
fn test_precision_from_str_malformed_scientific_notation() {
let _ = precision_from_str("1e-");
}
#[rstest]
fn test_precision_from_str_edge_case_max_u8() {
let result = precision_from_str("1e-255");
assert_eq!(result, 255);
}
#[rstest]
fn test_precision_from_str_just_above_max_u8() {
let result = precision_from_str("1e-256");
assert_eq!(result, 255);
}
#[rstest]
fn test_precision_from_str_u32_overflow() {
let result = precision_from_str("1e-4294967296");
assert_eq!(result, 255);
}
#[rstest]
fn test_precision_from_str_u64_overflow() {
let result = precision_from_str("1e-99999999999999999999");
assert_eq!(result, 255);
}
#[rstest]
fn test_min_increment_precision_from_str_large_exponent() {
let result = min_increment_precision_from_str("1e-300");
assert_eq!(result, 255);
}
#[rstest]
fn test_min_increment_precision_from_str_very_large_exponent() {
let result = min_increment_precision_from_str("1e-99999999999999999999");
assert_eq!(result, 255);
}
#[rstest]
fn test_min_increment_precision_from_str_consistency() {
let input = "1e-1000";
let precision = precision_from_str(input);
let min_precision = min_increment_precision_from_str(input);
assert_eq!(precision, min_precision);
assert_eq!(precision, 255);
}
#[rstest]
fn test_min_increment_precision_from_str_empty_exponent() {
let result = min_increment_precision_from_str("1e-");
assert_eq!(result, 0);
}
}