importFoundation/// The serving Mac's backup, as its own device page reads it off `read.device`/// (`backup`) and `device.backupNow` answers it again: the relay's state in/// one of four words, when the copy last reached the bucket, why it is/// failing, and when the next whole snapshot is due. The core maps the relay's/// status (`cotton_ffi::methods::host_settings::backup_section`); nothing/// here decides a state.publicstructDeviceBackup: Equatable, Sendable {publicenumState: String, Sendable {/// No bucket, or half a configuration (its missing keys the error).case never/// The last sync reached the bucket.case ok/// The copy fell behind or failed; `lastError` says why.case failing/// The first sync is not in yet.case running }publicvar state: State/// When the copy last reached the bucket (ISO-8601).publicvar lastAt: String?/// Why it is failing, or what a half configuration is missing.publicvar lastError: String?/// When the next whole snapshot is due (ISO-8601).publicvar nextAt: String?/// Since when a failing copy has not got through (ISO-8601): the Mac's/// own judgement of when it fell behind, or the last sync it saw before a/// failure. `nil` when it is not failing, or the Mac does not know.publicvar since: String?/// Each served database's photos and files, in the Mac's serving order.publicvar media: [Media]/// One database's photos and files — the core's words/// (`host_settings::media_line`), nothing decided here.publicstructMedia: Equatable, Sendable {publicenumState: String, Sendable {/// The bucket holds every file: `files` of them, as of `at`.case copied/// `pending` files still to copy `since`; the next pass is on its way.case copying/// The bucket refused, or the gate said no; `reason` says why.case failing/// The first pass has not finished.case starting/// Half a configuration; `reason` names what is missing.case never/// No bucket: this Mac holds the only copy.case off }publicvar databaseId: Stringpublicvar name: String?publicvar state: Statepublicvar at: String?publicvar files: Int?publicvar pending: Int?publicvar since: String?publicvar reason: String?publicinit( databaseId: String, name: String? = nil, state: State, at: String? = nil, files: Int? = nil, pending: Int? = nil, since: String? = nil, reason: String? = nil ) {self.databaseId = databaseIdself.name = nameself.state = stateself.at = atself.files = filesself.pending = pendingself.since = sinceself.reason = reason }/// One `databases[]` entry of the line, or `nil` for a state this/// build does not know.init?(database: JSONValue) {guardlet id = database["databaseId"]?.string, let media = database["media"],let word = media["state"]?.string, let state = State(rawValue: word)else { returnnil }let text = { (key: String) in media[key]?.string.flatMap { $0.isEmpty ? nil : $0 } }self.init( databaseId: id, name: database["name"]?.string.flatMap { $0.isEmpty ? nil : $0 }, state: state, at: text("at"), files: media["files"]?.int, pending: media["pending"]?.int, since: text("since"), reason: text("reason")) } }publicinit( state: State, lastAt: String? = nil, lastError: String? = nil, nextAt: String? = nil, since: String? = nil, media: [Media] = [] ) {self.state = stateself.lastAt = lastAtself.lastError = lastErrorself.nextAt = nextAtself.since = sinceself.media = media }/// Half a configuration: the Mac's env file names a bucket but its/// settings do not add up, so nothing is copied off it. The core answers/// that — and only that — as `never` WITH what is missing as its error;/// no bucket named at all is `never` with none (a Mac holding the only/// copy by choice).publicvar isHalfSetUp: Bool { state == .never && lastError != nil }/// The line itself — `read.device`'s `backup`, or `device.backupNow`'s./// `nil` for a null line or a state this build does not know, never a/// guess.publicinit?(line: JSONValue?) {guardlet line, let word = line["state"]?.string, let state = State(rawValue: word) else { returnnil }let text = { (key: String) in line[key]?.string.flatMap { $0.isEmpty ? nil : $0 } }self.init( state: state, lastAt: text("lastAt"), lastError: text("lastError"), nextAt: text("nextAt"), since: text("since"), media: (line["databases"]?.array ?? []).compactMap(Media.init(database:))) }/// Who said a page's backup (`read.device`'s `backupAnsweredBy`): the Mac/// itself on its own page, or the serving Mac for a phone or another Mac/// looking at that Mac's page. Every other answer carries no backup.publicenumAnsweredBy: String, Sendable {case thisMac = "this_mac"case servingMac = "serving_mac" }/// The backup a `read.device` answer carries and who said it, or `nil`/// when the page is not the serving Mac's (`not_serving`, `no_serving_mac`,/// a phone's own `not_a_mac`): only that Mac answers one.publicstaticfunc answered(_ answer: JSONValue) -> (backup: DeviceBackup, by: AnsweredBy)? {guardlet word = answer["backupAnsweredBy"]?.string, let by = AnsweredBy(rawValue: word),let backup = DeviceBackup(line: answer["backup"])else { returnnil }return (backup, by) }}