Cotton code
64 lines · 2.5 KB · SwiftRaw
import Foundation// The theme a database wears, through the boundary: read off the database's// catalogue (`definitions.catalogue`, `DefinitionCatalogues`) and// `definitions.saveTheme` written (design note 26203e21 §6). The row is// handed over as its JSON, because what a row MEANS — families, faces,// palettes — is CottonUI's `Theme.decode`, and this side only carries it./// The theme the shown database wears, as the core answered it.public struct WornTheme: Sendable, Equatable {    /// The row, as JSON — the database's own, or the shipped `paper`.    public let definition: Data    /// Why a row stored here was not drawn, or which family was not.    public let passedOver: String?    /// Whether the row is the database's own (`drawn: "row"`) rather than    /// the shipped default (`drawn: "default"`) — what a business flavour's    /// look gives way to (`FlavourLook`). An answer that does not say is the    /// database's own, so a look never covers a row it cannot tell apart.    public let own: Bool    public init(definition: Data, passedOver: String? = nil, own: Bool = true) {        self.definition = definition        self.passedOver = passedOver        self.own = own    }    /// The core's word for the shipped default (`presentation::Drawn`).    public static let defaultDrawn = "default"}public protocol ThemesCalling: Sendable {    /// What the shown database wears.    func theme() async throws -> WornTheme    /// Wear `definition` (a theme row's JSON) as the database's own row.    func save(_ definition: Data) async throws -> ActAnswer    /// Take a saved theme back.    func takeBack(_ undo: ActUndo) async throws -> ActAnswer}/// The core's own answers.public struct CoreThemes: ThemesCalling {    private let core: any CottonCalling    public init(core: any CottonCalling) {        self.core = core    }    public func theme() async throws -> WornTheme {        try await DefinitionCatalogues.shared.catalogue(core).theme    }    public func save(_ definition: Data) async throws -> ActAnswer {        let row = try JSONDecoder().decode(JSONValue.self, from: definition)        defer { DefinitionCatalogues.shared.invalidate(core) }        return try await core.pageAct(CottonMethod.definitionsSaveTheme, Save(definition: row))    }    public func takeBack(_ undo: ActUndo) async throws -> ActAnswer {        defer { DefinitionCatalogues.shared.invalidate(core) }        return try await core.takeBack(undo)    }    private struct Save: Encodable { let definition: JSONValue }}