Repository Public
1import Foundation23/// Text of a range reference with its header-row flag.4public struct RangeValue: Sendable, Hashable {5 public var text: String6 public var hasHeader: Bool7 public init(text: String = "", hasHeader: Bool = true) { self.text = text; self.hasHeader = hasHeader }8}910public enum FieldValue: Sendable, Hashable {11 case range(RangeValue)12 case text(String)13 case number(Double)14 case bool(Bool)15 /// Offset of the chosen column inside the range (nil = none).16 case column(Int?)17 case columns([Int])18 case date(Date)19}2021public struct FieldOption: Sendable, Hashable, Identifiable {22 public var id: String23 public var label: String24 public init(_ id: String, _ label: String) { self.id = id; self.label = label }25}2627public enum NumberStyle: Sendable, Hashable {28 case plain29 /// Stored as a fraction, edited as a percentage (0.05 ↔ "5").30 case percent31}3233public enum FieldKind: Sendable {34 /// A data range: A1 text + "Use Selection" + header toggle. `preset` fills the field from the selection when the tool opens.35 case range(preset: Bool)36 /// One column of the range field `range`.37 case column(range: String, numericOnly: Bool, optional: Bool, defaultIndex: Int?)38 /// Several columns of the range field `range`.39 case columns(range: String, numericOnly: Bool, defaultAll: Bool)40 case choice([FieldOption], default: String)41 case number(default: Double, min: Double?, max: Double?, style: NumberStyle, unit: String?)42 case integer(default: Int, min: Int, max: Int)43 case toggle(default: Bool)44 case text(default: String, placeholder: String)45 /// One cell ("B3", "Model!C7") with a "Use Selection" button; stored as text.46 case cell(default: String)47 /// Cells and ranges separated by commas; stored as text.48 case cells(default: String)49 case date(default: Date)50 /// A sub-heading inside the form (no value).51 case section52}5354public struct FormField: Sendable, Identifiable {55 public var id: String56 public var label: String57 public var kind: FieldKind58 public var help: String?59 /// The field is shown (and validated) only when this returns true.60 public var isVisible: @Sendable (FormValues) -> Bool6162 public init(_ id: String, _ label: String, _ kind: FieldKind, help: String? = nil,63 visible: @escaping @Sendable (FormValues) -> Bool = { _ in true }) {64 self.id = id; self.label = label; self.kind = kind; self.help = help; self.isVisible = visible65 }6667 // Shorthands68 public static func range(_ id: String = "data", _ label: String, help: String? = nil) -> FormField { FormField(id, label, .range(preset: true), help: help) }69 public static func column(_ id: String, _ label: String, of range: String = "data", numeric: Bool = true, optional: Bool = false,70 default index: Int? = nil, help: String? = nil, visible: @escaping @Sendable (FormValues) -> Bool = { _ in true }) -> FormField {71 FormField(id, label, .column(range: range, numericOnly: numeric, optional: optional, defaultIndex: index), help: help, visible: visible)72 }73 public static func columns(_ id: String, _ label: String, of range: String = "data", numeric: Bool = true, all: Bool = true,74 help: String? = nil, visible: @escaping @Sendable (FormValues) -> Bool = { _ in true }) -> FormField {75 FormField(id, label, .columns(range: range, numericOnly: numeric, defaultAll: all), help: help, visible: visible)76 }77 public static func choice(_ id: String, _ label: String, _ options: [FieldOption], default d: String? = nil, help: String? = nil,78 visible: @escaping @Sendable (FormValues) -> Bool = { _ in true }) -> FormField {79 FormField(id, label, .choice(options, default: d ?? options.first?.id ?? ""), help: help, visible: visible)80 }81 public static func number(_ id: String, _ label: String, _ d: Double, min: Double? = nil, max: Double? = nil, unit: String? = nil,82 help: String? = nil, visible: @escaping @Sendable (FormValues) -> Bool = { _ in true }) -> FormField {83 FormField(id, label, .number(default: d, min: min, max: max, style: .plain, unit: unit), help: help, visible: visible)84 }85 public static func percent(_ id: String, _ label: String, _ d: Double, min: Double? = nil, max: Double? = nil, help: String? = nil,86 visible: @escaping @Sendable (FormValues) -> Bool = { _ in true }) -> FormField {87 FormField(id, label, .number(default: d, min: min, max: max, style: .percent, unit: "%"), help: help, visible: visible)88 }89 public static func integer(_ id: String, _ label: String, _ d: Int, min: Int = 0, max: Int = 1_000_000, help: String? = nil,90 visible: @escaping @Sendable (FormValues) -> Bool = { _ in true }) -> FormField {91 FormField(id, label, .integer(default: d, min: min, max: max), help: help, visible: visible)92 }93 public static func toggle(_ id: String, _ label: String, _ d: Bool, help: String? = nil,94 visible: @escaping @Sendable (FormValues) -> Bool = { _ in true }) -> FormField {95 FormField(id, label, .toggle(default: d), help: help, visible: visible)96 }97 public static func text(_ id: String, _ label: String, _ d: String = "", placeholder: String = "", help: String? = nil,98 visible: @escaping @Sendable (FormValues) -> Bool = { _ in true }) -> FormField {99 FormField(id, label, .text(default: d, placeholder: placeholder), help: help, visible: visible)100 }101 public static func cell(_ id: String, _ label: String, _ d: String = "", help: String? = nil,102 visible: @escaping @Sendable (FormValues) -> Bool = { _ in true }) -> FormField {103 FormField(id, label, .cell(default: d), help: help, visible: visible)104 }105 public static func cells(_ id: String, _ label: String, _ d: String = "", help: String? = nil,106 visible: @escaping @Sendable (FormValues) -> Bool = { _ in true }) -> FormField {107 FormField(id, label, .cells(default: d), help: help, visible: visible)108 }109 public static func date(_ id: String, _ label: String, _ d: Date, help: String? = nil) -> FormField { FormField(id, label, .date(default: d), help: help) }110 public static func section(_ id: String, _ label: String, visible: @escaping @Sendable (FormValues) -> Bool = { _ in true }) -> FormField {111 FormField(id, label, .section, visible: visible)112 }113 /// The significance level α used by the interpretation sentence.114 public static func alpha(_ label: String) -> FormField { .number("alpha", label, 0.05, min: 0.0001, max: 0.5) }115}116117/// The values of a form, keyed by field id.118public struct FormValues: Sendable, Hashable {119 public var storage: [String: FieldValue] = [:]120 public init() {}121122 /// Default values of every field.123 public init(defaults fields: [FormField]) {124 for f in fields {125 switch f.kind {126 case .range: storage[f.id] = .range(RangeValue())127 case .column(_, _, _, let d): storage[f.id] = .column(d)128 case .columns: storage[f.id] = .columns([])129 case .choice(_, let d): storage[f.id] = .text(d)130 case .number(let d, _, _, _, _): storage[f.id] = .number(d)131 case .integer(let d, _, _): storage[f.id] = .number(Double(d))132 case .toggle(let d): storage[f.id] = .bool(d)133 case .text(let d, _), .cell(let d), .cells(let d): storage[f.id] = .text(d)134 case .date(let d): storage[f.id] = .date(d)135 case .section: break136 }137 }138 }139140 public subscript(_ id: String) -> FieldValue? {141 get { storage[id] }142 set { storage[id] = newValue }143 }144145 public func number(_ id: String) -> Double { if case .number(let d)? = storage[id] { d } else { .nan } }146 public func int(_ id: String) -> Int { let d = number(id); return d.isFinite ? Int(d.rounded()) : 0 }147 public func bool(_ id: String) -> Bool { if case .bool(let b)? = storage[id] { b } else { false } }148 public func text(_ id: String) -> String { if case .text(let s)? = storage[id] { s } else { "" } }149 /// Identifier of the selected option of a choice field.150 public func choice(_ id: String) -> String { text(id) }151 public func range(_ id: String) -> RangeValue { if case .range(let r)? = storage[id] { r } else { RangeValue() } }152 public func columnIndex(_ id: String) -> Int? { if case .column(let i)? = storage[id] { i } else { nil } }153 public func columnIndices(_ id: String) -> [Int] { if case .columns(let i)? = storage[id] { i } else { [] } }154 public func date(_ id: String) -> Date { if case .date(let d)? = storage[id] { d } else { Date() } }155156 public mutating func set(_ id: String, number: Double) { storage[id] = .number(number) }157 public mutating func set(_ id: String, int: Int) { storage[id] = .number(Double(int)) }158 public mutating func set(_ id: String, bool: Bool) { storage[id] = .bool(bool) }159 public mutating func set(_ id: String, text: String) { storage[id] = .text(text) }160 public mutating func set(_ id: String, choice: String) { storage[id] = .text(choice) }161 public mutating func set(_ id: String, range: String, hasHeader: Bool = true) { storage[id] = .range(RangeValue(text: range, hasHeader: hasHeader)) }162 public mutating func set(_ id: String, column: Int?) { storage[id] = .column(column) }163 public mutating func set(_ id: String, columns: [Int]) { storage[id] = .columns(columns) }164 public mutating func set(_ id: String, date: Date) { storage[id] = .date(date) }165166 /// Parses a typed list of numbers ("0.05; 0.06 0.07", decimal comma accepted when unambiguous).167 public static func numberList(_ text: String, locale: Locale = .current) -> [Double]? {168 let decimalComma = (locale.decimalSeparator ?? ".") == ","169 let separators: Set<Character> = decimalComma ? [";", " ", "\n", "\t"] : [";", ",", " ", "\n", "\t"]170 var out = [Double]()171 for raw in text.split(whereSeparator: { separators.contains($0) }) {172 var token = String(raw)173 var scale = 1.0174 if token.hasSuffix("%") { token.removeLast(); scale = 0.01 }175 if decimalComma { token = token.replacingOccurrences(of: ",", with: ".") }176 guard let d = Double(token), d.isFinite else { return nil }177 out.append(d * scale)178 }179 return out.isEmpty ? nil : out180 }181}182