From e28bee804f091399c94b0e15320e325924e3f995 Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Sat, 8 Oct 2022 11:03:28 +0200 Subject: [PATCH] Make importer protocol. --- .../LibMakeColors/Importers/Importer.swift | 5 +++ .../Importers/List/ListImporter.swift | 36 +++++++++++++++++++ .../List}/Scanner+ColorParser.swift | 4 +-- Sources/LibMakeColors/MakeColors.swift | 26 ++------------ 4 files changed, 45 insertions(+), 26 deletions(-) create mode 100644 Sources/LibMakeColors/Importers/Importer.swift create mode 100644 Sources/LibMakeColors/Importers/List/ListImporter.swift rename Sources/LibMakeColors/{Model => Importers/List}/Scanner+ColorParser.swift (98%) diff --git a/Sources/LibMakeColors/Importers/Importer.swift b/Sources/LibMakeColors/Importers/Importer.swift new file mode 100644 index 0000000..7528edb --- /dev/null +++ b/Sources/LibMakeColors/Importers/Importer.swift @@ -0,0 +1,5 @@ +protocol Importer { + init(source: String) throws + + func read() throws -> [String: ColorDef] +} diff --git a/Sources/LibMakeColors/Importers/List/ListImporter.swift b/Sources/LibMakeColors/Importers/List/ListImporter.swift new file mode 100644 index 0000000..d0e3d50 --- /dev/null +++ b/Sources/LibMakeColors/Importers/List/ListImporter.swift @@ -0,0 +1,36 @@ +import Foundation + +struct ListImporter: Importer { + let input: String + + init(source: String) { + input = source + } + + func read() throws -> [String: ColorDef] { + let scanner = Scanner(string: try readInput()) + scanner.charactersToBeSkipped = .whitespaces + + return try scanner.colorList() + } + + func readInput() throws -> String { + if input == "-" { + return try readStdin() + } + + let url = URL(fileURLWithPath: input) + return try String(contentsOf: url) + } + + func readStdin() throws -> String { + guard + let data = try FileHandle.standardInput.readToEnd(), + let input = String(data: data, encoding: .utf8) + else { + throw Errors.cannotReadStdin + } + + return input + } +} diff --git a/Sources/LibMakeColors/Model/Scanner+ColorParser.swift b/Sources/LibMakeColors/Importers/List/Scanner+ColorParser.swift similarity index 98% rename from Sources/LibMakeColors/Model/Scanner+ColorParser.swift rename to Sources/LibMakeColors/Importers/List/Scanner+ColorParser.swift index 9870824..b3d43d7 100644 --- a/Sources/LibMakeColors/Model/Scanner+ColorParser.swift +++ b/Sources/LibMakeColors/Importers/List/Scanner+ColorParser.swift @@ -110,7 +110,7 @@ extension Scanner { func colorLine() -> (String, ColorDef)? { guard - let name = self.name(), + let name = name(), let def = colorDef(), endOfLine() else { @@ -168,7 +168,7 @@ extension Scanner { func commaSeparated() -> [UInt8]? { var result: [UInt8] = [] repeat { - guard let component = self.component() else { + guard let component = component() else { return nil } result.append(component) diff --git a/Sources/LibMakeColors/MakeColors.swift b/Sources/LibMakeColors/MakeColors.swift index a2db649..810a7c8 100644 --- a/Sources/LibMakeColors/MakeColors.swift +++ b/Sources/LibMakeColors/MakeColors.swift @@ -66,10 +66,8 @@ public final class MakeColors: ParsableCommand, Context { public init() {} public func run() throws { - let scanner = Scanner(string: try readInput()) - scanner.charactersToBeSkipped = .whitespaces - - let data = try scanner.colorList() + let importer = ListImporter(source: input) + let data = try importer.read() if dump { try dump(data: data) @@ -81,26 +79,6 @@ public final class MakeColors: ParsableCommand, Context { try writeOutput(fileWrapper) } - func readInput() throws -> String { - if input == "-" { - return try readStdin() - } - - let url = URL(fileURLWithPath: input) - return try String(contentsOf: url) - } - - func readStdin() throws -> String { - guard - let data = try FileHandle.standardInput.readToEnd(), - let input = String(data: data, encoding: .utf8) - else { - throw Errors.cannotReadStdin - } - - return input - } - func dump(data: [String: ColorDef]) throws { for (key, color) in data.sorted() { let resolved = try data.resolve(key)