Load and replace Action sets from browser-picked contents

This commit is contained in:
dotCooCoo committed 2026-11-26 16:15:31 -08:00
1 parent 5c7bbeecaa
commit 826248f56a
2 files changed
+67 -8

No files matched your search

+21 -8
View File
@@ -20,7 +20,7 @@
//! `actionSet.read`), a Set Selection step as a Select Object step.
use craft::automation::actions::{Action, ActionColor, ActionStep, ActionsFile};
use craft::host::{PickKind, PickRequest}; // PLAN: assumed path of the host's picker types in the app's `craft` facade
use craft::host::{PickKind, PickRequest, Picked}; // PLAN: assumed path of the host's picker types in the app's `craft` facade
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
@@ -577,15 +577,20 @@ fn read_path(app: &mut VectorcraftApp, path: &str) -> Result<FileRead, String> {
(None, Some(read)) => read(path)?,
(None, None) => return Err("no file reader".into()),
};
read_bytes(app, path, &bytes)
}
/// A desktop or browser file's contents, with its name used only as the unnamed set's fallback.
fn read_bytes(app: &mut VectorcraftApp, name: &str, bytes: &[u8]) -> Result<FileRead, String> {
if bytes.len() > MAX_SET_FILE {
return Err("the action set file is larger than 8 MiB".into());
}
if is_aia(&bytes) {
return read_aia(app, json!({ "name": path, "dataBase64": vectorcraft_format::base64_encode(&bytes) }));
if is_aia(bytes) {
return read_aia(app, json!({ "name": name, "dataBase64": vectorcraft_format::base64_encode(bytes) }));
}
let stem = std::path::Path::new(path).file_stem().map(|s| s.to_string_lossy().into_owned()).unwrap_or_default();
let text = String::from_utf8(bytes).map_err(|_| "not an action set file")?;
let (name, actions) = read_set(&text, if stem.is_empty() { "Actions" } else { &stem })?;
let stem = std::path::Path::new(name).file_stem().map(|s| s.to_string_lossy().into_owned()).unwrap_or_default();
let text = std::str::from_utf8(bytes).map_err(|_| "not an action set file")?;
let (name, actions) = read_set(text, if stem.is_empty() { "Actions" } else { &stem })?;
Ok(FileRead { name, actions, aia: None })
}
@@ -613,8 +618,16 @@ fn read_file(
let exts = [&[ACTIONS_EXT], vectorcraft_engine::cmd::actionset::EXTS].concat();
let request = PickRequest::open("actions").filter("Action sets", &exts);
Ok(file_asks::ask(app, request, Owner::Free, move |app, picked| {
let path = picked.first_path().ok_or("cancelled")?;
let read = read_path(app, &path.to_string_lossy())?;
let read = match picked {
Picked::Contents(files) => {
let (name, bytes) = files.into_iter().next().ok_or("cancelled")?;
read_bytes(app, &name, &bytes)?
}
other => {
let path = other.first_path().ok_or("cancelled")?;
read_path(app, &path.to_string_lossy())?
}
};
then(app, read)
}))
}
@@ -7,6 +7,52 @@ use super::tests_support::*;
use super::*;
use crate::dialogs::action_batch::BatchDialog;
#[test]
fn browser_picks_load_and_replace_action_sets_without_a_file_reader() {
let native = json!({
"format": "vcactions", "version": 1, "name": "Browser",
"actions": [{"name": "Group", "steps": [{"command": "object.group", "params": {}}]}]
})
.to_string();
let mut aia = aia_text("", &[("Group", &[("group", true)])]).into_bytes();
// AIA's text encoding is not required to be UTF-8.
aia.extend(b"/note (\xFF)\n");
for command in [LOAD_SET, REPLACE_SET] {
for (name, bytes) in [("Browser.vcactions", native.as_bytes()), ("Browser.aia", aia.as_slice())] {
let mut app = app_with_sets();
app.run("file.new", json!({"width": 200, "height": 100})).unwrap();
let picker = crate::tests::picks::scripted(&mut app);
app.services.read = None;
app.services.read_head = None;
let before = saved_sets(&app);
let undo = undo_steps(&app);
app.run(command, json!({})).unwrap();
picker.cancel();
crate::file_asks::poll(&mut app);
assert_eq!(saved_sets(&app), before, "cancel keeps the sets");
for invalid in [vec![0xff], vec![b' '; files::MAX_SET_FILE + 1]] {
app.run(command, json!({})).unwrap();
picker.answer_contents(&[(name, &invalid)]);
crate::file_asks::poll(&mut app);
assert_eq!(saved_sets(&app), before, "invalid picked bytes keep the sets");
assert!(!app.ui.status.is_empty(), "a rejected file reports why");
}
app.run(command, json!({})).unwrap();
picker.answer_contents(&[(name, bytes)]);
crate::file_asks::poll(&mut app);
let names = set_names(&app);
assert_eq!(names.last().map(String::as_str), Some("Browser"));
if command == REPLACE_SET {
assert_eq!(names, ["Browser"]);
} else {
assert!(names.len() > 1, "Load keeps existing sets");
}
assert_eq!(set_actions(&app, names.len() - 1)[0].steps, [ActionStep::new("object.group", json!({}))]);
assert_eq!(undo_steps(&app), undo, "loading actions does not edit the drawing");
}
}
}
#[test]
fn defaults_are_valid_commands() {
for a in default_sets().actions {