|
1 | | -open class CSVDecoderSequence: IteratorProtocol, Sequence { |
2 | | - private let source: ShadowDecoder.Source |
3 | | - private var currentIndex: Int = 0 |
| 1 | +extension CSVDecoder { |
| 2 | + public struct LazySequence: IteratorProtocol, Sequence { |
| 3 | + private let source: ShadowDecoder.Source |
| 4 | + private var currentIndex: Int = 0 |
4 | 5 |
|
5 | | - init(source: ShadowDecoder.Source) { |
6 | | - self.source = source |
7 | | - } |
| 6 | + init(source: ShadowDecoder.Source) { |
| 7 | + self.source = source |
| 8 | + } |
| 9 | + |
| 10 | + /// Advances to the next row and returns a `CSVRowDecoder`, or `nil` if no next row exists. |
| 11 | + public mutating func next() -> Row? { |
| 12 | + guard !self.source.isRowAtEnd(index: self.currentIndex) else { |
| 13 | + return nil |
| 14 | + } |
8 | 15 |
|
9 | | - /// Advances to the next row and returns a `CSVRowDecoder`, or `nil` if no next row exists. |
10 | | - public func next() -> CSVRowDecoder? { |
11 | | - guard !self.source.isRowAtEnd(index: self.currentIndex) else { |
12 | | - return nil |
| 16 | + defer { self.currentIndex += 1 } |
| 17 | + let decoder = ShadowDecoder(source: source, codingPath: [IndexKey(self.currentIndex)]) |
| 18 | + return Row(decoder: decoder) |
13 | 19 | } |
14 | 20 |
|
15 | | - defer { self.currentIndex += 1 } |
16 | | - let decoder = ShadowDecoder(source: source, codingPath: [IndexKey(self.currentIndex)]) |
17 | | - return CSVRowDecoder(decoder: decoder) |
18 | | - } |
19 | | -} |
| 21 | + public struct Row { |
| 22 | + /// The representation of the decoding process point-in-time. |
| 23 | + private let decoder: ShadowDecoder |
20 | 24 |
|
21 | | -public struct CSVRowDecoder { |
22 | | - /// The representation of the decoding process point-in-time. |
23 | | - let decoder: ShadowDecoder |
| 25 | + fileprivate init(decoder: ShadowDecoder) { |
| 26 | + self.decoder = decoder |
| 27 | + } |
24 | 28 |
|
25 | | - /// Returns a value of the type you specify, decoded from CSV row. |
26 | | - /// - parameter type: The type of the value to decode from the supplied file. |
27 | | - /// - throws: `DecodingError`, or `CSVError<CSVReader>`, or the error raised by your custom types. |
28 | | - public func decode<T:Decodable>(_ type: T.Type) throws -> T { |
29 | | - return try T(from: decoder) |
| 29 | + /// Returns a value of the type you specify, decoded from CSV row. |
| 30 | + /// - parameter type: The type of the value to decode from the supplied file. |
| 31 | + /// - throws: `DecodingError`, or `CSVError<CSVReader>`, or the error raised by your custom types. |
| 32 | + public func decode<T:Decodable>(_ type: T.Type) throws -> T { |
| 33 | + return try T(from: decoder) |
| 34 | + } |
| 35 | + } |
30 | 36 | } |
31 | 37 | } |
0 commit comments