Cotton code
88 lines · 3.3 KB · SwiftRaw
// A .cottontool manifest: data describing a tool, never executable code.import CottonValuesimport Foundationimport SwiftUIextension DefinitionToolPicture {    /// What the card draws. A built-in's picture is a photo record that names    /// the object the app ships with the same bytes (`{"photo": …, "object":    /// "tool-todos"}`): the device draws that object — its prints, its small    /// variant, offline — and a photo the owner set, naming no object, is    /// drawn from the photo. A picture that only says how a link is drawn is    /// no picture.    public var drawn: ToolPicture? {        switch self {        case .asset(let object): .asset(object)        // An object the app does not ship (a package naming its own picture        // as an object) is no object: the photo is drawn.        case .photo(let photo): photo.object.flatMap { ToolObjects.ships($0) ? ToolPicture.asset($0) : nil } ?? .photo(photo.photo)        case .link: nil        }    }    /// How a card draws a link to one of this table's records.    public var linkObject: LinkObject? {        switch self {        case .asset: nil        case .photo(let photo): photo.linkObject        case .link(let link): link.linkObject        }    }    /// `picture` stored with `linkObject` kept. A shipped object alone is its    /// bare name, which carries no link object (the core's form), so only a    /// photo or no picture keeps one.    init?(_ picture: ToolPicture?, linkObject: LinkObject?) {        switch picture {        case .asset(let object): self = .asset(object)        case .photo(let id):            var photo = DefinitionToolPhoto(_generated: (), photo: id)            photo.linkObject = linkObject            self = .photo(photo)        case nil:            guard let linkObject else { return nil }            self = .link(DefinitionLinkPicture(_generated: (), linkObject: linkObject))        }    }}public typealias ToolDefinition = DefinitionToolDefinitionextension DefinitionToolDefinition: Identifiable {    public typealias Author = DefinitionToolAuthor    public init(        id: String, name: String, picture: DefinitionToolPicture? = nil, cardStock: ToolCardTint? = nil, tables: [String],        instructions: String, automations: [DefinitionJSONValue], author: Author, signature: String?    ) {        self.init(            _generated: (), id: id, name: name, tables: tables, instructions: instructions, automations: automations,            author: author)        self.picture = picture        self.cardStock = cardStock        self.signature = signature    }    public static func decode(_ data: Data) throws -> Self {        try JSONDecoder().decode(Self.self, from: data)    }}extension DefinitionToolAuthor {    public init(device: String, name: String) { self.init(_generated: (), device: device, name: name) }}/// Synced tools in tool_order order; compiled tools keep their existing slots.public struct ToolDefinitions: Sendable {    public var all: [ToolDefinition]    public init(_ all: [ToolDefinition]) { self.all = all }}private struct ToolDefinitionsKey: EnvironmentKey {    static let defaultValue = ToolDefinitions([])}extension EnvironmentValues {    public var toolDefinitions: ToolDefinitions {        get { self[ToolDefinitionsKey.self] }        set { self[ToolDefinitionsKey.self] = newValue }    }}