Cotton code
59 lines · 2.6 KB · SwiftRaw
// A row in one of the three feeds that read across databases.//// Only Today, search and "Asked of you" answer across the databases this// device holds (owner decision 3 Oct 2026, Cotton note 0a22168c); every other// read is the shown database's. A row in one of them says which database it// came from — `databaseId` and `databaseName` beside its own fields — and one// id in two databases is two rows, never one.//// A row with no database is what every read answers today, and it keeps the// identity and the request it always had: with one database held nothing// here changes a byte. The core's reads pick their arguments by name and// ignore the rest (`cotton-ffi/src/phone/mod.rs::read_args`), so `database`// is only sent when there is more than one database to read.import Foundation/// The database a feed row came from.public struct FeedRowDatabase: Equatable, Hashable, Sendable {    public let databaseId: String    /// The name the row's database had when the row was read; empty when the    /// row named only the id.    public let databaseName: String    /// Nil when there is no id: a row with no database is not in one of    /// several.    public init?(databaseId: String?, databaseName: String? = nil) {        guard let databaseId, !databaseId.trimmingCharacters(in: .whitespaces).isEmpty else { return nil }        self.databaseId = databaseId        self.databaseName = databaseName ?? ""    }    /// The row's `databaseId` and `databaseName`, or nil when it names none.    public init?(_ row: JSONValue) {        self.init(databaseId: row["databaseId"]?.string, databaseName: row["databaseName"]?.string)    }    /// The key a feed lists a row under: the row's own key with no database,    /// and the database's id before it with one — so one id in two databases    /// is two rows, and a row with none keeps the key it always had.    public static func key(_ database: FeedRowDatabase?, id: String) -> String {        guard let database else { return id }        return "\(database.databaseId)/\(id)"    }    /// What a feed's request names to read every held database.    public static let all = "all"    /// `database` for a feed's request: `"all"` when this device holds more    /// than one database, nil — no key, the request as it always was — with    /// one or none.    public static func scope(holding count: Int) -> String? {        count > 1 ? all : nil    }    /// The link a row's tap opens: its route, in its own database.    public static func link(_ database: FeedRowDatabase?, route: Route) -> DatabaseLink {        DatabaseLink(database: database?.databaseId, route: route)    }}