Add board edit API: lane-addressed pedal/region editing

Core support for interactively building and editing a board, all via the
existing capture-BoardState -> mutate -> rebuildBoard path (no RT-graph
surgery):

- PedalLane addresses the main chain or a stereo/parallel branch.
- appendPedal/removePedal/movePedal gain a `lane:` parameter (default
  .main, so existing call sites are unchanged).
- appendStereoSplit/appendParallelSplit/appendMergeToMono/removeSegment
  insert and remove branch regions; split/merge guarded by tail width.
- setInputSource preserves the rig across an input-source change.
- rebuildAllBoards / loadRig(from:Data) / rigData added; loadRig is now
  atomic (teardown-on-failure) so a failed load leaves an empty rig.
- Expose PedalBoard.orderedSegments and BoardSegment publicly so a UI can
  walk live board structure; index aligns with captureState().segments.
- InputSource: Equatable. New errors: unknownPedalType,
  pedalIndexOutOfRange, cannotSplitStereoChain, cannotMergeMonoChain,
  notABranchRegion.

Tests cover append/remove/move on main and branch lanes, region
add/remove, width guards, move-to-end before trailing regions, input
switch, and knob/bypass/branch value persistence through save/load.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-06-22 09:31:57 -07:00
parent 0960d4e7c5
commit e1ec98413d
8 changed files with 886 additions and 22 deletions
@@ -31,10 +31,65 @@ extension ShoeGaze {
/// - Parameter url: Destination file URL. The directory must already exist.
/// - Throws: Any error from `JSONEncoder` or `Data.write(to:options:)`.
public func saveRig(to url: URL) throws {
try rigData().write(to: url, options: .atomic)
}
/// Encodes the current rig to pretty-printed JSON `Data`.
///
/// Same payload `saveRig(to:)` writes to disk; exposed for callers that need the bytes
/// directly (e.g. a SwiftUI `fileExporter` `FileDocument` on iOS).
///
/// - Returns: The encoded `ShoeGazeState` JSON.
/// - Throws: Any error from `JSONEncoder`.
public func rigData() throws -> Data {
let encoder: JSONEncoder = JSONEncoder()
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
let data: Data = try encoder.encode(captureState())
try data.write(to: url, options: .atomic)
return try encoder.encode(captureState())
}
/// Loads a rig from in-memory JSON `Data` and rebuilds the engine graph.
///
/// Identical to ``loadRig(from:)`` but reads from `Data` rather than a file URL — convenient
/// for a SwiftUI `fileImporter` that has already read the document, or for tests.
///
/// - Important: Call with the engine **stopped**, as with ``loadRig(from:)``.
/// - Parameter data: JSON produced by ``rigData()`` / ``saveRig(to:)``.
/// - Throws: `JSONDecoder` errors, `PresetError.unsupportedVersion`, or any board-build error.
public func loadRig(from data: Data) throws {
let decoder: JSONDecoder = JSONDecoder()
let state: ShoeGazeState = try decoder.decode(ShoeGazeState.self, from: data)
guard state.version <= presetFormatVersion else {
throw PresetError.unsupportedVersion(state.version)
}
teardown()
try rebuildAllBoards(from: state)
}
/// Rebuilds every board from a snapshot with all-or-nothing semantics.
///
/// The caller is responsible for tearing down any existing rig first. On any failure while
/// reconstructing a board, the partially-built rig is torn down and the error rethrown, so
/// callers never observe a half-built rig — a board that fails on iteration N does not leave
/// boards `0..<N` stranded.
///
/// `internal` so `setInputSource(_:)` in `ShoeGaze+Edit.swift` shares the same atomic path.
///
/// - Parameter snapshot: The rig snapshot to reconstruct.
/// - Throws: `BusAllocatorError`, `EngineAddError`, or `ShoeGazeError` from board construction.
func rebuildAllBoards(from snapshot: ShoeGazeState) throws {
do {
for boardState: BoardState in snapshot.boards {
let board: PedalBoard = try addBoard()
board.label = boardState.label
try rebuildSegments(boardState.segments, into: board)
try finalizeBoard(board)
}
} catch {
// Leave no partial rig behind: tear down whatever was built, then rethrow.
teardown()
throw error
}
}
/// Loads a rig from a JSON preset file and rebuilds the engine graph.
@@ -57,21 +112,7 @@ extension ShoeGaze {
/// `PresetError.unsupportedVersion` when the file's version exceeds
/// `presetFormatVersion`.
public func loadRig(from url: URL) throws {
let data: Data = try Data(contentsOf: url)
let decoder: JSONDecoder = JSONDecoder()
let state: ShoeGazeState = try decoder.decode(ShoeGazeState.self, from: data)
guard state.version <= presetFormatVersion else {
throw PresetError.unsupportedVersion(state.version)
}
teardown()
for boardState: BoardState in state.boards {
let board: PedalBoard = try addBoard()
board.label = boardState.label
try rebuildSegments(boardState.segments, into: board)
try finalizeBoard(board)
}
try loadRig(from: try Data(contentsOf: url))
}
// MARK: - Segment reconstruction
@@ -84,7 +125,10 @@ extension ShoeGaze {
/// - segments: The ordered segment snapshots to reconstruct.
/// - board: The board to populate (freshly created, before `finalizeBoard`).
/// - Throws: `BusAllocatorError.noFreeBus` or `EngineAddError` from any node insertion.
private func rebuildSegments(_ segments: [BoardSegmentState], into board: PedalBoard) throws {
///
/// `internal` (not `private`) so the linear-edit and input-switch helpers in
/// `ShoeGaze+Edit.swift` can replay a captured topology onto a freshly created board.
func rebuildSegments(_ segments: [BoardSegmentState], into board: PedalBoard) throws {
for segment: BoardSegmentState in segments {
switch segment {
case .pedal(let state):