|
| 1 | +import Foundation |
| 2 | + |
| 3 | +extension CSVReader: Failable { |
| 4 | + /// Reader status indicating whether there are remaning lines to read, the CSV has been completely parsed, or an error occurred and no further operation shall be performed. |
| 5 | + public enum Status { |
| 6 | + /// The CSV file hasn't been completely parsed. |
| 7 | + case active |
| 8 | + /// There are no more rows to read. The EOF has been reached. |
| 9 | + case finished |
| 10 | + /// An error has occurred and no further operations shall be performed with the reader instance. |
| 11 | + case failed(CSVError<CSVReader>) |
| 12 | + } |
| 13 | + |
| 14 | + /// The type of error raised by the CSV reader. |
| 15 | + public enum Error: Int { |
| 16 | + /// Some of the configuration values provided are invalid. |
| 17 | + case invalidConfiguration = 1 |
| 18 | + /// The CSV data is invalid. |
| 19 | + case invalidInput = 2 |
| 20 | +// /// The inferral process to figure out delimiters or header row status was unsuccessful. |
| 21 | +// case inferenceFailure = 3 |
| 22 | + /// The input stream failed. |
| 23 | + case streamFailure = 4 |
| 24 | + } |
| 25 | + |
| 26 | + public static var errorDomain: String { |
| 27 | + "Reader" |
| 28 | + } |
| 29 | + |
| 30 | + public static func errorDescription(for failure: Error) -> String { |
| 31 | + switch failure { |
| 32 | + case .invalidConfiguration: return "Invalid configuration" |
| 33 | + // case .inferenceFailure: return "Inference failure" |
| 34 | + case .invalidInput: return "Invalid input" |
| 35 | + case .streamFailure: return "Stream failure" |
| 36 | + } |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +extension CSVReader { |
| 41 | + /// Private configuration variables for the CSV reader. |
| 42 | + internal struct Settings { |
| 43 | + /// The unicode scalar delimiters for fields and rows. |
| 44 | + let delimiters: Delimiter.RawPair |
| 45 | + /// The unicode scalar used as encapsulator and escaping character (when printed two times). |
| 46 | + let escapingScalar: Unicode.Scalar? |
| 47 | + /// The characters set to be trimmed at the beginning and ending of each field. |
| 48 | + let trimCharacters: CharacterSet |
| 49 | + |
| 50 | + /// Creates the inmutable reader settings from the user provided configuration values. |
| 51 | + /// - parameter configuration: The configuration values provided by the API user. |
| 52 | + /// - parameter decoder: The instance providing the input `Unicode.Scalar`s. |
| 53 | + /// - parameter buffer: Small buffer use to store `Unicode.Scalar` values that have been read from the input, but haven't yet been processed. |
| 54 | + /// - throws: `CSVError<CSVReader>` exclusively. |
| 55 | + init(configuration: Configuration, decoder: ScalarDecoder, buffer: ScalarBuffer) throws { |
| 56 | + // 1. Figure out the field and row delimiters. |
| 57 | + switch (configuration.delimiters.field.rawValue, configuration.delimiters.row.rawValue) { |
| 58 | + case (nil, nil): |
| 59 | + self.delimiters = try CSVReader.inferDelimiters(decoder: decoder, buffer: buffer) |
| 60 | + case (nil, let row): |
| 61 | + self.delimiters = try CSVReader.inferFieldDelimiter(rowDelimiter: row, decoder: decoder, buffer: buffer) |
| 62 | + case (let field, nil): |
| 63 | + self.delimiters = try CSVReader.inferRowDelimiter(fieldDelimiter: field, decoder: decoder, buffer: buffer) |
| 64 | + case (let field, let row) where !field.elementsEqual(row): |
| 65 | + self.delimiters = (.init(field), .init(row)) |
| 66 | + case (let delimiter, _): |
| 67 | + throw Error.invalidDelimiters(delimiter) |
| 68 | + } |
| 69 | + // 2. Set the escaping scalar. |
| 70 | + self.escapingScalar = configuration.escapingStrategy.scalar |
| 71 | + // 3. Set the trim characters set. |
| 72 | + self.trimCharacters = configuration.trimStrategry |
| 73 | + // 4. Ensure trim character set doesn't contain the field delimiter. |
| 74 | + guard delimiters.field.allSatisfy({ !self.trimCharacters.contains($0) }) else { |
| 75 | + throw Error.invalidTrimCharacters(self.trimCharacters, delimiter: configuration.delimiters.field.rawValue) |
| 76 | + } |
| 77 | + // 5. Ensure trim character set doesn't contain the row delimiter. |
| 78 | + guard delimiters.row.allSatisfy({ !self.trimCharacters.contains($0) }) else { |
| 79 | + throw Error.invalidTrimCharacters(self.trimCharacters, delimiter: configuration.delimiters.row.rawValue) |
| 80 | + } |
| 81 | + // 6. Ensure trim character set does not include escaping scalar |
| 82 | + if let escapingScalar = self.escapingScalar, self.trimCharacters.contains(escapingScalar) { |
| 83 | + throw Error.invalidTrimCharacters(self.trimCharacters, escapingScalar: escapingScalar) |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +fileprivate extension CSVReader.Error { |
| 90 | + /// Error raised when the field and row delimiters are the same. |
| 91 | + /// - parameter delimiter: The indicated field and row delimiters. |
| 92 | + static func invalidDelimiters(_ delimiter: String.UnicodeScalarView) -> CSVError<CSVReader> { |
| 93 | + .init(.invalidConfiguration, |
| 94 | + reason: "The field and row delimiters cannot be the same.", |
| 95 | + help: "Set different delimiters for field and rows.", |
| 96 | + userInfo: ["Delimiter": delimiter]) |
| 97 | + } |
| 98 | + /// Error raised when a delimiter (whether row or field) is included in the trim character set. |
| 99 | + static func invalidTrimCharacters(_ trimCharacters: CharacterSet, delimiter: String.UnicodeScalarView) -> CSVError<CSVReader> { |
| 100 | + .init(.invalidConfiguration, |
| 101 | + reason: "The trim character set includes delimiter characters.", |
| 102 | + help: "Remove the delimiter scalars from the trim character set.", |
| 103 | + userInfo: ["Delimiter": delimiter, "Trim characters": trimCharacters]) |
| 104 | + } |
| 105 | + /// Error raised when the escaping scalar has been included in the trim character set. |
| 106 | + /// - parameter escapingScalar: The selected escaping scalar. |
| 107 | + /// - parameter trimCharacters: The character set selected for trimming. |
| 108 | + static func invalidTrimCharacters(_ trimCharacters: CharacterSet, escapingScalar: Unicode.Scalar) -> CSVError<CSVReader> { |
| 109 | + .init(.invalidConfiguration, |
| 110 | + reason: "The trim characters set includes the escaping scalar.", |
| 111 | + help: "Remove the escaping scalar from the trim characters set.", |
| 112 | + userInfo: ["Escaping scalar": escapingScalar, "Trim characters": trimCharacters]) |
| 113 | + } |
| 114 | +} |
0 commit comments