Cotton code
427 lines · 13.1 KB · SwiftRaw
// Cotton Vault's screen answers, as the core publishes them// (`cotton_core::keys::app::shapes`, SHAPES.md in prose). Add-only on the// core's side: every field here is read as it is named there, and a field the// core adds later is ignored until a screen draws it. The phone decides// nothing: each act is sent back as it came.import CottonKitimport Foundationenum VaultTone: String, Decodable, Sendable {    case neutral, safe, attention, danger, accent    /// The colour role a tone draws in.    var role: String {        switch self {        case .neutral: return "secondary"        case .safe: return "safe"        case .attention: return "attention"        case .danger: return "danger"        case .accent: return "accent"        }    }    /// The wash behind a banner of this tone.    var wash: String {        switch self {        case .safe: return "safe_wash"        case .attention: return "attention_wash"        case .danger: return "danger_wash"        case .neutral, .accent: return "surface"        }    }}struct VaultConfirm: Decodable, Equatable, Sendable {    let title: String    let body: String    let yes: String    let no: String}/// One field an act asks for before it is sent (a trusted person's number).struct VaultPrompt: Decodable, Equatable, Sendable {    let key: String    let label: String    let input: String    var placeholder: String?    /// A choice's options (`input: choice`), in the core's order; empty for every other input.    var choices: [VaultChoice]?}struct VaultAct: Decodable, Equatable, Sendable, Identifiable {    let id: String    let label: String    let method: String    let request: JSONValue    var presence: Bool = false    var confirm: VaultConfirm?    var tone: VaultTone = .neutral    var symbol: String?    var input: VaultPrompt?    enum CodingKeys: String, CodingKey { case id, label, method, request, presence, confirm, tone, symbol, input }    init(from decoder: Decoder) throws {        let c = try decoder.container(keyedBy: CodingKeys.self)        id = try c.decode(String.self, forKey: .id)        label = try c.decodeIfPresent(String.self, forKey: .label) ?? ""        method = try c.decode(String.self, forKey: .method)        request = try c.decodeIfPresent(JSONValue.self, forKey: .request) ?? .object([:])        presence = try c.decodeIfPresent(Bool.self, forKey: .presence) ?? false        confirm = try c.decodeIfPresent(VaultConfirm.self, forKey: .confirm)        tone = try c.decodeIfPresent(VaultTone.self, forKey: .tone) ?? .neutral        symbol = try c.decodeIfPresent(String.self, forKey: .symbol)        input = try c.decodeIfPresent(VaultPrompt.self, forKey: .input)    }    init(id: String, label: String, method: String, request: JSONValue, presence: Bool = false, tone: VaultTone = .neutral) {        self.id = id        self.label = label        self.method = method        self.request = request        self.presence = presence        self.tone = tone    }}struct VaultPicture: Decodable, Equatable, Sendable {    let initials: String    let tint: Int    var symbol: String?}struct VaultMark: Decodable, Equatable, Sendable, Identifiable {    let id: String    let label: String    let tone: VaultTone}struct VaultToast: Decodable, Equatable, Sendable {    let text: String    var undo: VaultAct?}struct VaultChip: Decodable, Equatable, Sendable, Identifiable {    let id: String    let label: String    let count: Int    let on: Bool}struct VaultRow: Decodable, Equatable, Sendable, Identifiable {    let id: String    let kind: String    let title: String    let subtitle: String    let picture: VaultPicture    let favourite: Bool    let marks: [VaultMark]    var badge: String?    let quick: [VaultAct]}struct VaultSection: Decodable, Equatable, Sendable, Identifiable {    let id: String    var title: String?    let rows: [VaultRow]}struct VaultEmpty: Decodable, Equatable, Sendable {    let title: String    let body: String    var act: VaultAct?}struct VaultAddMenu: Decodable, Equatable, Sendable {    let label: String    let kinds: [VaultAct]}struct VaultItems: Decodable, Equatable, Sendable {    let title: String    let searchPlaceholder: String    let q: String    let filter: String    let chips: [VaultChip]    let sections: [VaultSection]    var next: String?    let total: Int    var empty: VaultEmpty?    var bin: VaultAct?    let add: VaultAddMenu    let open: Bool}struct VaultField: Decodable, Equatable, Sendable, Identifiable {    let key: String    let label: String    var value: String?    var concealed: Bool = false    var mono: Bool = false    var liveCode: Bool = false    var acts: [VaultAct] = []    var id: String { key }    enum CodingKeys: String, CodingKey { case key, label, value, concealed, mono, liveCode, acts }    init(from decoder: Decoder) throws {        let c = try decoder.container(keyedBy: CodingKeys.self)        key = try c.decode(String.self, forKey: .key)        label = try c.decodeIfPresent(String.self, forKey: .label) ?? ""        value = try c.decodeIfPresent(String.self, forKey: .value)        concealed = try c.decodeIfPresent(Bool.self, forKey: .concealed) ?? false        mono = try c.decodeIfPresent(Bool.self, forKey: .mono) ?? false        liveCode = try c.decodeIfPresent(Bool.self, forKey: .liveCode) ?? false        acts = try c.decodeIfPresent([VaultAct].self, forKey: .acts) ?? []    }}struct VaultBlock: Decodable, Equatable, Sendable, Identifiable {    let id: String    var title: String?    let fields: [VaultField]}struct VaultBanner: Decodable, Equatable, Sendable {    let text: String    let tone: VaultTone    var act: VaultAct?}struct VaultItem: Decodable, Equatable, Sendable {    let id: String    let kind: String    let kindLabel: String    let title: String    let subtitle: String    let picture: VaultPicture    let favourite: Bool    var banner: VaultBanner?    let blocks: [VaultBlock]    let acts: [VaultAct]    let open: Bool    var unlock: VaultAct?    let inBin: Bool}struct VaultFieldAnswer: Decodable, Equatable, Sendable {    let id: String    let field: String    let value: String    let mono: Bool    let clearAfter: Int    let copiedToast: String}struct VaultCodeAnswer: Decodable, Equatable, Sendable {    let id: String    let code: String    let shown: String    let secondsLeft: Int    let period: Int    let clearAfter: Int    let copiedToast: String}struct VaultPastRow: Decodable, Equatable, Sendable {    let value: String    let until: String    let acts: [VaultAct]}struct VaultHistory: Decodable, Equatable, Sendable {    let id: String    let title: String    let rows: [VaultPastRow]    var empty: String?    var clearAfter: Int?}struct VaultChoice: Decodable, Equatable, Sendable, Identifiable {    let value: String    let label: String    var id: String { value }}struct VaultFormField: Decodable, Equatable, Sendable, Identifiable {    let key: String    let label: String    let input: String    let required: Bool    var value: String?    var placeholder: String?    var generate: Bool = false    let sealed: Bool    var choices: [VaultChoice] = []    var id: String { key }    enum CodingKeys: String, CodingKey { case key, label, input, required, value, placeholder, generate, sealed, choices }    init(from decoder: Decoder) throws {        let c = try decoder.container(keyedBy: CodingKeys.self)        key = try c.decode(String.self, forKey: .key)        label = try c.decodeIfPresent(String.self, forKey: .label) ?? ""        input = try c.decodeIfPresent(String.self, forKey: .input) ?? "text"        required = try c.decodeIfPresent(Bool.self, forKey: .required) ?? false        value = try c.decodeIfPresent(String.self, forKey: .value)        placeholder = try c.decodeIfPresent(String.self, forKey: .placeholder)        generate = try c.decodeIfPresent(Bool.self, forKey: .generate) ?? false        sealed = try c.decodeIfPresent(Bool.self, forKey: .sealed) ?? false        choices = try c.decodeIfPresent([VaultChoice].self, forKey: .choices) ?? []    }}struct VaultEdit: Decodable, Equatable, Sendable {    var id: String?    let kind: String    let title: String    let fields: [VaultFormField]    let save: VaultAct    let cancel: String}struct VaultActAnswer: Decodable, Equatable, Sendable {    let act: String    var id: String?    var toast: VaultToast?    var item: VaultItem?    var gone: Bool = false    var platform: JSONValue?    enum CodingKeys: String, CodingKey { case act, id, toast, item, gone, platform }    init(from decoder: Decoder) throws {        let c = try decoder.container(keyedBy: CodingKeys.self)        act = try c.decodeIfPresent(String.self, forKey: .act) ?? ""        id = try c.decodeIfPresent(String.self, forKey: .id)        toast = try c.decodeIfPresent(VaultToast.self, forKey: .toast)        item = try c.decodeIfPresent(VaultItem.self, forKey: .item)        gone = try c.decodeIfPresent(Bool.self, forKey: .gone) ?? false        platform = try c.decodeIfPresent(JSONValue.self, forKey: .platform)    }}struct VaultControl: Decodable, Equatable, Sendable, Identifiable {    let key: String    let label: String    let control: String    let value: JSONValue    var min: Int?    var max: Int?    var choices: [VaultChoice] = []    var id: String { key }    enum CodingKeys: String, CodingKey { case key, label, control, value, min, max, choices }    init(from decoder: Decoder) throws {        let c = try decoder.container(keyedBy: CodingKeys.self)        key = try c.decode(String.self, forKey: .key)        label = try c.decodeIfPresent(String.self, forKey: .label) ?? ""        control = try c.decodeIfPresent(String.self, forKey: .control) ?? "switch"        value = try c.decodeIfPresent(JSONValue.self, forKey: .value) ?? .null        min = try c.decodeIfPresent(Int.self, forKey: .min)        max = try c.decodeIfPresent(Int.self, forKey: .max)        choices = try c.decodeIfPresent([VaultChoice].self, forKey: .choices) ?? []    }}struct VaultStrength: Decodable, Equatable, Sendable {    let score: Int    let label: String    let tone: VaultTone    let detail: String}struct VaultGenerated: Decodable, Equatable, Sendable {    let title: String    let mode: String    let modes: [VaultChoice]    let value: String    let strength: VaultStrength    let controls: [VaultControl]    let request: JSONValue    let again: String    let copy: VaultAct    let useNew: VaultAct    let useHere: String    let clearAfter: Int    let copiedToast: String}struct VaultListRow: Decodable, Equatable, Sendable, Identifiable {    let id: String    let title: String    var detail: String?    var tone: VaultTone = .neutral    var picture: VaultPicture?    var open: VaultAct?    var acts: [VaultAct] = []    enum CodingKeys: String, CodingKey { case id, title, detail, tone, picture, open, acts }    init(from decoder: Decoder) throws {        let c = try decoder.container(keyedBy: CodingKeys.self)        id = try c.decode(String.self, forKey: .id)        title = try c.decodeIfPresent(String.self, forKey: .title) ?? ""        detail = try c.decodeIfPresent(String.self, forKey: .detail)        tone = try c.decodeIfPresent(VaultTone.self, forKey: .tone) ?? .neutral        picture = try c.decodeIfPresent(VaultPicture.self, forKey: .picture)        open = try c.decodeIfPresent(VaultAct.self, forKey: .open)        acts = try c.decodeIfPresent([VaultAct].self, forKey: .acts) ?? []    }}struct VaultGroup: Decodable, Equatable, Sendable, Identifiable {    let id: String    let title: String    var detail: String?    let rows: [VaultListRow]    var empty: String?    var acts: [VaultAct] = []    enum CodingKeys: String, CodingKey { case id, title, detail, rows, empty, acts }    init(from decoder: Decoder) throws {        let c = try decoder.container(keyedBy: CodingKeys.self)        id = try c.decode(String.self, forKey: .id)        title = try c.decodeIfPresent(String.self, forKey: .title) ?? ""        detail = try c.decodeIfPresent(String.self, forKey: .detail)        rows = try c.decodeIfPresent([VaultListRow].self, forKey: .rows) ?? []        empty = try c.decodeIfPresent(String.self, forKey: .empty)        acts = try c.decodeIfPresent([VaultAct].self, forKey: .acts) ?? []    }}struct VaultHealth: Decodable, Equatable, Sendable {    let title: String    let summary: String    let tone: VaultTone    let weak: Int    let reused: Int    let old: Int    var breached: Int?    let counts: [VaultChip]    let open: Bool    var unlock: VaultAct?}struct VaultSecurity: Decodable, Equatable, Sendable {    let title: String    let health: VaultHealth    let groups: [VaultGroup]}struct VaultSettings: Decodable, Equatable, Sendable {    let title: String    let groups: [VaultGroup]}extension JSONValue {    /// This object with one key set: a control changed, a typed value added.    func setting(_ key: String, _ value: JSONValue) -> JSONValue {        guard case .object(var members) = self else { return .object([key: value]) }        members[key] = value        return .object(members)    }}