Type: Kinsoku Shori Settings keeps a sets field that is not a list of sets through a frame

A frame of the dialog wrote the sets it could read back into the `sets`
field, so a null value became an empty list and entries that are not
objects were dropped. OK then sent that list, and the document lost its
custom sets. The dialog now leaves such a field as it is on every frame,
New, Delete and Add refuse it, and OK sends it to type.kinsokuSets.set,
which refuses it while the document keeps its sets.
This commit is contained in:
dotCooCoo
2026-11-15 07:18:22 -08:00
parent fb90668e8a
commit 4cc6e321e0
2 changed files with 31 additions and 11 deletions
+14 -4
View File
@@ -73,6 +73,12 @@ fn sets(d: &Dialog) -> Vec<Map<String, Value>> {
d.fields.get("sets").and_then(Value::as_array).map(|a| a.iter().filter_map(|v| v.as_object().cloned()).collect()).unwrap_or_default()
}
/// Is the `sets` field a list of sets? One that is not (set with `ui.dialog.set`) is kept as it is:
/// no frame or button replaces it, and OK sends it to the command, which refuses it.
fn holds_sets(d: &Dialog) -> bool {
d.fields.get("sets").and_then(Value::as_array).is_some_and(|a| a.iter().all(Value::is_object))
}
fn put_sets(d: &mut Dialog, sets: &[Map<String, Value>]) {
d.fields.insert("sets".into(), json!(sets));
}
@@ -242,7 +248,9 @@ fn body(app: &mut VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog) -> bool {
}
d.fields.insert("shown".into(), Value::Object(set.clone()));
}
put_sets(d, &list);
if holds_sets(d) {
put_sets(d, &list);
}
if let Some(a) = act
&& let Err(e) = run(app, d, a)
{
@@ -253,6 +261,9 @@ fn body(app: &mut VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog) -> bool {
/// Do what button `act` asks for on the dialog's sets.
fn run(app: &mut VectorcraftApp, d: &mut Dialog, act: Action) -> Result<(), String> {
if !holds_sets(d) {
return Err("the sets must be a list of Kinsoku sets".into());
}
let mut list = sets(d);
let i = selected(d, list.len());
match act {
@@ -325,8 +336,7 @@ fn confirm(app: &mut VectorcraftApp, d: &Dialog) -> Result<Value, String> {
app.run("document.activate", json!({ "index": index }))?;
}
// A `sets` field that is not a list of sets goes to the command as it is, which refuses it.
let raw = d.fields.get("sets").cloned().unwrap_or(Value::Null);
let sets = if raw.as_array().is_some_and(|a| a.iter().all(Value::is_object)) {
let sets = if holds_sets(d) {
// A set the document gained while the dialog was open (a script, another command) is
// kept, after the dialog's sets.
let opened: Vec<&str> =
@@ -336,7 +346,7 @@ fn confirm(app: &mut VectorcraftApp, d: &Dialog) -> Result<Value, String> {
}
json!(list)
} else {
raw
d.fields.get("sets").cloned().unwrap_or(Value::Null)
};
let r = app.run(COMMAND, json!({ "sets": sets }));
// A name two sets share, a reserved name or a category too long keeps the dialog open to be
@@ -246,7 +246,8 @@ fn ok_sets_the_document_the_dialog_opened_on() {
/// OK keeps a set the document gained while the dialog was open, after the dialog's sets, and a
/// set deleted in the dialog stays deleted. A `sets` field that is not a list of sets keeps the
/// dialog open and leaves the document's sets as they are.
/// dialog open and leaves the document's sets as they are, also when a frame is drawn before OK,
/// and New refuses to start from it.
#[test]
fn ok_keeps_sets_added_while_open_and_refuses_malformed_sets() {
let mut app = app();
@@ -264,12 +265,21 @@ fn ok_keeps_sets_added_while_open_and_refuses_malformed_sets() {
super::super::confirm(&mut app).unwrap();
let names = |app: &VectorcraftApp| doc_sets(app).iter().map(|k| k.name.clone()).collect::<Vec<_>>();
assert_eq!(names(&app), ["Later"]);
for bad in [Value::Null, json!({}), json!("Later"), json!([1])] {
open(&mut app).unwrap();
dialog(&mut app).fields.insert("sets".into(), bad.clone());
assert!(super::super::confirm(&mut app).is_err(), "{bad}");
assert!(app.ui.dialog.is_some(), "{bad}");
assert_eq!(names(&app), ["Later"], "{bad}");
for bad in [Value::Null, json!({}), json!("Later"), json!([1]), json!([{"name": "Later", "cannotStart": "ー"}, 1])] {
for drawn in [false, true] {
open(&mut app).unwrap();
dialog(&mut app).fields.insert("sets".into(), bad.clone());
if drawn {
frame(&mut app);
assert_eq!(dialog(&mut app).fields.get("sets"), Some(&bad), "a frame keeps {bad} as it is");
let mut d = app.ui.dialog.clone().unwrap();
assert!(run(&mut app, &mut d, Action::New).is_err(), "{bad}");
assert_eq!(d.fields.get("sets"), Some(&bad), "New keeps {bad} as it is");
}
assert!(super::super::confirm(&mut app).is_err(), "{bad} drawn: {drawn}");
assert!(app.ui.dialog.is_some(), "{bad} drawn: {drawn}");
assert_eq!(names(&app), ["Later"], "{bad} drawn: {drawn}");
}
}
}