Keep ATN event codes distinct from TEXT names

This commit is contained in:
yoriswind committed 2026-10-31 21:41:03 -07:00
1 parent 0a605880dd
commit c5fc78bee1
3 files changed
+65 -11

No files matched your search

+8 -5
View File
@@ -649,11 +649,14 @@ const LEGACY_CODES: &[(&str, &str)] =
pub(super) fn event_for(item: &AtnEvent) -> Option<&'static Event> {
let id = item.id();
let find = |id: &str| EVENTS.iter().find(|e| (!e.code.is_empty() && e.code.trim_end() == id.trim_end()) || (!e.name.is_empty() && e.name == id));
find(&id).or_else(|| match item {
AtnEvent::Code(_) => LEGACY_CODES.iter().find(|(code, _)| *code == id).and_then(|(_, name)| find(name)),
AtnEvent::Name(_) => None,
})
let by_name = |name: &str| EVENTS.iter().find(|e| !e.name.is_empty() && e.name == name);
match item {
AtnEvent::Code(_) => EVENTS
.iter()
.find(|e| !e.code.is_empty() && e.code.trim_end() == id.trim_end())
.or_else(|| LEGACY_CODES.iter().find(|(code, _)| *code == id).and_then(|(_, name)| by_name(name))),
AtnEvent::Name(_) => by_name(&id),
}
}
/// The event `id` exports as: the first event listing it whose converter accepts the params
@@ -27,6 +27,57 @@ fn flare(brightness: i32, center: [f64; 2], lens: &str) -> Descriptor {
.with("Lns ", enm("Lns ", lens))
}
/// Codes and TEXT names have separate identities, including the old Code-only aliases.
#[test]
fn event_lookup_keeps_codes_separate_from_names() {
for (code, id) in [
(*b"Clds", "filter.render.clouds"),
(*b"DfrC", "filter.render.differenceClouds"),
(*b"Fbrs", "filter.render.fibers"),
(*b"LnsF", "filter.render.lensFlare"),
(*b"BoxB", "filter.blur.boxBlur"),
(*b"SrfB", "filter.blur.surfaceBlur"),
(*b"Fltn", "layer.flattenImage"),
(*b"BlnW", "image.adjustments.blackWhite"),
(*b"PhtF", "image.adjustments.photoFilter"),
] {
assert!(event_for(&AtnEvent::Code(code)).unwrap().ids.contains(&id));
assert!(event_for(&AtnEvent::Name(code.to_vec())).is_none(), "{code:?} is not a TEXT name");
}
assert!(event_for(&AtnEvent::name("play")).unwrap().ids.contains(&"actions.play"));
assert!(event_for(&AtnEvent::Code(*b"Ply ")).unwrap().ids.contains(&"actions.play"));
assert!(event_for(&AtnEvent::Code(*b"play")).is_none(), "a TEXT name is not a code");
for event in [AtnEvent::Code(*b"save"), AtnEvent::name("save")] {
assert!(event_for(&event).unwrap().ids.contains(&"file.save"), "save is registered in both forms");
}
assert!(event_for(&AtnEvent::name("")).is_none(), "empty registry names are not event identifiers");
}
/// Unknown event forms stay kept rather than becoming commands, and save verbatim.
#[test]
fn code_spellings_in_text_steps_keep_their_bytes_and_flags() {
let mut events: Vec<_> = ["Clds", "DfrC", "Fbrs", "LnsF", "BoxB", "SrfB", "Fltn", "BlnW", "PhtF"].into_iter().map(AtnEvent::name).collect();
events.push(AtnEvent::Code(*b"play"));
for event in events {
let mut original = atn::parse(&file(event, desc("null"))).unwrap();
original.actions[0].items[0].enabled = false;
original.actions[0].items[0].with_dialog = true;
original.actions[0].items[0].dialog_options = 2;
let bytes = atn::write(&original).unwrap();
let mut s = Session::new();
let loaded = s.execute("actions.load", json!({"data": photocraft_paint::tile::b64_encode(&bytes)})).unwrap();
assert_eq!(loaded["unsupported"].as_array().unwrap().len(), 1, "{:?}", original.actions[0].items[0].event);
assert_eq!(s.actions.list[0].steps[0].0, PHOTOSHOP_STEP);
let saved = s.execute("actions.save", json!({"action": "Render"})).unwrap();
assert_eq!(saved["skipped"], json!([]));
let back = atn::parse(&photocraft_paint::tile::b64_decode(saved["data"].as_str().unwrap()).unwrap()).unwrap();
assert_eq!(back.actions[0].items, original.actions[0].items);
let mut compared = original;
compared.actions[0].items = back.actions[0].items.clone();
assert_eq!(atn::write(&compared).unwrap(), bytes);
}
}
/// Both recorded event IDs map empty Clouds descriptors without inventing frozen colors.
#[test]
fn clouds_events_map_and_refuse_unrepresentable_native_settings() {
+6 -6
View File
@@ -102,13 +102,13 @@ fn sample() -> AtnFile {
function_key: 3,
items: vec![
blur,
item("make", "Make", Some(make_levels)),
item("Mk ", "Make", Some(make_levels)),
item("setd", "Set", Some(select_all)),
item("select", "Select", Some(select_layer)),
item("slct", "Select", Some(tool)),
item("stop", "Stop", Some(stop)),
item("Stop", "Stop", Some(stop)),
item("syntheticFill", "Generative Fill", Some(fancy)),
item("play", "Play", Some(play)),
item("Ply ", "Play", Some(play)),
off,
],
..Default::default()
@@ -1051,7 +1051,7 @@ fn retouching_set_events_map_to_their_commands() {
item("colorRange", "Color Range", Some(desc("null").with("Clrs", enm("Clrs", "skinTone")).with("Fzns", long(40.0)))),
item("colorRange", "Color Range", Some(sampled)),
item("colorRange", "Color Range", Some(desc("null").with("Clrs", enm("Clrs", "Rds ")).with("UsFc", D::Boolean(true)))),
item("make", "Make", Some(mask)),
item("Mk ", "Make", Some(mask)),
item("set", "Set", Some(fg)),
item("set", "Set", Some(lab_fg)),
item("groupEvent", "Create Clipping Mask", Some(desc("null").with("null", target_ref("Lyr ")))),
@@ -1434,8 +1434,8 @@ fn remaining_events_import_in_their_photoshop_shapes() {
let items = vec![
item("conditional", "If", Some(cond)),
item("imageSize", "Image Size", Some(size)),
item("fill", "Fill", Some(bucket_px)),
item("fill", "Fill", Some(bucket_pct)),
item("Fl ", "Fill", Some(bucket_px)),
item("Fl ", "Fill", Some(bucket_pct)),
item("placeEvent", "Place", Some(place)),
item("Opn ", "Open", Some(desc("null").with("null", path_value("/Users/me/in.psd")))),
];