// Find's answer, as the core words it (`read.find`'s `sections`, // `crates/cotton-ffi/src/phone/find_words.rs`). // // The core groups the hits, orders the sections, names each by its tool, // counts them, titles and captions every hit, and says where each opens and // which record its card is read from. Nothing here re-decides any of it: a // section is drawn in the order it came, under its own words, with its own // count, and a hit is drawn with the words it carries. import CottonKit import Foundation /// One section of the answer: a built-in kind's or an imported table's. struct FindSection: Equatable, Sendable { /// The section's id — a built-in section (`todo`, `message`) or an /// imported table's id. let id: String /// Its name, the tool that answered. let words: String /// How many the database holds, never fewer than the hits in hand. let count: Int /// An imported table's section, paged by its table. let defined: Bool /// The kinds a built-in section pages through, one at a time. let kinds: [String] var hits: [FindHit] init(id: String, words: String, count: Int, defined: Bool = false, kinds: [String] = [], hits: [FindHit]) { self.id = id self.words = words self.count = count self.defined = defined self.kinds = kinds self.hits = hits } init(_ json: JSONValue) { id = json.text("id") ?? "" words = json.text("words") ?? "" count = json.number("count") ?? 0 defined = json.flag("defined") kinds = json.list("kinds").compactMap(\.string) hits = json.list("hits").map(FindHit.init) } /// Every section of an answer, in its order. static func all(_ answer: JSONValue) -> [FindSection] { answer.list("sections").map(FindSection.init) } } /// One hit, as its section lists it. struct FindHit: Equatable, Sendable { /// The record a hit stands for: the one its card is read from. struct Card: Equatable, Sendable { let table: String let id: String } let kind: String let recordId: String let title: String /// The matched line, when it says something the title does not. let subtitle: String? /// The marks, then the page or the day. let caption: String? /// A finished life: a done todo, an achieved goal. let struck: Bool /// Where a tap goes; `nil` for a hit that opens nothing. let opens: Route? let card: Card? /// The database the hit is in, when the search read several. var database: FeedRowDatabase? init( kind: String, id: String, title: String, subtitle: String? = nil, caption: String? = nil, struck: Bool = false, opens: Route? = nil, card: Card? = nil, database: FeedRowDatabase? = nil ) { self.kind = kind self.recordId = id self.title = title self.subtitle = subtitle self.caption = caption self.struck = struck self.opens = opens self.card = card self.database = database } init(_ json: JSONValue) { kind = json.text("kind") ?? "" recordId = json.text("id") ?? "" title = json.text("title") ?? "" subtitle = json.text("subtitle") caption = json.text("caption") struck = json.flag("struck") opens = json["opens"].flatMap { opens in opens.text("path").flatMap { path in Route.parse(path + (opens.text("at").map { "?at=\($0)" } ?? "")) } } card = json["record"].flatMap { record in record.text("table").flatMap { table in record.text("id").map { Card(table: table, id: $0) } } } database = FeedRowDatabase(json) } /// The list identity: a kind and an id after the hit's database, because /// two kinds can carry one id and one id in two databases is two hits. var key: String { FeedRowDatabase.key(database, id: "\(kind):\(recordId)") } }