diff --git a/Source/CarthageKit/BinaryJSONError.swift b/Source/CarthageKit/BinaryJSONError.swift index bddd6a2700..9aca3b6c15 100644 --- a/Source/CarthageKit/BinaryJSONError.swift +++ b/Source/CarthageKit/BinaryJSONError.swift @@ -3,7 +3,7 @@ import Foundation /// Error parsing a binary-only framework JSON file, used in CarthageError.invalidBinaryJSON. public enum BinaryJSONError: Error { /// Unable to parse the JSON. - case invalidJSON(NSError) + case invalidJSON(Error) /// Unable to parse a semantic version from a framework entry. case invalidVersion(ScannableError) @@ -37,7 +37,7 @@ extension BinaryJSONError: Equatable { public static func == (lhs: BinaryJSONError, rhs: BinaryJSONError) -> Bool { switch (lhs, rhs) { case let (.invalidJSON(left), .invalidJSON(right)): - return left == right + return (left as NSError) == (right as NSError) case let (.invalidVersion(left), .invalidVersion(right)): return left == right diff --git a/Source/CarthageKit/BinaryProject.swift b/Source/CarthageKit/BinaryProject.swift index ca52e95cc4..22afcaa5f4 100644 --- a/Source/CarthageKit/BinaryProject.swift +++ b/Source/CarthageKit/BinaryProject.swift @@ -2,20 +2,14 @@ import Foundation import Result public struct BinaryProject { + private static let jsonDecoder = JSONDecoder() + public var versions: [PinnedVersion: URL] public static func from(jsonData: Data) -> Result { - return Result(attempt: { try JSONSerialization.jsonObject(with: jsonData, options: []) }) - .mapError(BinaryJSONError.invalidJSON) - .flatMap { json in - let error = NSError( - domain: Constants.bundleIdentifier, - code: 1, - userInfo: [NSLocalizedDescriptionKey: "Binary definition was not expected type [String: String]"] - ) - return Result(json as? [String: String], failWith: BinaryJSONError.invalidJSON(error)) - } - .flatMap { (json: [String: String]) -> Result in + return Result<[String: String], AnyError>(attempt: { try jsonDecoder.decode([String: String].self, from: jsonData) }) + .mapError { .invalidJSON($0.error) } + .flatMap { json -> Result in var versions = [PinnedVersion: URL]() for (key, value) in json { diff --git a/Tests/CarthageKitTests/BinaryProjectSpec.swift b/Tests/CarthageKitTests/BinaryProjectSpec.swift index 1875d025ea..3b6a17cdb0 100644 --- a/Tests/CarthageKitTests/BinaryProjectSpec.swift +++ b/Tests/CarthageKitTests/BinaryProjectSpec.swift @@ -44,12 +44,11 @@ class BinaryProjectSpec: QuickSpec { let actualError = BinaryProject.from(jsonData: jsonData).error - let error = NSError( - domain: Constants.bundleIdentifier, - code: 1, - userInfo: [NSLocalizedDescriptionKey: "Binary definition was not expected type [String: String]"] - ) - expect(actualError) == .invalidJSON(error) + if case let .invalidJSON(underlyingError)? = actualError { + expect(underlyingError is DecodingError) == true + } else { + fail() + } } it("should fail with an invalid semantic version") {