// BoardState.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT /// Serialisable snapshot of one `PedalBoard`'s label and full signal-chain topology. /// /// `label` is the board's display name. `segments` captures the chain in signal-flow order: /// linear pedals interleaved with parallel and stereo split regions (each region carries its /// branch pedal chains and routing mode). Unknown pedal types are skipped silently on load. /// /// ## Backward compatibility /// /// Presets written before topology capture stored a flat `pedals` array instead of `segments`. /// Such files still decode: the legacy `pedals` array is wrapped as `.pedal` segments. New /// files encode `segments` only. The computed ``pedals`` property exposes the linear pedal /// subset (the whole chain for boards with no split regions) for callers that only need it. public struct BoardState: Codable, Equatable { /// The board's display label (e.g. `"Board 1"`). public let label: String /// Ordered signal-chain segments (pedals + split regions) in signal-flow order. public let segments: [BoardSegmentState] /// The linear pedal subset of the chain — the `.pedal` segments only, in order. For a /// board with no split regions this is the entire chain. public var pedals: [PedalState] { segments.compactMap { segment in if case .pedal(let state) = segment { return state } return nil } } /// Creates a board state snapshot from an ordered segment list. /// /// - Parameters: /// - label: The board's display label. /// - segments: Ordered signal-chain segments. public init( label: String, segments: [BoardSegmentState] ) { self.label = label self.segments = segments } /// Creates a board state snapshot from a flat pedal list (no split regions). /// /// Each pedal becomes a `.pedal` segment. Convenience for linear boards and the legacy /// call shape. /// /// - Parameters: /// - label: The board's display label. /// - pedals: Ordered list of pedal state snapshots. public init( label: String, pedals: [PedalState] ) { self.label = label self.segments = pedals.map { BoardSegmentState.pedal($0) } } private enum CodingKeys: String, CodingKey { case label case segments case pedals } /// Decodes a snapshot, tolerating legacy presets that stored a flat `pedals` array by /// wrapping each entry as a `.pedal` segment. public init(from decoder: any Decoder) throws { let container: KeyedDecodingContainer = try decoder.container(keyedBy: CodingKeys.self) self.label = try container.decode(String.self, forKey: .label) if let decodedSegments: [BoardSegmentState] = try container.decodeIfPresent([BoardSegmentState].self, forKey: .segments) { self.segments = decodedSegments } else if let legacyPedals: [PedalState] = try container.decodeIfPresent([PedalState].self, forKey: .pedals) { // Legacy file: a flat pedal list with no split regions. self.segments = legacyPedals.map { BoardSegmentState.pedal($0) } } else { // Neither key present — a malformed file. Fail loudly rather than silently // producing an empty board. throw DecodingError.dataCorruptedError( forKey: CodingKeys.segments, in: container, debugDescription: "BoardState requires either 'segments' or legacy 'pedals'." ) } } /// Encodes the snapshot using the current `segments` representation. public func encode(to encoder: any Encoder) throws { var container: KeyedEncodingContainer = encoder.container(keyedBy: CodingKeys.self) try container.encode(label, forKey: .label) try container.encode(segments, forKey: .segments) } }