// 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.importCottonKitimportFoundation/// One section of the answer: a built-in kind's or an imported table's.structFindSection: 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 = idself.words = wordsself.count = countself.defined = definedself.kinds = kindsself.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.staticfunc all(_ answer: JSONValue) -> [FindSection] { answer.list("sections").map(FindSection.init) }}/// One hit, as its section lists it.structFindHit: Equatable, Sendable {/// The record a hit stands for: the one its card is read from.structCard: Equatable, Sendable {let table: Stringlet id: String }let kind: Stringlet recordId: Stringlet 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 = kindself.recordId = idself.title = titleself.subtitle = subtitleself.caption = captionself.struck = struckself.opens = opensself.card = cardself.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 inRoute.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)") }}