Find and Replace: an action that is not a string runs no action

Confirming the dialog with a number, null or list as its action used to run Find, which moved the match found, the selection and the last search. It is now an error, shown as the note, like a field of the wrong type.
This commit is contained in:
dotCooCoo
2026-11-18 01:12:58 -08:00
parent 2275ca2fe0
commit d27a9e9466
2 changed files with 30 additions and 4 deletions
+9 -4
View File
@@ -260,16 +260,21 @@ fn replace_all(app: &mut VectorcraftApp, d: &mut Dialog) -> Result<Value, String
/// A button or `ui.dialog.confirm`: run the `action` and keep the dialog open, or close it for
/// `done`. What a command refuses is the note, and the dialog stays open. A field set to a value
/// of another type than its own ([`check_types`]) runs no action: the error is the note, shown
/// under the match found, which Replace still changes once the field is set right.
/// under the match found, which Replace still changes once the field is set right. So does an
/// `action` that is not a string.
fn confirm(app: &mut VectorcraftApp, d: &Dialog) -> Result<Value, String> {
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;
@@ -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