Recolor Artwork: reject a library or group that is not a string

ui.recolorDialog, with or without a preset, treated a `library` or
`group` of another type as omitted, so a preset opened with no limit and
OK could recolor the art without the library that was asked for. Both
are now an error that opens no dialog. Null still means none.
This commit is contained in:
dotCooCoo
2026-11-22 16:13:38 -08:00
parent b7c98147e9
commit eaf1c48e5e
2 changed files with 33 additions and 6 deletions
+12 -6
View File
@@ -199,7 +199,11 @@ pub fn open(app: &mut VectorcraftApp, p: &Value) -> Result<Value, String> {
}
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<String>, Vec<String>
(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<String, String> {
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()),
}
}
+21
View File
@@ -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<String>, f64)>) {
match v {