Sources/OrgApp/TableReason.swift

74 lines · 5506 bytes

 1import Foundation
 2import OrgCore
 3
 4/// Why a table's formulas go to Emacs, `Calc.Unsupported`'s reason, in the app's language.
 5public enum TableReason {
 6    /// The reason translated, or as it is when it's one this doesn't know.
 7    public static func localized(_ reason: String) -> String { translated(reason) ?? reason }
 8
 9    /// Nil for a reason not known here.
10    static func translated(_ reason: String) -> String? {
11        if reason.hasPrefix("Lisp ") {
12            let inner = String(reason.dropFirst("Lisp ".count))
13            guard let what = lisp(inner) else { return nil }
14            return String(localized: "Lisp \(what)", comment: "Why a table needs Emacs: something its Lisp formula does")
15        }
16        if let fixed = fixed(reason) { return fixed }
17        func value(_ prefix: String) -> String? { reason.hasPrefix(prefix) ? String(reason.dropFirst(prefix.count)) : nil }
18        if let name = value("function ") { return String(localized: "function \(name)", comment: "Why a table needs Emacs: a Calc function") }
19        if let name = value("variable ") { return String(localized: "variable \(name)", comment: "Why a table needs Emacs: a Calc variable") }
20        if let text = value("non-numeric field ") { return String(localized: "non-numeric field \(text)", comment: "Why a table needs Emacs") }
21        if let number = value("number size ") { return String(localized: "number size \(number)", comment: "Why a table needs Emacs: a number too large") }
22        if let flag = value("format flag ") { return String(localized: "format flag \(flag)", comment: "Why a table needs Emacs: a format specifier flag") }
23        return nil
24    }
25
26    /// `Elisp.Unsupported`'s reasons.
27    static func lisp(_ what: String) -> String? {
28        if what == "integer size" { return fixed(what) }
29        func value(_ prefix: String) -> String? { what.hasPrefix(prefix) ? String(what.dropFirst(prefix.count)) : nil }
30        if let name = value("function ") { return String(localized: "function \(name)", comment: "Why a table needs Emacs: a Calc function") }
31        if let time = value("time value ") { return String(localized: "time value \(time)", comment: "Why a table needs Emacs: a Lisp time value") }
32        if let number = value("date number ") { return String(localized: "date number \(number)", comment: "Why a table needs Emacs: a Lisp date number") }
33        if what.hasSuffix(" on a place") {
34            let name = String(what.dropLast(" on a place".count))
35            return String(localized: "\(name) on a place", comment: "Why a table needs Emacs: a Lisp function used on a variable")
36        }
37        return nil
38    }
39
40    static func fixed(_ reason: String) -> String? {
41        let comment: StaticString = "Why a table needs Emacs"
42        switch reason {
43        case "arguments": return String(localized: "arguments", comment: comment)
44        case "calc precision": return String(localized: "calc precision", comment: comment)
45        case "chained comparison": return String(localized: "chained comparison", comment: comment)
46        case "comparison with nan": return String(localized: "comparison with nan", comment: comment)
47        case "complex result": return String(localized: "complex result", comment: comment)
48        case "condition": return String(localized: "condition", comment: comment)
49        case "constants that refer to themselves": return String(localized: "constants that refer to themselves", comment: comment)
50        case "date arithmetic": return String(localized: "date arithmetic", comment: comment)
51        case "date syntax": return String(localized: "date syntax", comment: comment)
52        case "division by zero": return String(localized: "division by zero", comment: comment)
53        case "duration size": return String(localized: "duration size", comment: comment)
54        case "empty vector": return String(localized: "empty vector", comment: comment)
55        case "formula range": return String(localized: "formula range", comment: comment)
56        case "formula syntax": return String(localized: "formula syntax", comment: comment)
57        case "infinite result": return String(localized: "infinite result", comment: comment)
58        case "integer size": return String(localized: "integer size", comment: comment)
59        case "logarithm domain": return String(localized: "logarithm domain", comment: comment)
60        case "number": return String(localized: "number", comment: comment)
61        case "number size": return String(localized: "number size", comment: comment)
62        case "range outside row": return String(localized: "range outside row", comment: comment)
63        case "reference outside table": return String(localized: "reference outside table", comment: comment)
64        case "remote table name from a range": return String(localized: "remote table name from a range", comment: comment)
65        case "row out of range": return String(localized: "row out of range", comment: comment)
66        case "table position": return String(localized: "table position", comment: comment)
67        case "variance of one value": return String(localized: "variance of one value", comment: comment)
68        case "vector argument": return String(localized: "vector argument", comment: comment)
69        case "vector arithmetic": return String(localized: "vector arithmetic", comment: comment)
70        case "vector result": return String(localized: "vector result", comment: comment)
71        default: return nil
72        }
73    }
74}