Cotton code
108 lines · 5.0 KB · SwiftRaw
// The build line (phone diagnostics, architect's brief of 12 Oct 2026): one// faint line at the bottom of the Databases sheet and of This phone / This// Mac that says which build this is and which Mac it last heard from —////   "Cotton 1.0.0 (254) · c76d108  ·  Mac mac-c 0.29.37"//// Left: this app's version, build number and short commit — constants of the// binary. Right: the serving Mac's name and version as this device last heard// them (`read.syncStatus`'s `loop.peer`, kept by the sync loop from the// exchange answer and the schema a 400 or 426 names). Red when the two schemas differ, but no schema number is ever// written on it: a person reads this line, and the rule since the opening// card is that no schema number stands anywhere a person reads (walker 298:// "schema 139" twice on the Databases sheet). Pure, so the line is the same// wherever it is drawn.import Foundationpublic struct BuildLine: Equatable, Sendable {    /// The Mac across the exchange, as the sync loop last heard it.    public struct Peer: Equatable, Sendable {        public var name: String?        public var version: String?        public var schema: Int?        public init(name: String? = nil, version: String? = nil, schema: Int? = nil) {            self.name = name            self.version = version            self.schema = schema        }        /// `read.syncStatus`'s `loop.peer`; `nil` when the loop has heard        /// nothing yet.        public static func of(syncStatus: JSONValue?) -> Peer? {            guard let peer = syncStatus?["loop"]?["peer"] else { return nil }            let heard = Peer(                name: peer["name"]?.string,                version: peer["version"]?.string,                schema: peer["schema"]?.int)            return heard == Peer() ? nil : heard        }    }    /// This build in the one form every screen says it in — "Cotton 1.0.0    /// (286) · 9c19043" — the Databases foot, the dev corner and About alike,    /// so two screens never name one build two ways (architect, 16 Oct 2026).    /// A part the build was not stamped with says so. No schema: a person    /// reads it. A business app says its own name: "Cotton" appears nowhere    /// in a business flavour (`BusinessFlavourWordsTests`).    public static func own(app: String = app, version: String, build: String, commit: String) -> String {        let short = commit.isEmpty ? "unstamped" : String(commit.prefix(7))        let numbers = "\(version.isEmpty ? "?" : version) (\(build.isEmpty ? "?" : build))"        return "\(app) \(numbers) · \(short)"    }    /// The app's own name: the business's in a business flavour, else Cotton.    public static var app: String { name(of: FlavourManifest.current) }    public static func name(of manifest: FlavourManifest?) -> String { manifest?.name ?? "Cotton" }    public let text: String    /// The two schemas differ — the line is drawn red.    public let mismatched: Bool    /// - Parameters:    ///   - build: `CFBundleVersion`; `commit`: `CottonBuildCommit`.    ///   - schema: this build's `SCHEMA_VERSION` (`build.facts`).    public init(version: String = "", build: String, commit: String, schema: Int?, peer: Peer?) {        let own = Self.own(version: version, build: build, commit: commit)        let other: String        if let peer {            let name = peer.name.map { "Mac \($0)" } ?? "Mac"            let version = peer.version.map { " \($0)" } ?? ""            other = name + version        } else {            other = "no Mac heard yet"        }        text = "\(own)  ·  \(other)"        mismatched = schema != nil && peer?.schema != nil && peer?.schema != schema    }}/// WHICH PROCESS DREW A PROBLEM CARD: its bundle id, version and build, and/// the commit it was built from — "com.cottondb.cotton · 1.0.0 (256) ·/// 5e7ce0f". Drawn at the foot of every card that offers "Send a report", so/// a page from a second install or an old build cannot be read as this one's/// (12 Oct 2026: a page an old schema-93 build drew was read for an hour as/// build 256's, because nothing on it said which app it came from).public struct ProcessStamp: Equatable, Sendable {    public let text: String    public init(bundleId: String, version: String, build: String, commit: String) {        let app = bundleId.isEmpty ? "unknown bundle" : bundleId        let numbers = "\(version.isEmpty ? "?" : version) (\(build.isEmpty ? "?" : build))"        let short = commit.isEmpty ? "unstamped" : String(commit.prefix(7))        text = "\(app) · \(numbers) · \(short)"    }    public static func of(_ bundle: Bundle = .main) -> ProcessStamp {        let string = { (key: String) -> String in            (bundle.object(forInfoDictionaryKey: key) as? String) ?? ""        }        return ProcessStamp(            bundleId: bundle.bundleIdentifier ?? "",            version: string("CFBundleShortVersionString"),            build: string("CFBundleVersion"),            commit: string("CottonBuildCommit"))    }}