diff --git a/crates/engine/src/cmd/props.rs b/crates/engine/src/cmd/props.rs index 75dc0b04..c89204c2 100644 --- a/crates/engine/src/cmd/props.rs +++ b/crates/engine/src/cmd/props.rs @@ -539,10 +539,17 @@ impl Interactive for LtscaleM { } } +/// The largest THICKNESS, either way. +const MAX_THICKNESS: f64 = 1e12; + +fn thickness_in_range(t: f64) -> bool { + t.is_finite() && t.abs() <= MAX_THICKNESS +} + fn run_thickness(s: &mut Session, p: &Value) -> Result { let t = f64_req("thickness", p, "thickness")?; - if !t.is_finite() || t.abs() > 1e12 { - return Err(bad("thickness", "thickness is out of range")); + if !thickness_in_range(t) { + return Err(bad("thickness", "thickness is out of range (-1e12 to 1e12)")); } s.doc_mut()?.header.set_f64("THICKNESS", t); Ok(json!({ "thickness": t })) @@ -565,9 +572,16 @@ impl Interactive for ThicknessM { Input::Text(t) => t, _ => return Ok(Step::Continue), }; - match super::machines::number(&text).map(|t| run_thickness(s, &json!({ "thickness": t }))) { - Some(Ok(_)) => Ok(Step::Done), - _ => { + match super::machines::number(&text) { + Some(t) if thickness_in_range(t) => { + run_thickness(s, &json!({ "thickness": t }))?; + Ok(Step::Done) + } + Some(_) => { + s.echo("Requires a value between -1e12 and 1e12."); + Ok(Step::Continue) + } + None => { s.echo("Requires a number."); Ok(Step::Continue) } diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 73125610..8c590dd9 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -1317,6 +1317,12 @@ fn format_thickness_asks_for_the_thickness() { // After text that is not a number, the prompt asks again. s.cmdline("thick").unwrap(); assert!(s.current_prompt().is_some(), "asked again"); + assert_eq!(s.log.last().map(String::as_str), Some("Requires a number.")); + // A value out of range says so, and the prompt asks again. + s.cmdline("2e12").unwrap(); + assert!(s.current_prompt().is_some(), "asked again"); + assert_eq!(s.log.last().map(String::as_str), Some("Requires a value between -1e12 and 1e12.")); + assert_eq!(s.doc().unwrap().header.f64("THICKNESS", 0.0), 0.0); // A negative thickness is accepted. s.cmdline("-1.5").unwrap(); assert!(s.current_prompt().is_none());