diff --git a/crates/ui-egui/src/dialogs/recolor.rs b/crates/ui-egui/src/dialogs/recolor.rs index 19657a52ca..682954ac7f 100644 --- a/crates/ui-egui/src/dialogs/recolor.rs +++ b/crates/ui-egui/src/dialogs/recolor.rs @@ -199,7 +199,11 @@ pub fn open(app: &mut VectorcraftApp, p: &Value) -> Result { } let st = app.session.active().ok_or("no document open")?; let art = !st.selection.is_empty(); - let group = p.get("group").and_then(Value::as_str); + let group = match p.get("group") { + None | Some(Value::Null) => None, + Some(Value::String(g)) => Some(g.as_str()), + Some(_) => return Err("`group` must be a colour group name".into()), + }; // The group is looked for in the document without the preview of the dialog this one replaces // (which can rename the group), as that dialog's Cancel below leaves it. let unpreviewed = match &st.interaction { @@ -283,12 +287,14 @@ pub(super) fn library_choices(app: &VectorcraftApp) -> (Vec, Vec (keys, shown) } -/// The `limitTo` key of `ui.recolorDialog`'s `library` (none without it, the first library for ""). +/// The `limitTo` key of `ui.recolorDialog`'s `library` (none without it or for null, the first +/// library for ""); a `library` that is not a string is an error. pub(super) fn library_param(app: &VectorcraftApp, p: &Value) -> Result { - match p.get("library").and_then(Value::as_str) { - Some("") => Ok(first_library(app).unwrap_or_default()), - Some(l) => limit_key(app, l), - None => Ok(String::new()), + match p.get("library") { + Some(Value::String(l)) if l.is_empty() => Ok(first_library(app).unwrap_or_default()), + Some(Value::String(l)) => limit_key(app, l), + None | Some(Value::Null) => Ok(String::new()), + Some(_) => Err("`library` must be a library id or name".into()), } } diff --git a/crates/ui-egui/src/tests/recolor.rs b/crates/ui-egui/src/tests/recolor.rs index 766f47b047..e618b4e830 100644 --- a/crates/ui-egui/src/tests/recolor.rs +++ b/crates/ui-egui/src/tests/recolor.rs @@ -2097,6 +2097,27 @@ fn reopening_a_group_over_a_preview_that_renames_it_finds_the_group() { dialogs::cancel(&mut app); } +/// A `library` or `group` that is not a string is an error, with or without a preset, and opens +/// no dialog. +#[test] +fn recolor_rejects_a_library_or_group_that_is_not_a_string() { + let mut app = app(); + art(&mut app, &["#ff0000", "#0000ff"]); + for p in [ + json!({"colors": 1, "library": 123}), + json!({"preset": true, "colors": 1, "library": 123}), + json!({"preset": true, "colors": 1, "library": true}), + json!({"group": 5}), + json!({"group": ["Brights"]}), + ] { + assert!(app.run("ui.recolorDialog", p.clone()).is_err(), "{p}"); + assert!(app.ui.dialog.is_none(), "{p} opens no dialog"); + } + app.run("ui.recolorDialog", json!({"colors": 1, "library": null, "group": null})).unwrap(); + assert_eq!(field(&app, "limitTo"), json!(""), "a null library is no limit"); + dialogs::cancel(&mut app); +} + /// Every paint under `v`, in order: its color, the swatch it is linked to and its tint. fn paints_in(v: &Value, out: &mut Vec<(Color, Option, f64)>) { match v {