Save for Web: a shift map over 256 entries is kept whole

A `shifted` map with more than 256 entries is one the engine refuses, so
the shift edit no longer drops its self-shifts and deleted colors. A shift
that would leave more than 256 colors shifted, including a new target for
a color that is already shifted, is refused with the limit error.
This commit is contained in:
dotCooCoo
2026-11-24 20:19:58 -08:00
parent c5c54e15f2
commit b9f6233c69
2 changed files with 31 additions and 12 deletions
@@ -196,30 +196,33 @@ fn delete(table: &mut Value, picked: &[String], shown: &[[u8; 3]]) -> Result<(),
}
/// Shift the source colour `from` to `to`, wherever it is in the image; it is no longer
/// web-shifted. Shifting a colour to itself takes its shift away. A new source when 256 colours
/// are already shifted is refused with the engine's limit error, and nothing changes. Entries the
/// web-shifted. Shifting a colour to itself takes its shift away. A shift that would leave more
/// than 256 colours shifted is refused with the engine's limit error, and nothing changes. Entries the
/// engine drops (a colour shifted to itself, a deleted colour) do not count, and a new shift writes
/// the map without them.
pub(crate) fn shift(table: &mut Value, from: [u8; 3], to: [u8; 3]) -> Result<(), String> {
let (source, to) = (hex(from), hex(to));
let deleted: Vec<[u8; 3]> = list(table, "deleted").iter().filter_map(|c| parse(c)).collect();
let map = shifts(table);
// Entries are dropped only from a map the engine accepts (every key and value a colour, no two
// keys naming one colour); any other map is kept whole for the engine to refuse.
// Entries are dropped only from a map the engine accepts (at most 256 entries, every key and
// value a colour, no two keys naming one colour); any other map is kept whole for the engine to
// refuse.
let mut keys: Vec<[u8; 3]> = Vec::new();
let valid = map.iter().all(|(k, v)| match (parse(k), v.as_str().and_then(parse)) {
(Some(k), Some(_)) if !keys.contains(&k) => {
keys.push(k);
true
}
_ => false,
});
let valid = map.len() <= webexport::MAX_TABLE
&& map.iter().all(|(k, v)| match (parse(k), v.as_str().and_then(parse)) {
(Some(k), Some(_)) if !keys.contains(&k) => {
keys.push(k);
true
}
_ => false,
});
let live = |k: &str, v: &Value| {
let (true, Some(key), Some(to)) = (valid, parse(k), v.as_str().and_then(parse)) else { return true };
to != key && !deleted.contains(&key)
};
let count = map.iter().filter(|(k, v)| live(k, v)).count();
if to != source && count >= webexport::MAX_TABLE && !map.iter().any(|(k, v)| live(k, v) && same(k, &source)) {
let held = usize::from(map.iter().any(|(k, v)| live(k, v) && same(k, &source)));
if to != source && count - held >= webexport::MAX_TABLE {
return Err(webexport::too_many("shifted"));
}
unshift(table, &source);
@@ -148,6 +148,22 @@ fn a_shift_skips_entries_the_engine_drops() {
}
}
/// A map with more than 256 entries is one the engine refuses, so none of its entries are dropped:
/// with 257 colours shifted to themselves, a new shift and a new target for a shifted colour are
/// refused with the limit error and nothing changes. Taking a shift away still works.
#[test]
fn a_shift_keeps_a_map_over_the_limit_whole() {
let over: serde_json::Map<String, Value> = (0..257).map(|i| (format!("#{i:06x}"), json!(format!("#{i:06x}")))).collect();
let mut t = json!({"shifted": over});
let before = t.clone();
let e = shift(&mut t, [0x12, 0x34, 0x56], A).unwrap_err();
assert_eq!((e, &t), (webexport::too_many("shifted"), &before));
let e = shift(&mut t, [0, 0, 5], [0x12, 0x34, 0x56]).unwrap_err();
assert_eq!((e, &t), (webexport::too_many("shifted"), &before));
shift(&mut t, [0, 0, 5], [0, 0, 5]).unwrap();
assert_eq!(t["shifted"].as_object().map(|m| m.len()), Some(256));
}
/// A key or list entry written another way (`#F00`, upper case, `blue`) still names its colour:
/// a new shift replaces it, Unshift and the buttons take it out, the marks show it, and a
/// spelling with alpha names no colour.