|
| 1 | +import Foundation |
| 2 | + |
| 3 | +extension CSVReader { |
| 4 | + /// Creates a reader instance that will be used to parse the given `String`. |
| 5 | + /// - parameter string: A `String` containing CSV formatted data. |
| 6 | + /// - parameter configuration: Closure receiving the default parsing configuration values and letting you change them. |
| 7 | + /// - throws: `CSVReader.Error` exclusively. |
| 8 | + @inlinable public convenience init(string: String, configuration: (inout Configuration)->Void) throws { |
| 9 | + var config = Configuration() |
| 10 | + configuration(&config) |
| 11 | + try self.init(string: string, configuration: config) |
| 12 | + } |
| 13 | + |
| 14 | + /// Creates a reader instance that will be used to parse the given data blob. |
| 15 | + /// - parameter data: A data blob containing CSV formatted data. |
| 16 | + /// - parameter configuration: Closure receiving the default parsing configuration values and letting you change them. |
| 17 | + /// - throws: `CSVReader.Error` exclusively. |
| 18 | + @inlinable public convenience init(data: Data, configuration: (inout Configuration)->Void) throws { |
| 19 | + var config = Configuration() |
| 20 | + configuration(&config) |
| 21 | + try self.init(data: data, configuration: config) |
| 22 | + } |
| 23 | + |
| 24 | + /// Creates a reader instance that will be used to parse the given CSV file. |
| 25 | + /// - parameter fileURL: The URL indicating the location of the file to be parsed. |
| 26 | + /// - parameter configuration: Closure receiving the default parsing configuration values and letting you change them. |
| 27 | + /// - throws: `CSVReader.Error` exclusively. |
| 28 | + @inlinable public convenience init(fileURL: URL, configuration: (inout Configuration)->Void) throws { |
| 29 | + var config = Configuration() |
| 30 | + configuration(&config) |
| 31 | + try self.init(fileURL: fileURL, configuration: config) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +extension CSVReader { |
| 36 | + /// Reads the Swift String and returns the CSV headers (if any) and all the records. |
| 37 | + /// - parameter string: A `String` value containing CSV formatted data. |
| 38 | + /// - parameter configuration: Recipe detailing how to parse the CSV data (i.e. delimiters, date strategy, etc.). |
| 39 | + /// - throws: `CSVReader.Error` exclusively. |
| 40 | + /// - returns: Tuple with the CSV headers (empty if none) and all records within the CSV file. |
| 41 | + public static func parse(string: String, configuration: Configuration = .init()) throws -> Output { |
| 42 | + let reader = try CSVReader(string: string, configuration: configuration) |
| 43 | + let lookup = try reader.makeHeaderLookup() |
| 44 | + |
| 45 | + var result: [[String]] = .init() |
| 46 | + while let row = try reader.parseRow() { |
| 47 | + result.append(row) |
| 48 | + } |
| 49 | + |
| 50 | + return .init(headers: reader.headers, rows: result, lookup: lookup) |
| 51 | + } |
| 52 | + |
| 53 | + /// Reads a blob of data using the encoding provided as argument and returns the CSV headers (if any) and all the CSV records. |
| 54 | + /// - parameter data: A blob of data containing CSV formatted data. |
| 55 | + /// - parameter configuration: Recipe detailing how to parse the CSV data (i.e. delimiters, date strategy, etc.). |
| 56 | + /// - throws: `CSVReader.Error` exclusively. |
| 57 | + /// - returns: Tuple with the CSV headers (empty if none) and all records within the CSV file. |
| 58 | + public static func parse(data: Data, configuration: Configuration = .init()) throws -> Output { |
| 59 | + let reader = try CSVReader(data: data, configuration: configuration) |
| 60 | + let lookup = try reader.makeHeaderLookup() |
| 61 | + |
| 62 | + var result: [[String]] = .init() |
| 63 | + while let row = try reader.parseRow() { |
| 64 | + result.append(row) |
| 65 | + } |
| 66 | + |
| 67 | + return .init(headers: reader.headers, rows: result, lookup: lookup) |
| 68 | + } |
| 69 | + |
| 70 | + /// Reads a CSV file using the provided encoding and returns the CSV headers (if any) and all the CSV records. |
| 71 | + /// - parameter fileURL: The URL indicating the location of the file to be parsed. |
| 72 | + /// - parameter configuration: Recipe detailing how to parse the CSV data (i.e. delimiters, date strategy, etc.). |
| 73 | + /// - throws: `CSVReader.Error` exclusively. |
| 74 | + /// - returns: Tuple with the CSV headers (empty if none) and all records within the CSV file. |
| 75 | + public static func parse(fileURL: URL, configuration: Configuration = .init()) throws -> Output { |
| 76 | + let reader = try CSVReader(fileURL: fileURL, configuration: configuration) |
| 77 | + let lookup = try reader.makeHeaderLookup() |
| 78 | + |
| 79 | + var result: [[String]] = .init() |
| 80 | + while let row = try reader.parseRow() { |
| 81 | + result.append(row) |
| 82 | + } |
| 83 | + |
| 84 | + return .init(headers: reader.headers, rows: result, lookup: lookup) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +extension CSVReader { |
| 89 | + /// Reads the Swift String and returns the CSV headers (if any) and all the records. |
| 90 | + /// - parameter string: A `String` value containing CSV formatted data. |
| 91 | + /// - parameter configuration: Closure receiving the default parsing configuration values and letting you change them. |
| 92 | + /// - throws: `CSVReader.Error` exclusively. |
| 93 | + /// - returns: Tuple with the CSV headers (empty if none) and all records within the CSV file. |
| 94 | + @inlinable public static func parse(string: String, configuration: (inout Configuration)->Void) throws -> Output { |
| 95 | + var config = Configuration() |
| 96 | + configuration(&config) |
| 97 | + return try CSVReader.parse(string: string, configuration: config) |
| 98 | + } |
| 99 | + |
| 100 | + /// Reads a blob of data using the encoding provided as argument and returns the CSV headers (if any) and all the CSV records. |
| 101 | + /// - parameter data: A blob of data containing CSV formatted data. |
| 102 | + /// - parameter configuration: Closure receiving the default parsing configuration values and letting you change them. |
| 103 | + /// - throws: `CSVReader.Error` exclusively. |
| 104 | + /// - returns: Tuple with the CSV headers (empty if none) and all records within the CSV file. |
| 105 | + @inlinable public static func parse(data: Data, configuration: (inout Configuration)->Void) throws -> Output { |
| 106 | + var config = Configuration() |
| 107 | + configuration(&config) |
| 108 | + return try CSVReader.parse(data: data, configuration: config) |
| 109 | + } |
| 110 | + |
| 111 | + /// Reads a CSV file using the provided encoding and returns the CSV headers (if any) and all the CSV records. |
| 112 | + /// - parameter fileURL: The URL indicating the location of the file to be parsed. |
| 113 | + /// - parameter configuration: Closure receiving the default parsing configuration values and letting you change them. |
| 114 | + /// - throws: `CSVReader.Error` exclusively. |
| 115 | + /// - returns: Tuple with the CSV headers (empty if none) and all records within the CSV file. |
| 116 | + @inlinable public static func parse(fileURL: URL, configuration: (inout Configuration)->Void) throws -> Output { |
| 117 | + var config = Configuration() |
| 118 | + configuration(&config) |
| 119 | + return try CSVReader.parse(fileURL: fileURL, configuration: config) |
| 120 | + } |
| 121 | +} |
0 commit comments