// The app's own voice — what it says when no record speaks — as the core// resolved it (`shell.lines`, `crates/cotton-ffi/src/methods/shell_lines.rs`).//// THE SHELL'S LINES ARE ROWS (architect, 21 Oct 2026): every such sentence is// a row of the shown database's `lines` table, the kit's words for one it has// none of. The core resolves each set once per database and again after a// sync (`ShellLines.refresh`, CottonKit); a screen reads the set this process// last received.//// Before the core has answered (a refusal, a first join, an extension with// no core, a face drawn in Loom), or for a set an older core does not ship, a// line is the kit's copy snapshot bundled here (`Resources/lines/<set>.json`,// every kit line file copied byte for byte, held so by// `the_apps_line_snapshot_is_the_kits_sets`)// — never a literal written here, and never a line file this code opens// itself. It lives in CottonValues, with no core, so a face says its words// the same in the app and in Loom (Loom, 28 Oct 2026), and the Mac's host// code reads the same lines through CottonKit. No composition: a line is its// template with its `{placeholders}` filled by typed values.importFoundationpublicenumShellLines {// No list of set names lives here: the sets are the kit's files, and the// core names them (`SHIPPED`) — the app reads every set in one call and// keeps what comes, and the bundled sets are whatever the snapshot holds. A list here was a copy of the kit's file list, and the guard// `NoTableInGenericSurfacesTests` read "bin" and "backup" in it as tables./// One set: each line's words by its dotted path.typealiasLines = [String: String]/// Each set as the core last resolved it, by set id.privatestaticlet received = Received()/// Whose words these are: this app's name, as its manifest/// (`flavour.json`) says it, read once. A test sets a business's to read/// the lines as its customer would. nonisolated(unsafe) publicstaticvar speaker: String? = bundledName(of: .main)/// Each set as the bundled snapshot has it, read once: every file in the/// bundle's `lines` folder is one set, named by its file.privatestaticlet bundled: [String: Lines] = {let urls = CottonValuesResources.bundle?.urls(forResourcesWithExtension: "json", subdirectory: "lines") ?? []var sets: [String: Lines] = [:]for url in urls {guardlet data = try? BundledJSON.read(url),let set = (try? JSONSerialization.jsonObject(with: data)) as? [String: Any]else { continue } sets[url.deletingPathExtension().lastPathComponent] = flat(set) }return sets }()/// The line at dotted `path` of `set`, its `{name}`s replaced by `fills`./// A line neither the core nor the bundled kit has is said as its own/// path, so a missing line shows where it is missing rather than nothing.publicstaticfunc line(_ set: String, _ path: String, _ fills: [String: String] = [:]) -> String { shipped(set, path, fills) ?? "\(set).\(path)" }/// The line at `path` of `set`, or nil when neither the core nor the/// bundled kit has it: for a path chosen at run time (a refusal's code),/// whose reader says its general line instead of a dotted path.publicstaticfunc shipped(_ set: String, _ path: String, _ fills: [String: String] = [:]) -> String? {let fromCore = received.set(set)guardlet template = fromCore?[path] ?? bundled[set]?[path] else { returnnil }// Said in this app's name: a business flavour never says Cotton.let filled = fills.reduce(template) { $0.replacingOccurrences(of: "{\($1.key)}", with: $1.value) }return said(filled, as: speaker) }/// The immediate line keys beneath a path, in their data order by key.publicstaticfunc keys(_ set: String, _ path: String) -> [String] {let prefix = path + "."let lines = received.set(set) ?? bundled[set] ?? [:]return lines.keys.compactMap { key inguard key.hasPrefix(prefix) else { returnnil }let child = String(key.dropFirst(prefix.count))return child.contains(".") ? nil : child }.sorted() }/// The sets the core resolved (`shell.lines`'s `sets`), each kept in/// place of the one before it.publicstaticfunc receive(_ sets: [String: Any]) {for (set, lines) in sets {guardlet lines = lines as? [String: Any] else { continue } received.keep(set, flat(lines)) } }/// A sentence as an app named `name` says it: the word Cotton becomes the/// app's own name, so a business's customer never reads Cotton on a gate/// screen, in a refusal or in an error (lane phonewalk, 19 Oct 2026);/// Cotton's own build, or no name, says Cotton.publicstaticfunc said(_ text: String, as name: String?) -> String {guardlet name, name != "Cotton", text.contains("Cotton") else { return text }// An app whose own name starts with Cotton (Cotton Project) leaves// its name as it is, rather than saying "Cotton Project Project".let rest = name.hasPrefix("Cotton") ? String(name.dropFirst("Cotton".count)) : ""// "Cotton" then a capitalised word names a Cotton app (Cotton Voice,// Cotton Meetings): that one is never renamed.let pattern = #"\bCotton(DB for Mac)?\b(?! \p{Lu})"# + (rest.isEmpty ? "" : "(?!\(NSRegularExpression.escapedPattern(for: rest)))")return text.replacingOccurrences(of: pattern, with: name, options: .regularExpression) }/// The app's name as `bundle`'s manifest (`flavour.json`) says it, or nil/// when the bundle carries none (a test runner, Loom).staticfunc bundledName(of bundle: Bundle) -> String? {guardlet url = bundle.url(forResource: "flavour", withExtension: "json"),let data = try? BundledJSON.read(url),let manifest = (try? JSONSerialization.jsonObject(with: data)) as? [String: Any]else { returnnil }return manifest["name"] as? String }/// A line file's strings by dotted path (`banner.body.phone`).privatestaticfunc flat(_ node: [String: Any], _ prefix: String = "") -> Lines {var lines: Lines = [:]for (key, value) in node {let path = prefix.isEmpty ? key : "\(prefix).\(key)"iflet words = value as? String { lines[path] = words } elseiflet inner = value as? [String: Any] { lines.merge(flat(inner, path)) { $1 } } }return lines }/// The sets the core sent, behind a lock: read from any thread.privatefinalclassReceived: @unchecked Sendable {privatelet lock = NSLock()privatevar sets: [String: Lines] = [:]func set(_ id: String) -> Lines? { lock.withLock { sets[id] } }func keep(_ id: String, _ lines: Lines) { lock.withLock { sets[id] = lines } } }}/// A file the app bundles, read with a bound: a packaging error is refused/// before a large file is buffered. CottonUI's `readBundledJSON` is this.publicenumBundledJSON {publicstaticfunc read(_ url: URL, limit: Int = 1_048_576) throws -> Data {let file = tryFileHandle(forReadingFrom: url)defer { try? file.close() }let data = try file.read(upToCount: limit + 1) ?? Data()guard data.count <= limit else { throwCocoaError(.fileReadTooLarge) }return data }}/// CottonValues' resource bundle: `Bundle.module` on the phone; on the Mac it/// is looked for by path, as `CottonResources` finds the look's — a packaged/// app's `Contents/Resources/` first, then beside the executable and up to/// three directories above it (`swift build`, `swift test`).enumCottonValuesResources {staticlet bundleName = "Loom_CottonValues.bundle"staticlet bundle: Bundle? = { #if canImport(UIKit)returnBundle.module #elsereturn locate(from: Bundle.main) ?? locate(from: Bundle(for: Marker.self)) #endif }()privatefinalclassMarker {}staticfunc locate(from main: Bundle) -> Bundle? {var candidates: [URL] = []iflet resources = main.resourceURL { candidates.append(resources.appendingPathComponent(bundleName)) }var directory = main.bundleURLfor _ in0..<4 { candidates.append(directory.appendingPathComponent(bundleName)) directory.deleteLastPathComponent() }for candidate in candidates whereFileManager.default.fileExists(atPath: candidate.path) {iflet found = Bundle(url: candidate) { return found } }returnnil }}