Import colors from Figma library #4

Merged
Sven merged 15 commits from figma into main 2022-10-08 11:14:23 +00:00
2 changed files with 41 additions and 1 deletions
Showing only changes of commit dc00d87a32 - Show all commits

View file

@ -2,4 +2,12 @@ protocol Importer {
init(source: String) throws
func read() throws -> [String: ColorDef]
static var option: String { get }
}
extension Importer {
static var option: String {
String(String(describing: self).droppingSuffix("Importer"))
}
}

View file

@ -19,6 +19,35 @@ private struct GeneratorOption: EnumerableFlag, CustomStringConvertible {
}
}
private struct ImporterOption: CaseIterable, ExpressibleByArgument, CustomStringConvertible {
static let allCases: [ImporterOption] = [
.list,
]
static let list = ImporterOption(type: ListImporter.self)
let type: Importer.Type
init(type: Importer.Type) {
self.type = type
}
init?(argument: String) {
guard let found = Self.allCases.first(where: { $0.description.caseInsensitiveCompare(argument) == .orderedSame }) else {
return nil
}
self = found
}
var description: String {
type.option
}
static func == (lhs: ImporterOption, rhs: ImporterOption) -> Bool {
lhs.type == rhs.type
}
}
enum Errors: Error {
case syntaxError
case duplicateColor(String)
@ -54,6 +83,9 @@ public final class MakeColors: ParsableCommand, Context {
@Flag(help: "The formatter to use.")
private var formatter = GeneratorOption.allCases[0]
@Option(help: "The importer to use.")
private var importer = ImporterOption.list
@Option(help: "Prefix for color names.")
var prefix: String?
@ -66,7 +98,7 @@ public final class MakeColors: ParsableCommand, Context {
public init() {}
public func run() throws {
let importer = ListImporter(source: input)
let importer = try importer.type.init(source: input)
let data = try importer.read()
if dump {