Cotton code
174 lines · 6.4 KB · SwiftRaw
// SEND, AS THE CORE ANSWERS IT (`crates/cotton-ffi/src/phone/cotton_send.rs`):// a copy of a record to one of the person's people. The object decides what// goes, to whom, what fits and what is held; the core says every word a// send's card and tier show (`shows`, the Send step). These are the shapes,// and nothing here decides anything.import CottonKitimport CottonValuesimport Foundation/// A record a send names: what it landed as, or what the person already holds.public struct SendRecordRef: Codable, Equatable, Hashable, Sendable {    public var table: String    public var id: String}/// What `send.trust` is asked: a person by number, and whether every send from them is kept.public struct SendTrust: Codable, Equatable, Hashable, Sendable {    public var number: String    public var trusted: Bool}/// One act a send's card offers under it, or a person's row holds, in the/// core's order and words: Keep (with its size), Decline, Open, Remove,/// Undo, Always keep from {name}.public struct SendAct: Decodable, Equatable, Hashable, Sendable, Identifiable {    public var id: String    public var words: String    public var primary: Bool?    public var red: Bool?    /// Open's record.    public var opens: SendRecordRef?    /// The trust act's ask.    public var trust: SendTrust?}/// How far a copy has come, in bytes.public struct SendProgress: Decodable, Equatable, Hashable, Sendable {    public var done: Double?    public var bytes: Double?    /// Done over the whole, 0…1; nil while the whole is not known.    public var fraction: Double? {        guard let bytes, bytes > 0 else { return nil }        return min(1, max(0, (done ?? 0) / bytes))    }}/// What a send's card says under it (`cotton_send::shows`).public struct SendShows: Decodable, Equatable, Hashable, Sendable {    /// "From {name}", on a received card.    public var from: String?    /// Its state in words: Kept, Already in your Cotton, Copying 3 MB of 12 MB, Delivered.    public var line: String?    public var progress: SendProgress?    public var acts: [SendAct] = []}/// One send, received or made, as the inbox (or its thread item) holds it.public struct SendItem: Decodable, Equatable, Identifiable, Sendable {    public var id: String    public var state: String?    public var title: String?    /// The real card: the root table's face and its fields (`{face, fields}`).    public var card: JSONValue?    public var shows: SendShows?    /// Whether its copy is still under way: the inbox is asked again until it lands.    public var copying: Bool { state == "keeping" }}/// `send.inbox`'s answer.public struct SendInbox: Decodable, Sendable {    public var `in`: [SendItem]    public var out: [SendItem]}/// One person of the Send tier (`shell.opened {step: send}`).public struct SendPerson: Decodable, Equatable, Hashable, Sendable {    /// Their Cotton thread: what `send.make` is asked with.    public var value: String    public var words: String    public var number: String    public var trusted: Bool?    public var initials: String?    /// The size that goes, and whether every send from them is kept.    public var detail: String?    /// What a hold on them offers (Always keep from {name}); none while they wait.    public var holds: [SendAct] = []    /// Added by number and not yet added back: nothing goes to them, and a    /// tap only says `detail` ("They have not added you yet").    public var waiting: Bool?    public init(from decoder: any Decoder) throws {        let row = try decoder.container(keyedBy: CodingKeys.self)        value = try row.decode(String.self, forKey: .value)        words = try row.decode(String.self, forKey: .words)        number = try row.decode(String.self, forKey: .number)        trusted = try row.decodeIfPresent(Bool.self, forKey: .trusted)        initials = try row.decodeIfPresent(String.self, forKey: .initials)        detail = try row.decodeIfPresent(String.self, forKey: .detail)        holds = try row.decodeIfPresent([SendAct].self, forKey: .holds) ?? []        waiting = try row.decodeIfPresent(Bool.self, forKey: .waiting)    }    private enum CodingKeys: String, CodingKey {        case value, words, number, trusted, initials, detail, holds, waiting    }}/// Add a person: their number in the shell's one short-text input, as the/// core's `ask` names it (`shell.input {op: ask, kind: people_add}`), the one/// add Send's tier and Account's Your people both open.public struct SendAdd: Decodable, Equatable, Hashable, Sendable {    public var words: String    public var hint: String?    /// The input's ask as the core wrote it, handed back to it whole.    public var ask: JSONValue?}/// The door to Cotton chat's request, where the app keeps chat.public struct SendDoor: Decodable, Equatable, Hashable, Sendable {    public var words: String    public var path: String}/// What the tier says with nobody to send to.public struct SendEmpty: Decodable, Equatable, Hashable, Sendable {    public var words: String    public var door: SendDoor?}/// The Send step: the people, what goes and its size, Add a person, or why nobody can be sent to.public struct SendStep: Decodable, Equatable, Hashable, Sendable {    public var says: String?    public var rows: [SendPerson] = []    public var empty: SendEmpty?    /// Add a person: present whenever the record can be sent, drawn last.    public var add: SendAdd?    /// The object's words for a record that cannot be sent.    public var refused: String?}struct SendStepAnswer: Decodable {    var step: SendStep}/// The receipt Send leaves: "Sent {title} to {name}", its Undo, and what Undo says.public struct SendReceipt: Decodable, Equatable, Sendable {    public var words: String    public var undo: String?    public var undone: String?}/// `send.make`'s answer.struct SendMade: Decodable {    var id: String    var receipt: SendReceipt?}/// `send.keep` / `send.undo`'s answer: the card's words as it now stands.struct SendMoved: Decodable {    var state: String?    var shows: SendShows?}/// A Cotton thread's row that is a send: its `meta.cotton.send`, the item/// `send.inbox` or `send.make` kept on it.enum SendRow {    static func item(of message: TableRecord) -> SendItem? {        guard let send = CottonPictures.meta(of: message)?["cotton"]?["send"], let data = try? JSONEncoder().encode(send) else { return nil }        return try? JSONDecoder().decode(SendItem.self, from: data)    }}