Find and Replace: a dialog field of the wrong type is an error

ui.paramDialog for edit.findReplace used to drop a field of the wrong type, such as a number for find, and open on the last search. It now returns an error and leaves the dialog closed. A null field still gives none, as the menu item's does.
This commit is contained in:
dotCooCoo
2026-11-17 21:53:34 -08:00
parent 1f704f2dc3
commit d4b1ff8f60
5 changed files with 32 additions and 5 deletions
+1 -1
View File
@@ -847,7 +847,7 @@ pub const UI_COMMANDS: &[(&str, &str, &str, &str)] = &[
"ui.paramDialog",
"Command Dialog",
"",
"{command, label?, params} open a parameter dialog for any command. For edit.findReplace it opens Find and Replace (dialog `findReplace`), which stays open: fields find, replace, matchCase, wholeWord, backwards, hiddenLayers, lockedLayers, and action, which ui.dialog.confirm runs: find (the next match, edit.findNext, shown in the dialog with its place among the matches), replace (the match found, edit.replaceMatch), replaceFind (both), replaceAll (edit.findReplace) or done (close). After each one action is find again. Replace and Replace & Find change a match that find reached with the find text and options as they are now, and find one otherwise. The dialog opens on the last search, with the fields params gives in its place, and Edit › Find Next goes on from it in the same document while the dialog is closed",
"{command, label?, params} open a parameter dialog for any command. For edit.findReplace it opens Find and Replace (dialog `findReplace`), which stays open: fields find, replace, matchCase, wholeWord, backwards, hiddenLayers, lockedLayers, and action, which ui.dialog.confirm runs: find (the next match, edit.findNext, shown in the dialog with its place among the matches), replace (the match found, edit.replaceMatch), replaceFind (both), replaceAll (edit.findReplace) or done (close). After each one action is find again. Replace and Replace & Find change a match that find reached with the find text and options as they are now, and find one otherwise. The dialog opens on the last search, with the fields params gives in its place (a null field gives none; a field of the wrong type is an error), and Edit › Find Next goes on from it in the same document while the dialog is closed",
),
(
"ui.recolorDialog",
+9 -2
View File
@@ -79,8 +79,8 @@ fn active_uid(app: &VectorcraftApp) -> Option<u64> {
}
/// Open Find and Replace on the last search when there has been one, with the fields that
/// `params` gives in its place. A value of another type than the field's, such as the menu item's
/// null `find` ([`menu_params`]), gives none.
/// `params` gives in its place. A null value, such as the menu item's `find` ([`menu_params`]),
/// gives none. A field given a value of another type is an error, and the dialog does not open.
pub fn open(app: &mut VectorcraftApp, params: &Map<String, Value>) -> Result<Value, String> {
let mut fields = Map::new();
fields.insert("find".into(), json!(""));
@@ -88,6 +88,13 @@ pub fn open(app: &mut VectorcraftApp, params: &Map<String, Value>) -> Result<Val
for key in SWITCHES.into_iter().chain(["backwards"]) {
fields.insert(key.into(), json!(false));
}
for (k, v) in params.iter().filter(|(_, v)| !v.is_null()) {
match fields.get(k) {
Some(Value::String(_)) if !v.is_string() => return Err(format!("`{k}` must be a string")),
Some(Value::Bool(_)) if !v.is_boolean() => return Err(format!("`{k}` must be true or false")),
_ => {}
}
}
let last = app.ui.last_find.as_ref().and_then(Value::as_object).cloned().unwrap_or_default();
for (k, v) in last.iter().chain(params) {
// Only the dialog's own fields, each of its own type.
@@ -131,6 +131,25 @@ fn the_edit_menu_opens_the_find_and_replace_dialog() {
}
}
/// A field given a value of another type than its own is an error and the dialog does not open; a
/// null value gives no field.
#[test]
fn the_dialog_rejects_a_field_of_the_wrong_type() {
let (mut app, _) = app_with(&["cat"]);
let open = |app: &mut VectorcraftApp, params: Value| app.run("ui.paramDialog", json!({"command": super::COMMAND, "params": params}));
for (params, want) in [
(json!({"find": 17}), "`find` must be a string"),
(json!({"replace": true}), "`replace` must be a string"),
(json!({"matchCase": "yes"}), "`matchCase` must be true or false"),
(json!({"backwards": 1}), "`backwards` must be true or false"),
] {
assert_eq!(open(&mut app, params.clone()).unwrap_err(), want, "{params}");
assert!(app.ui.dialog.is_none(), "{params}");
}
open(&mut app, json!({"find": null, "replace": "dog", "wholeWord": true})).unwrap();
assert_eq!((field(&app, "find"), field(&app, "replace"), field(&app, "wholeWord")), (json!(""), json!("dog"), json!(true)));
}
/// 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
+1 -1
View File
@@ -848,7 +848,7 @@ pub const UI_COMMANDS: &[(&str, &str, &str, &str)] = &[
"ui.paramDialog",
"Command Dialog",
"",
"{command, label?, params} open a parameter dialog for any command. For edit.findReplace it opens Find and Replace (dialog `findReplace`), which stays open: fields find, replace, matchCase, wholeWord, backwards, hiddenLayers, lockedLayers, and action, which ui.dialog.confirm runs: find (the next match, edit.findNext, shown in the dialog with its place among the matches), replace (the match found, edit.replaceMatch), replaceFind (both), replaceAll (edit.findReplace) or done (close). After each one action is find again. Replace and Replace & Find change a match that find reached with the find text and options as they are now, and find one otherwise. The dialog opens on the last search, with the fields params gives in its place, and Edit › Find Next goes on from it in the same document while the dialog is closed",
"{command, label?, params} open a parameter dialog for any command. For edit.findReplace it opens Find and Replace (dialog `findReplace`), which stays open: fields find, replace, matchCase, wholeWord, backwards, hiddenLayers, lockedLayers, and action, which ui.dialog.confirm runs: find (the next match, edit.findNext, shown in the dialog with its place among the matches), replace (the match found, edit.replaceMatch), replaceFind (both), replaceAll (edit.findReplace) or done (close). After each one action is find again. Replace and Replace & Find change a match that find reached with the find text and options as they are now, and find one otherwise. The dialog opens on the last search, with the fields params gives in its place (a null field gives none; a field of the wrong type is an error), and Edit › Find Next goes on from it in the same document while the dialog is closed",
),
(
"ui.recolorDialog",
+2 -1
View File
@@ -9996,7 +9996,8 @@ the options as they are now, and find one otherwise. Enter runs Find. Edit › F
search while the dialog is closed, and opens the dialog when there has been none. A place the search reached in one
document is not used in another, nor once the Find text or an option that decides a match changes: the search then
starts again as it does without `from`. `ui.paramDialog` opens
the dialog on the last search, with the fields its `params` give in its place.
the dialog on the last search, with the fields its `params` give in its place. A null field gives none, and a field of
the wrong type is an error that leaves the dialog closed.
## Justification options