// What a write answers — the change, its receipt, the command it was and what // it opened — over the records `CottonValues/TableRecord.swift` holds. import CottonKit import CottonValues import Foundation /// What every write answers: the change the app shows with its byline and can /// undo. `before` is nil for a create, `after` nil for a delete. /// /// `undoable` and `reason` are the core's own words on the change (cotton-core's /// change JSON carries `undoable` and `reason`): whoever minted the change says /// whether `undo(change_id)` can take it back, so a receipt never offers an /// Undo the verbs would refuse — or one this build cannot send at all. public struct ChangeRecord: Hashable, Sendable, Identifiable { public var id: String public var byline: String public var before: TableRecord? public var after: TableRecord? /// The record this one was folded into by the write (`moved_to`): a /// person merged into the other side of "Same as …?" lives on as that /// record, and the page that showed it shows that record's page instead. /// Nil for every other write. public var movedTo: RecordRef? /// Whether the verbs can take this change back. public var undoable: Bool /// Why not, in words, when `undoable` is false. public var reason: String? /// What happened, in the core's words (`change.receipt`): the receipt /// prints these and composes none of its own. Nil from a store that /// answers none (the fixtures, Settings' own tables). public var said: ChangeSaid? /// The command this change was (`records.command`): its Undo is the /// command the definition names, run on the same record. public var command: CommandTaken? /// What an `open` command answered: the record or the address to open. /// Such a change changed nothing and wears no receipt. public var opened: CommandOpened? /// The record a create command wrote, alongside its durable receipt and Undo. public var created: RecordLink? /// A change only this device's memory holds — a field of a draft nothing /// has written yet. It wears no receipt: there is nothing to report or undo. public var quiet = false /// The core saved an intent whose serving Mac has not confirmed it yet. /// It cannot dismiss a record or announce a completed change. public var pending = false public init( id: String, byline: String, before: TableRecord?, after: TableRecord?, movedTo: RecordRef? = nil, undoable: Bool = true, reason: String? = nil, said: ChangeSaid? = nil, command: CommandTaken? = nil, opened: CommandOpened? = nil ) { self.said = said self.command = command self.opened = opened self.id = id self.byline = byline self.before = before self.after = after self.movedTo = movedTo self.undoable = undoable self.reason = reason } } /// The core's receipt for a change: a verb and the record's name ("Added Call /// the plumber", "Pinned"), and a before and after only for words. public struct ChangeSaid: Hashable, Sendable, Decodable { public struct Words: Hashable, Sendable, Decodable { public var name: String public var before: String? public var after: String? } /// added, appended, deleted, put_back, archived, switched, edited, /// changed, reordered — or `command` for a command's own words. public var kind: String public var said: String public var words: [Words] public init(kind: String, said: String, words: [Words] = []) { self.kind = kind self.said = said self.words = words } private enum CodingKeys: String, CodingKey { case kind, said, words } public init(from decoder: Decoder) throws { let c = try decoder.container(keyedBy: CodingKeys.self) kind = try c.decode(String.self, forKey: .kind) said = try c.decode(String.self, forKey: .said) words = try c.decodeIfPresent([Words].self, forKey: .words) ?? [] } } /// A command that ran on one record, and what takes it back: a command of /// its table (`undo`), or — when the app method it ran answered its own way /// back — that same method with what was there before (`undoCall`). public struct CommandTaken: Hashable, Sendable { public var table: String public var record: String public var undo: String? public var undoCall: CommandUndoCall? public init(table: String, record: String, undo: String?, undoCall: CommandUndoCall? = nil) { self.table = table self.record = record self.undo = undo self.undoCall = undoCall } } /// An app method's own way back (`records.command`'s `undo_call`): the same /// method, with the arguments that put back what it changed. The core checks /// it is the command's own method; the app only sends it back. public struct CommandUndoCall: Hashable, Sendable { public var method: String /// The arguments, as the JSON the core answered. public var args: String public init(method: String, args: String) { self.method = method self.args = args } } /// What an `open` command opens: a link's record, or a url column's address. public enum CommandOpened: Hashable, Sendable { case record(RecordLink) /// A record the act made at the object (`RecordCommand.lands`), coming by /// sync: stood on once it is here (an article's Fetch, its download). case landed(RecordLink) case address(String) case platform(RecordPlatformAction) /// A database to show (`shows_database`): a restored one, its cap back /// on the arc — the page it was restored from closes onto it. case database(String) } /// One record of one table, by id. public struct RecordRef: Hashable, Sendable, Codable { public var table: String public var id: String public init(table: String, id: String) { self.table = table self.id = id } } extension JSONValue { /// A stamp of any read as the core resolved it (`page::attach_stamp_parts` /// puts `@day_parts` beside the stamps), or nil. public func day(_ key: String) -> DayParts? { guard let parts = self["@day_parts"]?[key] else { return nil } return DayParts(RecordValue(parts)) } }