Decode midiMap resiliently so a bad map can't drop the whole rig

Root cause of the "highlight right, wrong graph" restart: the autosave held a
midiMap written by an older binding schema (CCTarget was a struct before it
became an enum). Decoding threw, loadRig threw, RigStore.restore failed, and the
app fell back to seeding a single default board — so the graph showed Default
while the highlight (correctly) showed the loaded rig.

ShoeGazeState now decodes midiMap with try?, so an unreadable/incompatible map
drops just the (re-learnable) bindings while version + boards decode strictly.
The rig survives.

Test: a malformed midiMap decodes to nil without throwing. 324 green; both app
targets build.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-07-01 14:10:00 -07:00
parent dee1c29744
commit 4fef5aed7b
2 changed files with 28 additions and 0 deletions
@@ -49,4 +49,22 @@ public struct ShoeGazeState: Codable, Equatable {
self.boards = boards
self.midiMap = midiMap
}
private enum CodingKeys: String, CodingKey {
case version
case boards
case midiMap
}
/// Decodes a rig snapshot, tolerating an absent OR malformed `midiMap`.
///
/// The MIDI map is decoded with `try?` so a binding format that this build can't read (e.g. an
/// autosave written by an older schema) drops just the bindings — which are re-learnable —
/// rather than throwing and losing the entire rig. `version` and `boards` still decode strictly.
public init(from decoder: Decoder) throws {
let container: KeyedDecodingContainer<CodingKeys> = try decoder.container(keyedBy: CodingKeys.self)
self.version = try container.decode(Int.self, forKey: .version)
self.boards = try container.decode([BoardState].self, forKey: .boards)
self.midiMap = try? container.decodeIfPresent(MIDIMap.self, forKey: .midiMap) ?? nil
}
}