diff --git a/crates/engine/src/cmd/edit.rs b/crates/engine/src/cmd/edit.rs index 3d596cbe0..67d682d03 100644 --- a/crates/engine/src/cmd/edit.rs +++ b/crates/engine/src/cmd/edit.rs @@ -41,9 +41,9 @@ pub static COMMANDS: &[CommandSpec] = &[ CommandSpec::new("timeline.move_many", "Move Timeline Items", move_many).icon("reorder").params( "features?: [id|name], groups?: [folder ids], group?: folder id, position: insertion slot; atomically changes folder membership and moves all selected contents as one block", ), - CommandSpec::new("timeline.rollback", "Move Timeline Marker", rollback).icon("rollback").params("position: number of features to keep active (omit = end)"), + CommandSpec::new("timeline.rollback", "Move Timeline Marker", rollback).icon("rollback").params("position: number of features to keep active, or \"end\" (omit = end)"), CommandSpec::new("timeline.roll_to", "Roll History Marker", rollback).icon("rollback") - .params("position: number of features to keep active (omit = end) | feature: id|name (the marker goes right after it)"), + .params("position: number of features to keep active, or \"end\" (omit = end) | feature: id|name (the marker goes right after it)"), CommandSpec::new("timeline.reorder", "Reorder Feature", reorder).icon("reorder") .params("feature: id|name, position: its new index; refused when a feature that worked would fail"), CommandSpec::new("timeline.reorder_many", "Reorder Features", reorder_many).icon("reorder").params( @@ -509,7 +509,17 @@ fn rollback(s: &mut Session, p: &Value) -> Result { let id = feature_id(s, Some(f), "timeline.roll_to")?; s.doc.feature_index(id).map(|i| i + 1) } - None => num(p, "position").map(|x| x.max(0.0) as usize), + // A number of features, or "end" (as omitting it); anything else is refused rather than + // read as "end". + None => match p.get("position") { + None | Some(Value::Null) => None, + Some(Value::String(t)) if t.trim().eq_ignore_ascii_case("end") => None, + Some(v) => { + let x = v.as_f64().or_else(|| v.as_str().and_then(|t| t.trim().parse::().ok())).and_then(crate::params::finite); + let why = "`position` must be a number of features to keep active, or \"end\""; + Some(x.map(|x| x.max(0.0) as usize).ok_or_else(|| bad("timeline.roll_to", why))?) + } + }, }; s.doc_mut().marker = match pos { Some(k) if k < n => Some(k), diff --git a/crates/engine/src/cmd/edit_tests.rs b/crates/engine/src/cmd/edit_tests.rs index 22ebcf033..a5381f327 100644 --- a/crates/engine/src/cmd/edit_tests.rs +++ b/crates/engine/src/cmd/edit_tests.rs @@ -8,6 +8,28 @@ fn run(s: &mut Session, id: &str, params: Value) -> Value { s.execute(id, ¶ms).unwrap_or_else(|e| panic!("{id} {params}: {e}")) } +/// `timeline.roll_to` takes a number of features or "end"; any other value is refused instead of +/// silently moving the marker to the end. +#[test] +fn roll_to_takes_a_number_or_end_and_refuses_anything_else() { + let mut s = Session::default(); + run(&mut s, "solid.box", json!({"length": 10, "width": 10, "height": 10})); + run(&mut s, "solid.box", json!({"length": 5, "width": 5, "height": 20, "operation": "join"})); + for cmd in ["timeline.roll_to", "timeline.rollback"] { + assert_eq!(run(&mut s, cmd, json!({"position": 1}))["marker"], 1); + assert_eq!(run(&mut s, cmd, json!({"position": "end"}))["marker"], 2); + assert_eq!(run(&mut s, cmd, json!({"position": "1"}))["marker"], 1, "a number in a string is still a number"); + assert_eq!(run(&mut s, cmd, json!({"position": "END"}))["marker"], 2); + run(&mut s, cmd, json!({"position": 1})); + for bad in [json!("start"), json!("first"), json!([1]), json!({"n": 1}), json!(true), json!("NaN")] { + let e = s.execute(cmd, &json!({"position": bad})).unwrap_err().to_string(); + assert!(e.contains("`position` must be a number") && e.contains("\"end\""), "{bad}: {e}"); + assert_eq!(s.doc.marker, Some(1), "{bad}: the refused call left the marker where it was"); + } + assert_eq!(run(&mut s, cmd, json!({}))["marker"], 2, "omitted: the end"); + } +} + #[test] fn g2_fillet_survives_edit_undo_and_reopen() { use solidcraft_doc::{Document, FeatureKind, FilletStyle};