refactor(library): extract search seam (#144)
CI / ci (push) Canceled after 0s
CI / ci (push) Canceled after 0s
Mount the pure LibrarySearch.results(for:in:bodyOf:) core inside the landed structured scan: the nonisolated async search() keeps its shape (off-main, same task, .task(id: trimmedQuery) untouched) and now delegates to the seam with PostStore.readBody as the injected reader; the reader's per-piece Task.isCancelled guard preserves the sweep's between-files cancellation of body reads. No behavior change vs 53c314d; the seam exists so the ranking logic is testable.
This commit is contained in:
1 parent
9b1fa75907
commit
08005d322f
2 files changed
+45
-19
No files matched your search
@@ -0,0 +1,35 @@
|
||||
//
|
||||
// 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
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user