use alloc::{format, string::String};
pub const MERCURY_MAX_CONTENT_PCT: f64 = 0.0005;
pub const CADMIUM_PORTABLE_MAX_CONTENT_PCT: f64 = 0.002;
#[must_use]
pub fn mercury_content_prohibited(content_pct: f64) -> bool {
content_pct > MERCURY_MAX_CONTENT_PCT
}
#[must_use]
pub fn cadmium_content_prohibited_for_portable(content_pct: f64) -> bool {
content_pct > CADMIUM_PORTABLE_MAX_CONTENT_PCT
}
pub fn validate_operating_temp_range(min_c: Option<f64>, max_c: Option<f64>) -> Result<(), String> {
if let (Some(min), Some(max)) = (min_c, max_c)
&& min >= max
{
return Err(format!(
"operatingTempMinC ({min}°C) must be less than operatingTempMaxC ({max}°C)"
));
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn mercury_at_and_below_threshold_allowed() {
assert!(!mercury_content_prohibited(0.0));
assert!(!mercury_content_prohibited(0.0005)); }
#[test]
fn mercury_above_threshold_prohibited() {
assert!(mercury_content_prohibited(0.0006));
assert!(mercury_content_prohibited(1.0));
}
#[test]
fn cadmium_at_and_below_threshold_allowed() {
assert!(!cadmium_content_prohibited_for_portable(0.0));
assert!(!cadmium_content_prohibited_for_portable(0.002)); }
#[test]
fn cadmium_above_threshold_prohibited() {
assert!(cadmium_content_prohibited_for_portable(0.0021));
}
#[test]
fn temp_range_valid_cases() {
assert!(validate_operating_temp_range(Some(-20.0), Some(60.0)).is_ok());
assert!(validate_operating_temp_range(None, Some(60.0)).is_ok()); assert!(validate_operating_temp_range(None, None).is_ok());
}
#[test]
fn temp_range_min_greater_than_max_rejected() {
let err = validate_operating_temp_range(Some(60.0), Some(-20.0)).unwrap_err();
assert!(err.contains("operatingTempMinC"), "unexpected: {err}");
}
#[test]
fn temp_range_equal_values_rejected() {
let err = validate_operating_temp_range(Some(25.0), Some(25.0)).unwrap_err();
assert!(err.contains("less than"), "unexpected: {err}");
}
}