Cotton code
74 lines · 3.6 KB · SwiftRaw
// How far a write to a record that repeats reaches.//// One day of a repeating event is not a row of its own: a change or a delete// of it could mean that day or every day. The core never guesses (architect,// 5 Oct: one day's new time moved a whole series) — it refuses such a write// without a `scope` — and it marks the records that ask (`scope_columns`), so// the app asks "Only this one" or "Every one" before it writes, on any table// whose engine says so, never by knowing which table repeats.import CottonValuesimport Foundation/// The reach of a write to a record that repeats.public enum WriteScope: String, Sendable {    /// Only this one: the day becomes its own record; the rest are untouched.    case this    /// Every one: the whole series.    case all}/// A store that cannot say how far a write reaches was handed one that must.public struct ScopeNotCarried: LocalizedError, Equatable {    public var errorDescription: String? { ShellLines.line("definitions", "scope.not_carried") }}extension TableRecord {    /// The columns whose change asks how far it reaches, as the core lists    /// them on a record that repeats. Not a column of any definition; absent    /// on a record that does not repeat.    public static let scopeColumnsKey = "scope_columns"    /// Whether this record repeats: a delete of it asks how far it reaches.    public var repeats: Bool { !scopeColumns.isEmpty }    var scopeColumns: [String] {        guard case .list(let keys) = self[Self.scopeColumnsKey] else { return [] }        return keys.compactMap(\.text)    }}/// The question asked before a write to a record that repeats: two answers,/// "Only this one" and "Every one" — or "Every one" alone where the change/// belongs to all of them (how it repeats).public struct ScopeQuestion: Equatable, Sendable {    public static var onlyThis: String { ShellLines.line("definitions", "scope.only_this") }    public static var everyOne: String { ShellLines.line("definitions", "scope.every_one") }    public let title: String    public let message: String    /// False where only the whole series can take the change.    public let offersThis: Bool    /// It removes something (red ink).    public let destructive: Bool    /// Before a change to `column` of a record that repeats.    public static func change(_ record: TableRecord, column: ColumnDefinition, in definition: TableDefinition) -> ScopeQuestion {        let name = record.said?.slip.name ?? ShellLines.line("definitions", "scope.this", ["name": definition.name.lowercased()])        // How it repeats is every one's: the core takes that change for all only.        let whole = column.format == .repeatRule        return ScopeQuestion(            title: ShellLines.line("definitions", "scope.repeats", ["name": name]),            message: ShellLines.line("definitions", whole ? "scope.change_whole" : "scope.change_question"),            offersThis: !whole, destructive: false)    }    /// Before a delete of a record that repeats; `bin` is the table's own    /// sentence for where a deleted record goes.    public static func delete(_ record: TableRecord, in definition: TableDefinition, bin: String) -> ScopeQuestion {        ScopeQuestion(            title: ShellLines.line("definitions", "scope.repeats", ["name": record.said?.slip.name ?? ShellLines.line("definitions", "scope.this", ["name": definition.name.lowercased()])]),            message: bin.isEmpty ? ShellLines.line("definitions", "scope.delete_question") : ShellLines.line("definitions", "scope.delete_question_bin", ["bin": bin]),            offersThis: true, destructive: true)    }}