Cotton code
246 lines · 7.3 KB · SwiftRaw
// COTTON CONTACTS' ANSWERS (assets/design/shared/CONTACTS-ANSWERS.md): the core's shapes, read as given. Every// tappable thing carries `call: {method, request}`, which the shell calls as given and redraws with the answer. All// the words are the core's; the shell writes none.import CottonKitimport Foundation/// A call the core named: its method and request, made as given.struct ContactsCall: Codable, Equatable, Hashable, Sendable {    var method: String    var request: JSONValue?    init(method: String, request: JSONValue? = nil) {        self.method = method        self.request = request    }}/// An act: its words, red when it destroys, the sentence to ask first when present.struct ContactsAct: Decodable, Equatable, Sendable, Identifiable {    var id: String    var words: String    var red: Bool?    var confirm: String?    var call: ContactsCall?}/// A row of a list: a person, an organisation, an assistant, a group.struct ContactsRow: Decodable, Equatable, Sendable, Identifiable {    var id: String    var words: String    var detail: String?    var initials: String?    var picture: String?    var kind: String?    var call: ContactsCall?    /// A group's person row: its "take out" at the right.    var acts: [ContactsAct]?}struct ContactsSection: Decodable, Equatable, Sendable, Identifiable {    var id: String    var title: String    var count: Int?    var rows: [ContactsRow]}struct ContactsEmpty: Decodable, Equatable, Sendable {    var title: String    var message: String?}/// Words and the call they make (Add, New group, Save, Review).struct ContactsLink: Decodable, Equatable, Sendable {    var id: String?    var words: String    var detail: String?    var act: String?    var call: ContactsCall?}/// `contacts.home {kind?, query?}`.struct ContactsHome: Decodable, Equatable, Sendable {    struct Search: Decodable, Equatable, Sendable {        var placeholder: String        var query: String?    }    struct Kind: Decodable, Equatable, Sendable, Identifiable {        var id: String        var words: String        var on: Bool        var call: ContactsCall?    }    struct Import: Decodable, Equatable, Sendable {        var words: String        var hint: String?    }    var title: String    var search: Search?    var kinds: [Kind]    /// "Still sorting your people" until each app's lines about people are counted after an update; nil then.    var sorting: String?    var same: ContactsLink?    var sections: [ContactsSection]    var add: ContactsLink?    var `import`: Import?    var empty: ContactsEmpty?    enum CodingKeys: String, CodingKey { case title, search, kinds, sorting, same, sections, add, `import`, empty }    init(title: String, search: Search?, kinds: [Kind], same: ContactsLink?, sections: [ContactsSection], add: ContactsLink?,         import: Import?, empty: ContactsEmpty?, sorting: String? = nil) {        self.sorting = sorting        self.title = title        self.search = search        self.kinds = kinds        self.same = same        self.sections = sections        self.add = add        self.import = `import`        self.empty = empty    }    init(from decoder: any Decoder) throws {        let c = try decoder.container(keyedBy: CodingKeys.self)        title = try c.decode(String.self, forKey: .title)        search = try c.decodeIfPresent(Search.self, forKey: .search)        kinds = try c.decodeIfPresent([Kind].self, forKey: .kinds) ?? []        sorting = try c.decodeIfPresent(String.self, forKey: .sorting)        same = try c.decodeIfPresent(ContactsLink.self, forKey: .same)        sections = try c.decodeIfPresent([ContactsSection].self, forKey: .sections) ?? []        add = try c.decodeIfPresent(ContactsLink.self, forKey: .add)        `import` = try c.decodeIfPresent(Import.self, forKey: .import)        empty = try c.decodeIfPresent(ContactsEmpty.self, forKey: .empty)    }}/// `contacts.person {id}`.struct ContactsPerson: Decodable, Equatable, Sendable {    struct Between: Decodable, Equatable, Sendable, Identifiable {        var app: String        var words: String        var detail: String?        var open: JSONValue?        var id: String { app }    }    struct Reach: Decodable, Equatable, Sendable, Identifiable {        var id: String        var kind: String?        var words: String        var detail: String?        var shared: [String]?        var acts: [ContactsAct]?    }    struct Source: Decodable, Equatable, Sendable {        var words: String        var detail: String?    }    var id: String    var kind: String?    var title: String    var initials: String?    var picture: String?    var subtitle: String?    var archived: Bool?    var mergedInto: String?    var between: [Between]?    var reach: [Reach]?    var groups: [ContactsLink]?    var organisation: ContactsLink?    var sources: [Source]?    var same: [ContactsLink]?    var pinned: Bool?    var acts: [ContactsAct]?}/// `contacts.add.form {}` and `contacts.edit.form {id}`.struct ContactsForm: Decodable, Equatable, Sendable {    struct Kind: Decodable, Equatable, Sendable, Identifiable {        var id: String        var words: String        var on: Bool    }    struct Field: Decodable, Equatable, Sendable, Identifiable {        var id: String        var words: String        var value: String?        var values: [String]?        var add: String?    }    var title: String    var kinds: [Kind]?    var fields: [Field]    var save: ContactsLink}/// `contacts.same.list {}`.struct ContactsSameList: Decodable, Equatable, Sendable {    struct Side: Decodable, Equatable, Sendable {        var id: String        var words: String        var detail: String?        var initials: String?        var reach: [String]?    }    struct Pair: Decodable, Equatable, Sendable, Identifiable {        var a: Side        var b: Side        var why: String?        var acts: [ContactsAct]        var id: String { a.id + "|" + b.id }    }    var title: String    var count: Int?    var pairs: [Pair]    var empty: ContactsEmpty?}/// `contacts.groups {}`.struct ContactsGroups: Decodable, Equatable, Sendable {    var title: String    var sections: [ContactsSection]    var foot: String?    var add: ContactsLink?}/// `contacts.group {id}`.struct ContactsGroup: Decodable, Equatable, Sendable {    var id: String    var title: String    var detail: String?    var madeBy: String?    var rows: [ContactsRow]    var acts: [ContactsAct]?}/// `contacts.circle {}` (later; "Soon" until then).struct ContactsCircle: Decodable, Equatable, Sendable {    var title: String    var empty: ContactsEmpty?}/// What a write answers: the kept id (or ids) and the core's sentence, or its refusal.struct ContactsSaid: Decodable, Equatable, Sendable {    var id: String?    var ids: [String]?    var said: String?    var error: String?}/// A read: the answer, or the sentence saying why not.enum ContactsRead<Answer: Sendable>: Sendable {    case read(Answer)    case refused(String)    var answer: Answer? { if case .read(let a) = self { a } else { nil } }}extension JSONValue {    /// A request's object fields (an absent request is an empty one).    var contactsFields: [String: JSONValue] { if case .object(let o) = self { o } else { [:] } }}