Skip to content

Commit

Permalink
Make AppleMapsKitError Equatable
Browse files Browse the repository at this point in the history
  • Loading branch information
fpseverino committed Dec 29, 2024
1 parent 498b7a3 commit 74acf1b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
27 changes: 22 additions & 5 deletions Sources/AppleMapsKit/AppleMapsKitError.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/// AppleMapsKit error type.
public struct AppleMapsKitError: Error, Sendable {
public struct ErrorType: Sendable, Hashable, CustomStringConvertible {
enum Base: String, Sendable {
public struct AppleMapsKitError: Error, Sendable, Equatable {
/// The type of the errors that can be thrown by AppleMapsKit.
public struct ErrorType: Sendable, Hashable, CustomStringConvertible, Equatable {
enum Base: String, Sendable, Equatable {
case noPlacesFound
case invalidSearchResultType
}
Expand All @@ -11,36 +12,52 @@ public struct AppleMapsKitError: Error, Sendable {
private init(_ base: Base) {
self.base = base
}


/// No places were found for the given query.
public static let noPlacesFound = Self(.noPlacesFound)
/// The search result type is invalid.
public static let invalidSearchResultType = Self(.invalidSearchResultType)

/// A textual representation of this error.
public var description: String {
self.base.rawValue
}
}

private struct Backing: Sendable {
private struct Backing: Sendable, Equatable {
fileprivate let errorType: ErrorType

init(errorType: ErrorType) {
self.errorType = errorType
}

static func == (lhs: AppleMapsKitError.Backing, rhs: AppleMapsKitError.Backing) -> Bool {
lhs.errorType == rhs.errorType
}
}

private var backing: Backing

/// The type of this error.
public var errorType: ErrorType { backing.errorType }

private init(errorType: ErrorType) {
self.backing = .init(errorType: errorType)
}

/// No places were found for the given query.
public static let noPlacesFound = Self(errorType: .noPlacesFound)

/// The search result type is invalid.
public static let invalidSearchResultType = Self(errorType: .invalidSearchResultType)

public static func == (lhs: AppleMapsKitError, rhs: AppleMapsKitError) -> Bool {
lhs.backing == rhs.backing
}
}

extension AppleMapsKitError: CustomStringConvertible {
/// A textual representation of this error.
public var description: String {
"AppleMapsKitError(errorType: \(self.errorType))"
}
Expand Down
5 changes: 1 addition & 4 deletions Tests/AppleMapsKitTests/AppleMapsKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,11 @@ struct AppleMapsKitTests {
}

@Test("Search with invalid Result Type") func searchWithInvalidResultType() async throws {
await #expect {
await #expect(throws: AppleMapsKitError.invalidSearchResultType) {
try await client.search(
for: "eiffel tower",
resultTypeFilter: [.pointOfInterest, .physicalFeature, .poi, .address, .query]
)
} throws: { error in
guard let error = error as? AppleMapsKitError else { return false }
return error.errorType.base == .invalidSearchResultType
}
}

Expand Down

0 comments on commit 74acf1b

Please sign in to comment.