// What the faces read off a card's facts: the money, the passwords and the// studio's faces each list a card's facts (`facts`), a label and its words.// And the studio's own readings: a class, a clock's figures, a number's// figure, its seats and their share. Which face a studio's card wears is its// kit's (`sharmila.trainer`, `sharmila.studio`, …); a class's time is the// app's (CottonUI's `StudioFaces`).importCottonPaperimportCottonValues/// What the studio faces read off a card.publicenumStudioReading {/// A card's facts (`facts`): each one's label and words.publicstaticfunc facts(_ card: DeckCard) -> [(label: String?, said: String)] { card.entries("facts").compactMap { entry inguardlet said = entry["said"]?.text, !said.isEmpty else { returnnil }return (entry["label"]?.text, said) } }/// An event card with a picture is a class.publicstaticfunc isClass(_ card: DeckCard) -> Bool { card.text("photo") != nil }/// A time as the editorial figures and the clock's word after them/// ("11:30 am" → "11:30", "am"; "19:30" → "19:30", nil); nil when the/// words are no clock (an event's kind), which the face sets as a line.publicstaticfunc clock(_ time: String) -> (figures: String, suffix: String?)? {guard time.first?.isNumber == trueelse { returnnil }let figures = String(time.prefix { $0.isNumber || $0 == ":" || $0 == "." })let suffix = time.dropFirst(figures.count).trimmingCharacters(in: .whitespaces)return (figures, suffix.isEmpty ? nil : suffix) }/// The core's words for a number split into the figure drawn large and/// the words after it ("148 sessions attended" → "148", "sessions/// attended"); nil when the words do not open on a figure.publicstaticfunc figure(_ words: String?) -> (figure: String, words: String?)? {guardlet words, words.first?.isNumber == trueelse { returnnil }let figure = String(words.prefix { $0.isNumber || $0 == "," || $0 == "." })let rest = words.dropFirst(figure.count).trimmingCharacters(in: .whitespaces)return (figure, rest.isEmpty ? nil : rest) }/// The places taken and the room (`used` of `limit`), when the card/// names both and they make sense.publicstaticfunc seats(_ card: DeckCard) -> (taken: Int, of: Int)? {guardlet used = card.fields["used"]?.number, let limit = card.fields["limit"]?.number, limit > 0, used >= 0else { returnnil }return (min(Int(used), Int(limit)), Int(limit)) }/// How far a ring or a bar is drawn: taken of the whole, never below/// empty or past full.publicstaticfunc ringShare(taken: Int, of: Int) -> Double {guard of > 0else { return0 }return min(1, max(0, Double(taken) / Double(of))) }/// A share as a whole percent, rounded (2 of 12 → 17).publicstaticfunc percent(taken: Int, of: Int) -> Int {Int((ringShare(taken: taken, of: of) * 100).rounded()) }}