// // LibrarySearch.swift // Museion // // #144 (FEAT-004): the library search's pure core — extracted from the // landed scan so ranking/partitioning is testable // (MuseionTests/LibrarySearchTests.swift) while PostsLibraryColumn's // structured .task stays the runtime path. // import Foundation /// #144: the library search's pure core — title matches first, then body /// matches, both halves preserving the scan's title order (#108: Finder /// order, never re-sorted). nonisolated + the injected `@Sendable` reader /// keep the seam pure: `bodyOf` is `PostStore.readBody` at the call site /// (frontmatter never searched — stripping is readBody's contract). nonisolated enum LibrarySearch { /// The query is trimmed here; empty (or whitespace-only) matches /// nothing — the caller renders the sectioned library instead. static func results( for query: String, in candidates: [Post], bodyOf: @Sendable (Post) -> String?, ) -> [Post] { let needle = query.trimmingCharacters(in: .whitespacesAndNewlines) guard !needle.isEmpty else { return [] } let titleHits = candidates.filter { $0.title.localizedCaseInsensitiveContains(needle) } let bodyHits = candidates.filter { !$0.title.localizedCaseInsensitiveContains(needle) && (bodyOf($0) ?? "").localizedCaseInsensitiveContains(needle) } return titleHits + bodyHits } /// #144 (mounted half): the identity of one search sweep — the /// trimmed query, the Home it ran under, and the library content /// revision it saw (`PostsModel.contentRevision`). The column's /// `.task(id:)` re-keys on ANY difference, so a keystroke, a Home /// repoint, or a library write that landed after the last sweep /// restarts it instead of serving the stale snapshot. nonisolated struct SearchKey: Equatable { var query: String var homePath: String var revision: Int } /// #144 (mounted half): one landed sweep — the results plus the key /// that produced them. `isFresh` is the column's stale gate: results /// render only under the key that computed them, so an archived, /// trashed, renamed, or since-edited library drops to the recompute /// state, never stale rows. nonisolated struct Snapshot: Equatable { var key: SearchKey var results: [Post] /// A snapshot renders only under its own key. func isFresh(for key: SearchKey) -> Bool { self.key == key } } }