Created
August 17, 2020 14:10
-
-
Save Sajjon/1962c9d104d2b1faf85476fbad8cf32e to your computer and use it in GitHub Desktop.
Cachable ScanJob
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct ScanJob { | |
let runContext: RunContext | |
} | |
// MARK: Job | |
extension ScanJob: CacheableJob { | |
typealias Input = RunContext | |
typealias Output = ScannedLines | |
func validateCached(_ cached: Output) throws { | |
guard cached.numberOfLines >= runContext.numberOfLinesToScan else { | |
throw Error.notEnoughLinesScanned | |
} | |
} | |
func newWork(input _: Input) throws -> Output { | |
let inputFileName = runContext.fileNameOfInputCorpus | |
let file = try Cacher.currentDirectoryPath.openFile(named: inputFileName) | |
let lineReader = try LineScanner(file: file) | |
print("✅ Starting to scan lines") | |
let numberOfLinesToScan = runContext.numberOfLinesToScan | |
var scannedLines = [ScannedLine]() | |
readlines: for lineIndex in 0...numberOfLinesToScan { | |
guard let rawLine: String = lineReader.nextLine() else { | |
if lineIndex < numberOfLinesToScan { | |
throw Error.notEnoughLinesScanned | |
} | |
break readlines | |
} | |
let scannedLine = ScannedLine(lineFromCorpus: rawLine, positionInCorpus: lineIndex) | |
scannedLines.append(scannedLine) | |
} | |
return ScannedLines(scannedLines) | |
} | |
} | |
extension ScanJob { | |
enum Error: Swift.Error, Equatable { | |
case notEnoughLinesScanned | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment