diff --git a/crates/ui-egui/src/dialogs/find_replace.rs b/crates/ui-egui/src/dialogs/find_replace.rs index 523586d1a9e..f56d07c722a 100644 --- a/crates/ui-egui/src/dialogs/find_replace.rs +++ b/crates/ui-egui/src/dialogs/find_replace.rs @@ -260,16 +260,21 @@ fn replace_all(app: &mut VectorcraftApp, d: &mut Dialog) -> Result Result { let mut d = d.clone(); - let action = d.fields.insert("action".into(), json!("find")).and_then(|a| a.as_str().map(str::to_string)).unwrap_or_default(); - if let Err(e) = check_types(&d.fields, false) { + let action = d.fields.insert("action".into(), json!("find")); + let checked = match &action { + Some(a) if !a.is_string() => Err("`action` must be a string".to_string()), + _ => check_types(&d.fields, false), + }; + if let Err(e) = checked { d.fields.insert(NOTE.into(), json!(e)); app.ui.dialog = Some(d); return Err(e); } - let r = match action.as_str() { + let r = match action.as_ref().and_then(Value::as_str).unwrap_or_default() { "done" => { remember(app, &d); app.ui.dialog = None; diff --git a/crates/ui-egui/src/dialogs/find_replace/tests.rs b/crates/ui-egui/src/dialogs/find_replace/tests.rs index 0e38820877c..59dbe4937b6 100644 --- a/crates/ui-egui/src/dialogs/find_replace/tests.rs +++ b/crates/ui-egui/src/dialogs/find_replace/tests.rs @@ -174,6 +174,27 @@ fn the_dialog_rejects_a_field_of_the_wrong_type() { assert_eq!((field(&app, "find"), field(&app, "replace"), field(&app, "wholeWord")), (json!("cat"), json!("dog"), json!(true))); } +/// An `action` that is not a string runs no action, Find included: the error is the note, and the +/// match found, the selection and the last search stay as they were. +#[test] +fn an_action_of_the_wrong_type_runs_no_action() { + let (mut app, _) = app_with(&["cat", "cat"]); + choose(&mut app); + set(&mut app, "find", json!("cat")); + crate::dialogs::confirm(&mut app).unwrap(); + let (at, sel, last) = (matched(&app), selected(&app), app.ui.last_find.clone()); + assert!(at.is_some()); + for v in [json!(5), Value::Null, json!(["find"])] { + set(&mut app, "action", v.clone()); + assert_eq!(crate::dialogs::confirm(&mut app).unwrap_err(), "`action` must be a string", "{v}"); + assert_eq!((matched(&app), selected(&app), app.ui.last_find.clone()), (at, sel.clone(), last.clone()), "{v}"); + assert_eq!(field(&app, "__note"), json!("`action` must be a string"), "{v}"); + } + set(&mut app, "action", json!("find")); + crate::dialogs::confirm(&mut app).unwrap(); + assert_ne!(matched(&app), at, "a string action still runs"); +} + /// The dialog stays open from match to match: Find goes to the next match and selects its /// object, Replace changes that match, Replace & Find changes it and goes on, Replace All changes /// the rest, and Done closes. Each Replace is one undo step, and so is Replace All; finding and a