// ShoeGazeState.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT /// Format version written into every saved preset file. /// /// Increment this constant when the `ShoeGazeState` schema changes in a way that /// requires migration logic on load. public let presetFormatVersion: Int = 1 /// Serialisable snapshot of an entire `ShoeGaze` rig — all boards and their pedals. /// /// Use `ShoeGaze.saveRig(to:)` to write and `ShoeGaze.loadRig(from:)` to restore. /// The `version` field allows future migrations: loaders that read a file with a /// higher `version` than `presetFormatVersion` should reject or warn, while loaders /// reading older versions may apply forward-compatibility mappings. /// /// Unknown pedal types encountered during load are silently skipped so presets made /// with custom third-party pedals degrade gracefully on systems without those pedals. public struct ShoeGazeState: Codable, Equatable { /// Preset format version. Written as `presetFormatVersion` at save time. public let version: Int /// All board snapshots in the order they were created. public let boards: [BoardState] /// The rig's MIDI foot-controller bindings, or `nil` for a rig saved without any. /// /// Optional so rigs written before MIDI mapping existed decode cleanly (the synthesized /// `init(from:)` uses `decodeIfPresent`, yielding `nil` for the missing key) — no format /// version bump is required for this additive field. public let midiMap: MIDIMap? /// Creates a rig state snapshot. /// /// - Parameters: /// - version: Preset format version. Defaults to `presetFormatVersion`. /// - boards: Ordered list of board state snapshots. /// - midiMap: The rig's MIDI bindings, or `nil` when there are none. Defaults to `nil`. public init( version: Int = presetFormatVersion, boards: [BoardState], midiMap: MIDIMap? = nil ) { self.version = version 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 = 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 } }