// Reading one screen's answer. // // A `read.*` answer is a tree the boundary owns, so a screen asks it for what // it expects and gets nothing when the value is something else — the same // bargain `JSONValue` already makes, with the shapes this lane reads for often // enough to be worth a name. import Foundation extension JSONValue { /// The members of an array, or none — so `ForEach` over an absent list /// draws an empty section rather than crashing. public var rows: [JSONValue] { array ?? [] } /// A string member, trimmed to nothing when it is blank. /// /// A column that is present and empty is the same absence as a column that /// is not there: both render as a missing line, and a screen that treated /// them differently would draw one blank row and one nothing. public func text(_ key: String) -> String? { guard let value = self[key]?.string else { return nil } let trimmed = value.trimmingCharacters(in: .whitespacesAndNewlines) return trimmed.isEmpty ? nil : trimmed } /// A string member EXACTLY as it was stored, blank lines and all. /// /// For the one case `text(_:)` cannot serve: a column whose whitespace is /// CONTENT, seeded into an editor that will write it back. A scene's /// Fountain body is the source of truth, so trimming it on the way into /// the field silently rewrites the writer's page on the next save. public func untrimmed(_ key: String) -> String? { self[key]?.string } public func number(_ key: String) -> Int? { self[key]?.int } /// A number that is not a count: a sort position, a coordinate, a /// confidence. `number(_:)` truncates to `Int`, which is right for a count /// and silently wrong for `0.5`. public func decimal(_ key: String) -> Double? { self[key]?.double } /// A flag, where absent is FALSE. The crate answers a boolean for every /// flag column it reads, so an absence here is a column the screen asked /// for and the read does not carry. public func flag(_ key: String) -> Bool { self[key]?.bool ?? false } /// A flag with THREE answers: true, false, and *the server did not say*. /// /// `flag(_:)` is right wherever an absence is a false — a column the read /// carries for every row. This is for the keys where `null` is a state of /// its own and reading it as `false` would be an invented answer: `serving` /// on a device is the one, where `null` means the server answering cannot /// speak for that machine and `false` would mean it knows no Mac is /// serving. public func maybeFlag(_ key: String) -> Bool? { self[key]?.bool } /// The list a key holds, or none. public func list(_ key: String) -> [JSONValue] { self[key]?.array ?? [] } /// The strings a key's array holds, skipping anything that is not one. public func strings(_ key: String) -> [String] { list(key).compactMap(\.string) } } /// A meta line: the facts under a row's title, joined the way every list in /// this app joins them. /// /// ` · ` and nothing else, and an absent fact contributes NOTHING rather than /// an empty segment — "32 documents · · 2 chats" is what a join over /// optionals gives you if nobody says otherwise. public func metaLine(_ parts: String?...) -> String? { metaLine(parts) } /// The same join over a list built up a fact at a time — a meta line whose /// parts are decided by conditions rather than written out. public func metaLine(_ parts: [String?]) -> String? { let kept = parts.compactMap { $0 }.filter { !$0.isEmpty } return kept.isEmpty ? nil : kept.joined(separator: " · ") } /// `1 thing` / `2 things` — the one place this lane counts in English. public func plural(_ count: Int, one: String, many: String) -> String { count == 1 ? one : many }