Cotton code
272 lines · 13.9 KB · SwiftRaw
// FlavourManifest.swift — which app this build is.//// One tree builds Cotton and, beside it, Cotton Development and one app per// business (`project.yml`'s `businessFlavours`). Each bundle carries its// manifest as `flavour.json`: its name, how many databases it shows and which// (`database`), and its Home — the board its root page// draws, section by section, which the core holds in place of Cotton's Home.// Cotton's manifest (`App/CottonManifest`) names Personal and no Home, so it// draws the shipped one. A bundle with none — the Mac app, a test bundle —// shows every database. Its fan holds the database's tools, package// records: an app whose manifest names its kit (`"kit": "books"`) keeps to// the kit's tools and tables, signed in or not, and one that names none// (Cotton) to the whole object; the core reads it (`kit::AppKit`), never// the screens. The app reads it once at launch and// hands it to the core unchanged (`CottonConfig.manifest`); the screens read the// same value here.import Foundationpublic struct FlavourManifest: Codable, Equatable, Sendable {    /// The flavour's name in `project.yml` — `sharmila`.    public var flavour: String    /// Whether a fresh install may start its own Personal database.    public var genesis: Genesis    public enum Genesis: String, Codable, Sendable { case personal, none }    /// The app's name, as the business says it.    public var name: String    /// `single`: one database shown, no caps and no Databases screen;    /// `only`: one database ever.    public var databases: Databases    /// Which database the app shows — `"personal"`, or one database by its id    /// and the host it signs in at: `{"id", "host", "named"}`. None is every    /// database it holds. The core reads it (`shell::scope`).    public var database: JSONValue?    /// The app's Home: a board (`boards/home.json`'s shape) whose parts are the    /// root page's sections, in order. Handed to the core whole, which holds    /// it as this app's Home. None: the shipped Home (Cotton's own); every    /// business app names one (`HomeNeverMeTests`).    public var home: JSONValue?    /// Which of Cotton's own cards the dial holds beside the tools (`fan`):    /// absent, Home and Tools; Rigby holds no Home and no Tools card and ends    /// in a Settings card (owner and architect, 22 Oct 2026); Cotton keeps    /// Home and Tools and ends in the same Settings card, so its owner exports    /// from it as from Rigby (the owner, 23 Oct 2026).    public var fan: Fan    public struct Fan: Codable, Equatable, Sendable {        public var home: Bool        public var tools: Bool        /// A Settings card last on the dial: the settings list, reachable        /// from every page by the dial.        public var settings: Bool        /// The tool the dial opens on; with Home kept beside it, Home is        /// dealt last (Cotton Music). Nil, the dial opens on Home, else its        /// first card.        public var opens: String?        /// The app's face is one stream of the conversations of the tool it        /// opens on (Cotton Messages): no fan and no hub are drawn, and the        /// core names the chats (`shell.stream`). Absent, the fan.        public var stream: Bool        public init(home: Bool = true, tools: Bool = true, settings: Bool = false, opens: String? = nil, stream: Bool = false) {            self.home = home            self.tools = tools            self.settings = settings            self.opens = opens            self.stream = stream        }        public init(from decoder: Decoder) throws {            let keys = try decoder.container(keyedBy: CodingKeys.self)            home = try keys.decodeIfPresent(Bool.self, forKey: .home) ?? true            tools = try keys.decodeIfPresent(Bool.self, forKey: .tools) ?? true            settings = try keys.decodeIfPresent(Bool.self, forKey: .settings) ?? false            opens = try keys.decodeIfPresent(String.self, forKey: .opens)            stream = try keys.decodeIfPresent(Bool.self, forKey: .stream) ?? false        }        /// Written as the manifest says it: only the keys that differ from        /// Cotton's own answer, which is what the core reads an absent key        /// as (`FanCards`) — so `{"tools": false}` crosses as written and not        /// as a whole dial the file never said (Nancy's, 23 Oct 2026).        public func encode(to encoder: Encoder) throws {            var keys = encoder.container(keyedBy: CodingKeys.self)            if !home { try keys.encode(home, forKey: .home) }            if !tools { try keys.encode(tools, forKey: .tools) }            if settings { try keys.encode(settings, forKey: .settings) }            try keys.encodeIfPresent(opens, forKey: .opens)            if stream { try keys.encode(stream, forKey: .stream) }        }        private enum CodingKeys: String, CodingKey { case home, tools, settings, opens, stream }        /// The dial of a manifest that says no `fan`: Home and Tools, no        /// Settings card. Cotton's own until its manifest named the Settings        /// card (the owner, 23 Oct 2026); every business app's still.        public static let cottons = Fan()    }    /// Whether this app syncs at all (`sync`): absent, true, as in Cotton.    /// Rigby says false — one device, no Mac — and the core reads the same    /// key from the manifest it is handed (architect, 22 Oct 2026).    public var sync: Bool    /// The business's object this app runs against (`business`): the    /// Worker's address and the business's slug. Named, the app opens on the    /// business's shell (`CustomerShell`), every page drawn from what the    /// object answers; none, it is Cotton's frame. Cotton's names its server    /// per side of the build, so Cotton Dev signs in at the dev Worker    /// (`BusinessAddress`).    public var business: BusinessAddress?    /// The ground this app stands on (`ground`), a `FaceGround` by its id:    /// the material its icon's object stands on (the calendar's kitchen tile,    /// the people's amethyst desk). The launch screen and the splash are laid    /// on its tile, and wherever a card or a page would lie on    /// the shell's paper the app lays its ground instead. Absent, Cotton's    /// paper (Cotton, Rigby, a business app).    public var ground: String?    /// The ground this app LAUNCHES on (`launch`), where it stands on none of    /// its own: Messages' chats lie on the shell's paper, and its launch    /// screen and splash are still its icon's pegboard with the object on it    /// (owner, 15 Nov 2026: build 5 launched bare, because `ground` was the    /// one switch for both). Absent, the app launches on `ground`.    public var launch: String?    /// The owner's personal object this app's database is (Cotton): a    /// personal app whose manifest names an object. It signs in as a business    /// app does and then opens on Cotton's own frame, its decks reading the    /// phone's copy (`personal.connect`).    public var personalObject: BusinessAddress? { genesis == .personal ? business : nil }    /// THE FILE AS THE BUNDLE CARRIES IT, every key — what crosses to the core    /// (`CottonConfig.manifest`) with this struct's own fields laid over it.    /// The core reads keys the app never does (`database`, `web`); re-encoding    /// only this struct's fields dropped them, and Cotton Development opened    /// on Personal (walk, 19 Oct 2026).    private var whole: [String: JSONValue] = [:]    public enum Databases: String, Codable, Sendable {        /// One database shown at a time.        case single        case many        /// Exactly one database ever: no new, no join, no Databases screen        /// (Rigby; architect, 22 Oct 2026). Shown as `single` everywhere.        case only    }    private enum CodingKeys: String, CodingKey {        case flavour, genesis, name, databases, database, home, business, fan, sync, ground, launch    }    public init(from decoder: Decoder) throws {        let keys = try decoder.container(keyedBy: CodingKeys.self)        flavour = try keys.decode(String.self, forKey: .flavour)        if keys.contains(.genesis) { genesis = try keys.decode(Genesis.self, forKey: .genesis) }        else { genesis = .personal }        name = try keys.decode(String.self, forKey: .name)        databases = try keys.decode(Databases.self, forKey: .databases)        database = try keys.decodeIfPresent(JSONValue.self, forKey: .database)        home = try keys.decodeIfPresent(JSONValue.self, forKey: .home)        business = try keys.decodeIfPresent(BusinessAddress.self, forKey: .business)        fan = try keys.decodeIfPresent(Fan.self, forKey: .fan) ?? .cottons        sync = try keys.decodeIfPresent(Bool.self, forKey: .sync) ?? true        ground = try keys.decodeIfPresent(String.self, forKey: .ground)        launch = try keys.decodeIfPresent(String.self, forKey: .launch)        if case .object(let all) = try JSONValue(from: decoder) { whole = all }    }    public func encode(to encoder: Encoder) throws {        var all = whole        all["flavour"] = .string(flavour)        all["genesis"] = .string(genesis.rawValue)        all["name"] = .string(name)        all["databases"] = .string(databases.rawValue)        all["database"] = database        all["home"] = home        // Absent is Home and Tools, so a manifest that never said it stays unsaid.        all["fan"] = fan == .cottons ? nil : try JSONDecoder().decode(JSONValue.self, from: JSONEncoder().encode(fan))        // Absent is a syncing app, so a manifest that never said it stays unsaid.        all["sync"] = sync ? nil : .bool(false)        all["ground"] = ground.map(JSONValue.string)        all["launch"] = launch.map(JSONValue.string)        all["business"] = try business.map { try JSONDecoder().decode(JSONValue.self, from: JSONEncoder().encode($0)) }        try JSONValue.object(all).encode(to: encoder)    }    public init(flavour: String, name: String, databases: Databases, genesis: Genesis = .personal, database: JSONValue? = nil, home: JSONValue?, business: BusinessAddress? = nil, fan: Fan = .cottons, sync: Bool = true, ground: String? = nil) {        self.flavour = flavour        self.genesis = genesis        self.name = name        self.databases = databases        self.database = database        self.home = home        self.business = business        self.fan = fan        self.sync = sync        self.ground = ground    }    /// Whether the app launches flat (flavour.json `flat`): one colour of its    /// own, light and dark, laid by its launch storyboard as `LaunchGround`    /// (`scripts/gen-launch-screens.py`), with no ground's tiles under it.    public var flat: Bool { whole["flat"] != nil }    /// The bundle resource a business app carries.    public static let resource = "flavour"    /// A bundle's manifest: nil when it carries none (Cotton). A manifest that    /// is there and does not read is a broken build, said out loud rather than    /// opened as Cotton.    public static func of(_ bundle: Bundle) -> FlavourManifest? {        guard let url = bundle.url(forResource: resource, withExtension: "json") else { return nil }        do {            return try JSONDecoder().decode(FlavourManifest.self, from: Data(contentsOf: url))        } catch {            fatalError("flavour.json in \(bundle.bundlePath) does not read: \(error)")        }    }    /// This process's manifest.    public static let current: FlavourManifest? = of(.main).map(onThisMachine)    /// The launch arguments a SIMULATOR build may take in place of the    /// manifest's business, so a walk runs against a synthetic business    /// served on the Mac beside it    /// (`-businessServer http://127.0.0.1:8787 -businessSlug <slug>`).    public static let businessServerArgument = "businessServer"    public static let businessSlugArgument = "businessSlug"    /// The manifest as this machine runs it. On a device it is the bundle's,    /// unchanged — a phone in a customer's hand signs in only where the    /// business's manifest says (`FlavourOverrideTests`).    static func onThisMachine(_ manifest: FlavourManifest) -> FlavourManifest {        #if targetEnvironment(simulator)        let arguments = UserDefaults.standard        return manifest            .servingBusiness(                server: arguments.string(forKey: businessServerArgument),                slug: arguments.string(forKey: businessSlugArgument))        #else        return manifest        #endif    }    /// This manifest with its business served elsewhere: both named, or    /// unchanged; a manifest that names no business is unchanged. A manifest    /// at the open host (Cotton's, no slug) moves its server alone: the    /// override never makes Cotton a business (`FlavourOverrideTests`).    public func servingBusiness(server: String?, slug: String?) -> FlavourManifest {        guard let business, let server, !server.isEmpty else { return self }        var served = self        if business.slug == nil {            served.business = BusinessAddress(server: server, window: business.window)            return served        }        guard let slug, !slug.isEmpty else { return self }        served.business = BusinessAddress(server: server, slug: slug, window: business.window)        return served    }    /// One database on screen: no caps row and no Databases screen.    public var isSingleDatabase: Bool { databases != .many }}extension FlavourManifest {    /// A sentence as this app 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). Every table    /// sentence goes through here (`CottonStrings.local`), and so does every    /// error's words (`CottonCallError`); Cotton's own build says Cotton.    public static func said(_ text: String, by manifest: FlavourManifest? = current) -> String {        ShellLines.said(text, as: manifest?.name)    }}