Add .shoegaze document type and rig/board sharing
Introduce a single shareable document type for rigs and boards: - ShoeGazeDocument: versioned JSON envelope wrapping either a RigArchive (one or many rigs) or a BoardVersion, with a tolerant decoder that also reads the legacy bare-RigArchive and bare-ShoeGazeState shapes. - Register the .shoegaze UTType (net.realizedsound.shoegaze, conforms to public.json) in all four demo app Info.plists. - Migrate library export/import onto the envelope: exportDocumentData() and a decoder-tolerant importArchive(); RigFileDocument and the Builder exporter/importer now use .shoegaze. Internal per-rig and board-version files stay app-private JSON. - Add OS share-sheet entries via SwiftUI ShareLink: Share per rig in the library sidebar, Share per version in the board editor, backed by the Transferable wrappers RigShareItem / BoardShareItem. Tests: ShoeGazeDocument round-trip and legacy decode, library .shoegaze export/import round-trip, and a board-document-imports-no-rigs case. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -278,22 +278,39 @@ public final class RigLibraryStore {
|
||||
|
||||
// MARK: - Archive import / export
|
||||
|
||||
/// Encodes the entire library as a single ``RigArchive`` JSON document for export.
|
||||
/// Encodes the entire library as a single ``RigArchive`` JSON document.
|
||||
///
|
||||
/// - Returns: Pretty-printed archive bytes suitable for a `fileExporter` / `NSSavePanel`.
|
||||
/// Retained for callers and tests that want the bare archive bytes. The user-facing
|
||||
/// export/share path uses ``exportDocumentData()``, which wraps the same rigs in a
|
||||
/// ``ShoeGazeDocument`` envelope (the `.shoegaze` file type).
|
||||
///
|
||||
/// - Returns: Pretty-printed archive bytes.
|
||||
/// - Throws: `JSONEncoder` errors.
|
||||
public func exportArchiveData() throws -> Data {
|
||||
let archive: RigArchive = RigArchive(rigs: entries)
|
||||
return try RigLibraryStore.makeEncoder().encode(archive)
|
||||
}
|
||||
|
||||
/// Encodes the entire library as a ``ShoeGazeDocument`` for export to a `.shoegaze` file.
|
||||
///
|
||||
/// This is the format written by the library's **Export** action and shared via the system
|
||||
/// share sheet. ``importArchive(from:)`` reads it back, and also still accepts the bare
|
||||
/// ``RigArchive`` and legacy single-rig shapes.
|
||||
///
|
||||
/// - Returns: Pretty-printed `.shoegaze` document bytes.
|
||||
/// - Throws: `JSONEncoder` errors.
|
||||
public func exportDocumentData() throws -> Data {
|
||||
try ShoeGazeDocument(rigs: entries).data()
|
||||
}
|
||||
|
||||
/// Imports rigs from archive `data`, saving each into the library, and returns how many were
|
||||
/// added.
|
||||
///
|
||||
/// Accepts either a ``RigArchive`` (the multi-rig export format) or a legacy single-rig file
|
||||
/// (a bare ``ShoeGazeState``), which is imported as one entry named `legacyRigName`. Imported
|
||||
/// entries whose `id` already exists in the library are given a fresh `id` so an import never
|
||||
/// silently overwrites an existing rig.
|
||||
/// Accepts a ``ShoeGazeDocument`` (the `.shoegaze` file type), a bare ``RigArchive`` (the prior
|
||||
/// multi-rig export), or a legacy single-rig file (a bare ``ShoeGazeState``), which is imported
|
||||
/// as one entry named `legacyRigName`. A document carrying a board rather than rigs contributes
|
||||
/// no entries. Imported entries whose `id` already exists in the library are given a fresh `id`
|
||||
/// so an import never silently overwrites an existing rig.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - data: Archive or legacy-rig JSON bytes.
|
||||
@@ -512,19 +529,15 @@ public final class RigLibraryStore {
|
||||
return decoder
|
||||
}
|
||||
|
||||
/// Decodes import bytes as a ``RigArchive`` first, falling back to a legacy single-rig
|
||||
/// ``ShoeGazeState``.
|
||||
/// Decodes import bytes into rig entries, accepting the `.shoegaze` ``ShoeGazeDocument``
|
||||
/// envelope plus the bare ``RigArchive`` and legacy ``ShoeGazeState`` shapes (see
|
||||
/// ``ShoeGazeDocument/decoded(from:legacyRigName:)``). A board-carrying document yields no rigs.
|
||||
private func decodeImportable(
|
||||
_ data: Data,
|
||||
legacyRigName: String
|
||||
) throws -> [RigEntry] {
|
||||
let decoder: JSONDecoder = RigLibraryStore.makeDecoder()
|
||||
if let archive: RigArchive = try? decoder.decode(RigArchive.self, from: data) {
|
||||
return archive.rigs
|
||||
}
|
||||
// A legacy single-rig file is a bare ShoeGazeState (no `rigs` array). Decode and wrap.
|
||||
let state: ShoeGazeState = try decoder.decode(ShoeGazeState.self, from: data)
|
||||
return [RigEntry(name: legacyRigName, state: state)]
|
||||
let document: ShoeGazeDocument = try ShoeGazeDocument.decoded(from: data, legacyRigName: legacyRigName)
|
||||
return document.rigEntries ?? []
|
||||
}
|
||||
|
||||
// MARK: - In-memory helpers
|
||||
|
||||
@@ -0,0 +1,201 @@
|
||||
// ShoeGazeDocument.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Foundation
|
||||
|
||||
/// Format version written into every `.shoegaze` document.
|
||||
///
|
||||
/// Increment when the `ShoeGazeDocument` envelope schema changes in a way that requires
|
||||
/// migration on load. The payload types (``RigArchive`` / ``BoardVersion``) carry their own
|
||||
/// independent version fields.
|
||||
public let shoeGazeDocumentFormatVersion: Int = 1
|
||||
|
||||
/// The portable `.shoegaze` document — the single file type ShoeGaze writes when a user shares
|
||||
/// or exports their work, and reads when opening a shared file.
|
||||
///
|
||||
/// A document is a small versioned envelope around one of two payloads:
|
||||
///
|
||||
/// - ``Payload/rigs(_:)`` — one or more rigs, carried as a ``RigArchive`` (a single shared rig is
|
||||
/// a one-rig archive; the whole-library export is a many-rig archive).
|
||||
/// - ``Payload/board(_:)`` — a single named board snapshot (``BoardVersion``).
|
||||
///
|
||||
/// Files are written pretty-printed with sorted keys and `secondsSince1970` dates, so they stay
|
||||
/// hand-readable and round-trip losslessly.
|
||||
///
|
||||
/// ## Backward compatibility
|
||||
///
|
||||
/// ``decoded(from:)`` accepts three on-disk shapes so older files keep opening:
|
||||
/// 1. A `ShoeGazeDocument` envelope (the current format).
|
||||
/// 2. A bare ``RigArchive`` (the previous multi-rig export).
|
||||
/// 3. A bare ``ShoeGazeState`` (the original single-rig save).
|
||||
///
|
||||
/// Shapes 2 and 3 are normalised into a ``Payload/rigs(_:)`` document on load.
|
||||
public struct ShoeGazeDocument: Codable, Equatable {
|
||||
|
||||
/// The two things a `.shoegaze` file can carry.
|
||||
public enum Payload: Equatable {
|
||||
|
||||
/// One or more rigs, as a portable archive.
|
||||
case rigs(RigArchive)
|
||||
|
||||
/// A single named board snapshot.
|
||||
case board(BoardVersion)
|
||||
}
|
||||
|
||||
/// Filename extension and the basis for the document's UTType (`net.realizedsound.shoegaze`).
|
||||
public static let fileExtension: String = "shoegaze"
|
||||
|
||||
/// Document envelope version. Written as `shoeGazeDocumentFormatVersion` at save time.
|
||||
public let version: Int
|
||||
|
||||
/// What this document carries.
|
||||
public let payload: Payload
|
||||
|
||||
/// Creates a document with an explicit payload.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - version: Envelope version. Defaults to `shoeGazeDocumentFormatVersion`.
|
||||
/// - payload: The rigs or board this document carries.
|
||||
public init(
|
||||
version: Int = shoeGazeDocumentFormatVersion,
|
||||
payload: Payload
|
||||
) {
|
||||
self.version = version
|
||||
self.payload = payload
|
||||
}
|
||||
|
||||
// MARK: - Convenience constructors
|
||||
|
||||
/// Creates a single-rig document by wrapping one ``RigEntry`` in a one-rig ``RigArchive``.
|
||||
///
|
||||
/// - Parameter entry: The rig to share.
|
||||
public init(rig entry: RigEntry) {
|
||||
self.init(payload: .rigs(RigArchive(rigs: [entry])))
|
||||
}
|
||||
|
||||
/// Creates a multi-rig document from a list of entries.
|
||||
///
|
||||
/// - Parameter entries: The rigs to bundle, in listing order.
|
||||
public init(rigs entries: [RigEntry]) {
|
||||
self.init(payload: .rigs(RigArchive(rigs: entries)))
|
||||
}
|
||||
|
||||
/// Creates a single-board document.
|
||||
///
|
||||
/// - Parameter version: The board snapshot to share.
|
||||
public init(board version: BoardVersion) {
|
||||
self.init(payload: .board(version))
|
||||
}
|
||||
|
||||
// MARK: - Payload accessors
|
||||
|
||||
/// The rigs this document carries, or `nil` when it carries a board.
|
||||
public var rigEntries: [RigEntry]? {
|
||||
if case .rigs(let archive) = payload { return archive.rigs }
|
||||
return nil
|
||||
}
|
||||
|
||||
/// The board this document carries, or `nil` when it carries rigs.
|
||||
public var boardVersion: BoardVersion? {
|
||||
if case .board(let version) = payload { return version }
|
||||
return nil
|
||||
}
|
||||
|
||||
// MARK: - Encoding
|
||||
|
||||
/// Encodes this document to pretty-printed, hand-editable `.shoegaze` JSON.
|
||||
///
|
||||
/// - Returns: The encoded document bytes.
|
||||
/// - Throws: Any error from `JSONEncoder`.
|
||||
public func data() throws -> Data {
|
||||
try ShoeGazeDocument.makeEncoder().encode(self)
|
||||
}
|
||||
|
||||
/// Decodes a `.shoegaze` document, tolerating the two legacy on-disk shapes.
|
||||
///
|
||||
/// Tries the `ShoeGazeDocument` envelope first, then a bare ``RigArchive``, then a bare
|
||||
/// ``ShoeGazeState`` — normalising the latter two into a rigs document. A bare state with no
|
||||
/// name becomes a one-rig archive named `legacyRigName`.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - data: The document bytes.
|
||||
/// - legacyRigName: Name applied when normalising a bare ``ShoeGazeState``. Defaults to
|
||||
/// `"Imported Rig"`.
|
||||
/// - Returns: A normalised document.
|
||||
/// - Throws: `JSONDecoder` errors when `data` matches none of the three shapes.
|
||||
public static func decoded(
|
||||
from data: Data,
|
||||
legacyRigName: String = "Imported Rig"
|
||||
) throws -> ShoeGazeDocument {
|
||||
let decoder: JSONDecoder = makeDecoder()
|
||||
if let document: ShoeGazeDocument = try? decoder.decode(ShoeGazeDocument.self, from: data) {
|
||||
return document
|
||||
}
|
||||
if let archive: RigArchive = try? decoder.decode(RigArchive.self, from: data) {
|
||||
return ShoeGazeDocument(payload: .rigs(archive))
|
||||
}
|
||||
// Original single-rig save: a bare ShoeGazeState. Let a genuine decode failure surface here.
|
||||
let state: ShoeGazeState = try decoder.decode(ShoeGazeState.self, from: data)
|
||||
return ShoeGazeDocument(rig: RigEntry(name: legacyRigName, state: state))
|
||||
}
|
||||
|
||||
/// A JSON encoder configured for human-readable, round-trip-stable document files.
|
||||
static func makeEncoder() -> JSONEncoder {
|
||||
let encoder: JSONEncoder = JSONEncoder()
|
||||
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
|
||||
encoder.dateEncodingStrategy = .secondsSince1970
|
||||
return encoder
|
||||
}
|
||||
|
||||
/// A JSON decoder matching ``makeEncoder()``.
|
||||
static func makeDecoder() -> JSONDecoder {
|
||||
let decoder: JSONDecoder = JSONDecoder()
|
||||
decoder.dateDecodingStrategy = .secondsSince1970
|
||||
return decoder
|
||||
}
|
||||
|
||||
// MARK: - Codable
|
||||
|
||||
/// Discriminator written into the JSON so a payload reads back unambiguously.
|
||||
private enum Kind: String, Codable {
|
||||
case rigs
|
||||
case board
|
||||
}
|
||||
|
||||
private enum CodingKeys: String, CodingKey {
|
||||
case version
|
||||
case kind
|
||||
case rigs
|
||||
case board
|
||||
}
|
||||
|
||||
/// Encodes the envelope with an explicit `kind` discriminator and the matching payload key.
|
||||
public func encode(to encoder: any Encoder) throws {
|
||||
var container: KeyedEncodingContainer<CodingKeys> = encoder.container(keyedBy: CodingKeys.self)
|
||||
try container.encode(version, forKey: .version)
|
||||
switch payload {
|
||||
case .rigs(let archive):
|
||||
try container.encode(Kind.rigs, forKey: .kind)
|
||||
try container.encode(archive, forKey: .rigs)
|
||||
case .board(let boardVersion):
|
||||
try container.encode(Kind.board, forKey: .kind)
|
||||
try container.encode(boardVersion, forKey: .board)
|
||||
}
|
||||
}
|
||||
|
||||
/// Decodes the envelope, reading the payload named by the `kind` discriminator.
|
||||
public init(from decoder: any Decoder) throws {
|
||||
let container: KeyedDecodingContainer<CodingKeys> = try decoder.container(keyedBy: CodingKeys.self)
|
||||
self.version = try container.decode(Int.self, forKey: .version)
|
||||
let kind: Kind = try container.decode(Kind.self, forKey: .kind)
|
||||
switch kind {
|
||||
case .rigs:
|
||||
self.payload = .rigs(try container.decode(RigArchive.self, forKey: .rigs))
|
||||
case .board:
|
||||
self.payload = .board(try container.decode(BoardVersion.self, forKey: .board))
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user