Skip to content

Commit

Permalink
Merge pull request #10 from fummicc1/add-runtime-warning
Browse files Browse the repository at this point in the history
Use RuntimeWarning instead of precondition
  • Loading branch information
fummicc1 authored Dec 17, 2024
2 parents 7241b04 + 3393052 commit bdd4ee8
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 141 deletions.
108 changes: 0 additions & 108 deletions .swiftpm/xcode/xcshareddata/xcschemes/GeoHash.xcscheme

This file was deleted.

4 changes: 4 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import PackageDescription

let package = Package(
name: "GeoHash",
platforms: [
.macOS(.v10_15),
.iOS(.v15),
],
products: [
// Products define the executables and libraries a package produces, making them visible to other packages.
.library(
Expand Down
6 changes: 3 additions & 3 deletions Sources/GeoHashCLI/GeoHashCLI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ struct GeoHashCLI: ParsableCommand {
)
let precision = GeoHashBitsPrecision.exact(digits: length * 5)

let geoHash: GeoHash
let geoHash: GeoHash?

if let coordinate {
let components = coordinate.split(separator: ",")
Expand All @@ -53,6 +53,6 @@ struct GeoHashCLI: ParsableCommand {
} else {
throw ValidationError("Coordinate or latitude and longitude must be provided.")
}
print(geoHash.geoHash)
print(geoHash?.geoHash ?? "Invalid GeoHash")
}
}
}
75 changes: 50 additions & 25 deletions Sources/GeoHashFramework/GeoHash.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import Foundation

internal let runtimeWarning = RuntimeWarning()

public struct GeoHash: Sendable, Hashable {
public private(set) var precision: GeoHashBitsPrecision
public private(set) var coordinate: GeoHashCoordinate2D
Expand All @@ -18,20 +20,31 @@ public struct GeoHash: Sendable, Hashable {
/// Assure that all characters in the string are "0" or "1".
///
/// - Parameter value: A string that contains only "0" or "1".
public init(
public init?(
binary: String,
precision: GeoHashBitsPrecision = .mid
) {
precondition(
binary.allSatisfy({
["0", "1"].contains($0)
})
)
let isValidBinary = binary.allSatisfy({
["0", "1"].contains($0)
})
if !isValidBinary {
Task {
await runtimeWarning.log(
message: "Binary string must contain only '0' or '1'."
)
}
return nil
}
if binary.count != precision.rawValue {
precondition(
binary.count == precision.rawValue,
"Binary length must be \(precision.rawValue), but binary length is \(binary.count)"
)
let isSameLength = binary.count == precision.rawValue
if !isSameLength {
Task {
await runtimeWarning.log(
message: "Binary length must be %d, but binary length is %d",
args: precision.rawValue, binary.count
)
}
}
}
self.binary = binary
self.precision = precision
Expand All @@ -41,19 +54,27 @@ public struct GeoHash: Sendable, Hashable {
)
}

public init(
public init?(
latitude: Double,
longitude: Double,
precision: GeoHashBitsPrecision = .mid
) {
precondition(
latitude >= -90 && latitude <= 90,
"Latitude must be between -90 and 90"
)
precondition(
longitude >= -180 && longitude <= 180,
"Longitude must be between -180 and 180"
)
if latitude >= 90 || latitude <= -90 {
Task {
await runtimeWarning.log(
message: "Latitude must be between -90 and 90"
)
}
return nil
}
if longitude >= 180 || longitude <= -180 {
Task {
await runtimeWarning.log(
message: "Longitude must be between -180 and 180"
)
}
return nil
}

self.init(
binary: Self.makeBinary(
Expand All @@ -67,7 +88,7 @@ public struct GeoHash: Sendable, Hashable {
)
}

public init(geoHash: String, precision: GeoHashBitsPrecision = .mid) {
public init?(geoHash: String, precision: GeoHashBitsPrecision = .mid) {
self.init(
binary: Self.makeBinary(
from: geoHash,
Expand All @@ -88,9 +109,13 @@ extension GeoHash {

for char in geoHash {
guard let index = Self.base32Chars.firstIndex(of: char) else {
preconditionFailure(
"Invalid geohash character \(char) in geoHash: \(geoHash)"
)
Task {
await runtimeWarning.log(
message: "Invalid geohash character %s in geoHash: %s",
args: String(char), geoHash
)
}
continue
}

let value = Self.base32Chars.distance(
Expand Down Expand Up @@ -237,7 +262,7 @@ extension GeoHash {
latitude: newLat,
longitude: newLng,
precision: precision
)
)!
}

return [
Expand Down Expand Up @@ -363,7 +388,7 @@ extension GeoHash {
let bound = GeoHash(
binary: String(repeating: "0", count: precision.rawValue),
precision: precision
).getBound()
)!.getBound()

let latitudeDelta = bound[0].latitude - bound[3].latitude
let longitudeDelta = bound[1].longitude - bound[0].longitude
Expand Down
45 changes: 45 additions & 0 deletions Sources/GeoHashFramework/RuntimeWarning.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// RuntimeWarning.swift
// GeoHash
//
// Created by Fumiya Tanaka on 2024/12/17.
//

import Foundation
import OSLog

actor RuntimeWarning {
var info = Dl_info()

init() {
#if DEBUG
dladdr(
dlsym(
dlopen(
nil,
RTLD_LAZY
),
"""
$sxSg7SwiftUI8CommandsA2bCRzlAbCP4body4BodyQzvgTW
"""
),
&info
)
#endif
}

func log(message: StaticString, args: any CVarArg...) {
#if DEBUG
os_log(
.fault,
dso: info.dli_fbase,
log: OSLog(
subsystem: "com.apple.runtime-issues",
category: "ReportRuntimeWarningSample"
),
message,
args
)
#endif
}
}
10 changes: 5 additions & 5 deletions Tests/GeoHashFrameworkTests/GeoHashTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct GeoHashTests {
let geoHash = GeoHash(
binary: input,
precision: .exact(digits: 20)
)
)!
#expect(geoHash.geoHash == "e9p6")
#expect(geoHash.geoHash.count == input.count / 5)
#expect(geoHash.latitudeBits == expectedLat)
Expand All @@ -31,7 +31,7 @@ struct GeoHashTests {
let lat = 35.681382
let lng = 139.766084

let geoHash = GeoHash(latitude: lat, longitude: lng)
let geoHash = GeoHash(latitude: lat, longitude: lng)!
#expect(
geoHash.binary == "1110110100001110011011010101111110001101"
)
Expand All @@ -48,7 +48,7 @@ struct GeoHashTests {
precision: .exact(
digits: geoHashString.count * 5
)
)
)!
#expect(geoHash.geoHash == geoHashString)
}

Expand All @@ -64,7 +64,7 @@ struct GeoHashTests {
latitude: coordinate.latitude,
longitude: coordinate.longitude,
precision: precision
)
)!
let actual = geoHash.getBound()
let expected = [
GeoHashCoordinate2D(
Expand Down Expand Up @@ -110,7 +110,7 @@ struct GeoHashTests {
"xn76urwk",
]

let geoHash = GeoHash(latitude: lat, longitude: lng)
let geoHash = GeoHash(latitude: lat, longitude: lng)!
let neighbors = geoHash.getNeighbors()
#expect(neighbors.map(\.geoHash) == expected)
}
Expand Down

0 comments on commit bdd4ee8

Please sign in to comment.