Skip to content

Commit

Permalink
Replace UDL definitions with proc macros!
Browse files Browse the repository at this point in the history
  • Loading branch information
ianthetechie committed Oct 29, 2023
1 parent 581d7b0 commit 5f2d60d
Show file tree
Hide file tree
Showing 15 changed files with 125 additions and 319 deletions.
6 changes: 3 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ if useLocalFramework {
name: "FerrostarCoreRS",
// IMPORTANT: Swift packages importing this locally will not be able to
// import Ferrostar core unless you specify this as a relative path!
path: "./common/target/ios/ferrostar-rs.xcframework"
path: "./common/target/ios/libferrostar_core-rs.xcframework"
)
} else {
let releaseTag = "0.0.9"
let releaseChecksum = "96c50c1e27733e9d88cecb337aa2ee86797d2ba07461f20b76f6577a4c3c66b5"
let releaseChecksum = "318369b7304a28f45a0223759dda6fedfd9103690b3c5d5eb0144fa1adf3ac98"
binaryTarget = .binaryTarget(
name: "FerrostarCoreRS",
url: "https://github.com/stadiamaps/ferrostar/releases/download/\(releaseTag)/ferrostar-rs.xcframework.zip",
url: "https://github.com/stadiamaps/ferrostar/releases/download/\(releaseTag)/libferrostar_core-rs.xcframework.zip",
checksum: releaseChecksum
)
}
Expand Down
2 changes: 1 addition & 1 deletion apple/Sources/FerrostarCore/CoreLocation Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import UniFFI

extension CLLocationCoordinate2D {
var geographicCoordinates: UniFFI.GeographicCoordinates {
UniFFI.GeographicCoordinates(lat: latitude, lng: longitude)
UniFFI.GeographicCoordinates(lng: longitude, lat: latitude)
}

init(geographicCoordinates: GeographicCoordinates) {
Expand Down
3 changes: 1 addition & 2 deletions apple/Sources/FerrostarCore/FerrostarCore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ public protocol FerrostarCoreDelegate: AnyObject {
if let res = response as? HTTPURLResponse, res.statusCode < 200 || res.statusCode >= 300 {
throw FerrostarCoreError.httpStatusCode(res.statusCode)
} else {
let uint8Data = [UInt8](data)
let routes = try routeAdapter.parseResponse(response: uint8Data)
let routes = try routeAdapter.parseResponse(response: data)

return routes.map { Route(inner: $0) }
}
Expand Down
2 changes: 1 addition & 1 deletion apple/Sources/FerrostarMapLibreUI/BannerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ struct BannerView: View {
}

#Preview {
let location = GeographicCoordinates(lat: 0, lng: 0)
let location = GeographicCoordinates(lng: 0, lat: 0)
let instructions = UniFFI.VisualInstructions(primaryContent: VisualInstructionContent(text: "Hyde Street", maneuverType: .turn, maneuverModifier: .left, roundaboutExitDegrees: nil), secondaryContent: nil, triggerDistanceBeforeManeuver: 42.0)

return BannerView(instructions: instructions, distanceToNextManeuver: 42)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import Foundation
// Depending on the consumer's build setup, the low-level FFI code
// might be in a separate module, or it might be compiled inline into
// this module. This is a bit of light hackery to work with both.
#if canImport(ferrostarFFI)
import ferrostarFFI
#if canImport(ferrostar_coreFFI)
import ferrostar_coreFFI
#endif

private extension RustBuffer {
Expand Down Expand Up @@ -298,19 +298,6 @@ private func uniffiCheckCallStatus(

// Public interface members begin here.

private struct FfiConverterUInt8: FfiConverterPrimitive {
typealias FfiType = UInt8
typealias SwiftType = UInt8

public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> UInt8 {
return try lift(readInt(&buf))
}

public static func write(_ value: UInt8, into buf: inout [UInt8]) {
writeInt(&buf, lower(value))
}
}

private struct FfiConverterUInt16: FfiConverterPrimitive {
typealias FfiType = UInt16
typealias SwiftType = UInt16
Expand Down Expand Up @@ -388,6 +375,21 @@ private struct FfiConverterString: FfiConverter {
}
}

private struct FfiConverterData: FfiConverterRustBuffer {
typealias SwiftType = Data

public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> Data {
let len: Int32 = try readInt(&buf)
return try Data(readBytes(&buf, count: Int(len)))
}

public static func write(_ value: Data, into buf: inout [UInt8]) {
let len = Int32(value.count)
writeInt(&buf, len)
writeBytes(&buf, value)
}
}

private struct FfiConverterTimestamp: FfiConverterRustBuffer {
typealias SwiftType = Date

Expand Down Expand Up @@ -512,7 +514,7 @@ public func FfiConverterTypeNavigationController_lower(_ value: NavigationContro

public protocol RouteAdapterProtocol {
func generateRequest(userLocation: UserLocation, waypoints: [GeographicCoordinates]) throws -> RouteRequest
func parseResponse(response: [UInt8]) throws -> [Route]
func parseResponse(response: Data) throws -> [Route]
}

public class RouteAdapter: RouteAdapterProtocol {
Expand Down Expand Up @@ -557,11 +559,11 @@ public class RouteAdapter: RouteAdapterProtocol {
)
}

public func parseResponse(response: [UInt8]) throws -> [Route] {
public func parseResponse(response: Data) throws -> [Route] {
return try FfiConverterSequenceTypeRoute.lift(
rustCallWithError(FfiConverterTypeRoutingResponseParseError.lift) {
uniffi_ferrostar_core_fn_method_routeadapter_parse_response(self.pointer,
FfiConverterSequenceUInt8.lower(response), $0)
FfiConverterData.lower(response), $0)
}
)
}
Expand Down Expand Up @@ -673,7 +675,7 @@ public func FfiConverterTypeRouteRequestGenerator_lower(_ value: RouteRequestGen
}

public protocol RouteResponseParserProtocol {
func parseResponse(response: [UInt8]) throws -> [Route]
func parseResponse(response: Data) throws -> [Route]
}

public class RouteResponseParser: RouteResponseParserProtocol {
Expand All @@ -690,11 +692,11 @@ public class RouteResponseParser: RouteResponseParserProtocol {
try! rustCall { uniffi_ferrostar_core_fn_free_routeresponseparser(pointer, $0) }
}

public func parseResponse(response: [UInt8]) throws -> [Route] {
public func parseResponse(response: Data) throws -> [Route] {
return try FfiConverterSequenceTypeRoute.lift(
rustCallWithError(FfiConverterTypeRoutingResponseParseError.lift) {
uniffi_ferrostar_core_fn_method_routeresponseparser_parse_response(self.pointer,
FfiConverterSequenceUInt8.lower(response), $0)
FfiConverterData.lower(response), $0)
}
)
}
Expand Down Expand Up @@ -790,45 +792,45 @@ public func FfiConverterTypeCourseOverGround_lower(_ value: CourseOverGround) ->
}

public struct GeographicCoordinates {
public var lat: Double
public var lng: Double
public var lat: Double

// Default memberwise initializers are never public by default, so we
// declare one manually.
public init(lat: Double, lng: Double) {
self.lat = lat
public init(lng: Double, lat: Double) {
self.lng = lng
self.lat = lat
}
}

extension GeographicCoordinates: Equatable, Hashable {
public static func == (lhs: GeographicCoordinates, rhs: GeographicCoordinates) -> Bool {
if lhs.lat != rhs.lat {
if lhs.lng != rhs.lng {
return false
}
if lhs.lng != rhs.lng {
if lhs.lat != rhs.lat {
return false
}
return true
}

public func hash(into hasher: inout Hasher) {
hasher.combine(lat)
hasher.combine(lng)
hasher.combine(lat)
}
}

public struct FfiConverterTypeGeographicCoordinates: FfiConverterRustBuffer {
public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> GeographicCoordinates {
return try GeographicCoordinates(
lat: FfiConverterDouble.read(from: &buf),
lng: FfiConverterDouble.read(from: &buf)
lng: FfiConverterDouble.read(from: &buf),
lat: FfiConverterDouble.read(from: &buf)
)
}

public static func write(_ value: GeographicCoordinates, into buf: inout [UInt8]) {
FfiConverterDouble.write(value.lat, into: &buf)
FfiConverterDouble.write(value.lng, into: &buf)
FfiConverterDouble.write(value.lat, into: &buf)
}
}

Expand Down Expand Up @@ -1535,7 +1537,7 @@ extension NavigationStateUpdate: Equatable, Hashable {}
// Note that we don't yet support `indirect` for enums.
// See https://github.com/mozilla/uniffi-rs/issues/396 for further discussion.
public enum RouteRequest {
case httpPost(url: String, headers: [String: String], body: [UInt8])
case httpPost(url: String, headers: [String: String], body: Data)
}

public struct FfiConverterTypeRouteRequest: FfiConverterRustBuffer {
Expand All @@ -1547,7 +1549,7 @@ public struct FfiConverterTypeRouteRequest: FfiConverterRustBuffer {
case 1: return try .httpPost(
url: FfiConverterString.read(from: &buf),
headers: FfiConverterDictionaryStringString.read(from: &buf),
body: FfiConverterSequenceUInt8.read(from: &buf)
body: FfiConverterData.read(from: &buf)
)

default: throw UniffiInternalError.unexpectedEnumCase
Expand All @@ -1560,7 +1562,7 @@ public struct FfiConverterTypeRouteRequest: FfiConverterRustBuffer {
writeInt(&buf, Int32(1))
FfiConverterString.write(url, into: &buf)
FfiConverterDictionaryStringString.write(headers, into: &buf)
FfiConverterSequenceUInt8.write(body, into: &buf)
FfiConverterData.write(body, into: &buf)
}
}
}
Expand All @@ -1576,14 +1578,9 @@ public func FfiConverterTypeRouteRequest_lower(_ value: RouteRequest) -> RustBuf
extension RouteRequest: Equatable, Hashable {}

public enum RoutingRequestGenerationError {
// Simple error enums only carry a message
case NotEnoughWaypoints(message: String)

// Simple error enums only carry a message
case JsonError(message: String)

// Simple error enums only carry a message
case UnknownError(message: String)
case NotEnoughWaypoints
case JsonError
case UnknownError

fileprivate static func uniffiErrorHandler(_ error: RustBuffer) throws -> Error {
return try FfiConverterTypeRoutingRequestGenerationError.lift(error)
Expand All @@ -1596,29 +1593,23 @@ public struct FfiConverterTypeRoutingRequestGenerationError: FfiConverterRustBuf
public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> RoutingRequestGenerationError {
let variant: Int32 = try readInt(&buf)
switch variant {
case 1: return try .NotEnoughWaypoints(
message: FfiConverterString.read(from: &buf)
)

case 2: return try .JsonError(
message: FfiConverterString.read(from: &buf)
)

case 3: return try .UnknownError(
message: FfiConverterString.read(from: &buf)
)
case 1: return .NotEnoughWaypoints
case 2: return .JsonError
case 3: return .UnknownError

default: throw UniffiInternalError.unexpectedEnumCase
}
}

public static func write(_ value: RoutingRequestGenerationError, into buf: inout [UInt8]) {
switch value {
case .NotEnoughWaypoints(_ /* message is ignored*/ ):
case .NotEnoughWaypoints:
writeInt(&buf, Int32(1))
case .JsonError(_ /* message is ignored*/ ):

case .JsonError:
writeInt(&buf, Int32(2))
case .UnknownError(_ /* message is ignored*/ ):

case .UnknownError:
writeInt(&buf, Int32(3))
}
}
Expand Down Expand Up @@ -1852,28 +1843,6 @@ private struct FfiConverterOptionTypeManeuverType: FfiConverterRustBuffer {
}
}

private struct FfiConverterSequenceUInt8: FfiConverterRustBuffer {
typealias SwiftType = [UInt8]

public static func write(_ value: [UInt8], into buf: inout [UInt8]) {
let len = Int32(value.count)
writeInt(&buf, len)
for item in value {
FfiConverterUInt8.write(item, into: &buf)
}
}

public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> [UInt8] {
let len: Int32 = try readInt(&buf)
var seq = [UInt8]()
seq.reserveCapacity(Int(len))
for _ in 0 ..< len {
try seq.append(FfiConverterUInt8.read(from: &buf))
}
return seq
}
}

private struct FfiConverterSequenceTypeGeographicCoordinates: FfiConverterRustBuffer {
typealias SwiftType = [GeographicCoordinates]

Expand Down Expand Up @@ -2022,37 +1991,37 @@ private var initializationResult: InitializationResult {
if bindings_contract_version != scaffolding_contract_version {
return InitializationResult.contractVersionMismatch
}
if uniffi_ferrostar_core_checksum_func_create_osrm_response_parser() != 50895 {
if uniffi_ferrostar_core_checksum_func_create_osrm_response_parser() != 13999 {
return InitializationResult.apiChecksumMismatch
}
if uniffi_ferrostar_core_checksum_func_create_valhalla_request_generator() != 62930 {
if uniffi_ferrostar_core_checksum_func_create_valhalla_request_generator() != 29123 {
return InitializationResult.apiChecksumMismatch
}
if uniffi_ferrostar_core_checksum_method_navigationcontroller_advance_to_next_step() != 53731 {
if uniffi_ferrostar_core_checksum_method_navigationcontroller_advance_to_next_step() != 1041 {
return InitializationResult.apiChecksumMismatch
}
if uniffi_ferrostar_core_checksum_method_navigationcontroller_update_user_location() != 4353 {
if uniffi_ferrostar_core_checksum_method_navigationcontroller_update_user_location() != 64701 {
return InitializationResult.apiChecksumMismatch
}
if uniffi_ferrostar_core_checksum_method_routeadapter_generate_request() != 46269 {
if uniffi_ferrostar_core_checksum_method_routeadapter_generate_request() != 61210 {
return InitializationResult.apiChecksumMismatch
}
if uniffi_ferrostar_core_checksum_method_routeadapter_parse_response() != 11562 {
if uniffi_ferrostar_core_checksum_method_routeadapter_parse_response() != 49628 {
return InitializationResult.apiChecksumMismatch
}
if uniffi_ferrostar_core_checksum_method_routerequestgenerator_generate_request() != 65091 {
if uniffi_ferrostar_core_checksum_method_routerequestgenerator_generate_request() != 10361 {
return InitializationResult.apiChecksumMismatch
}
if uniffi_ferrostar_core_checksum_method_routeresponseparser_parse_response() != 27004 {
if uniffi_ferrostar_core_checksum_method_routeresponseparser_parse_response() != 54334 {
return InitializationResult.apiChecksumMismatch
}
if uniffi_ferrostar_core_checksum_constructor_navigationcontroller_new() != 55587 {
if uniffi_ferrostar_core_checksum_constructor_navigationcontroller_new() != 12876 {
return InitializationResult.apiChecksumMismatch
}
if uniffi_ferrostar_core_checksum_constructor_routeadapter_new() != 17242 {
if uniffi_ferrostar_core_checksum_constructor_routeadapter_new() != 32286 {
return InitializationResult.apiChecksumMismatch
}
if uniffi_ferrostar_core_checksum_constructor_routeadapter_new_valhalla_http() != 148 {
if uniffi_ferrostar_core_checksum_constructor_routeadapter_new_valhalla_http() != 17926 {
return InitializationResult.apiChecksumMismatch
}

Expand Down
4 changes: 2 additions & 2 deletions apple/Tests/FerrostarCoreTests/FerrostarCoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ private class MockRouteAdapter: RouteAdapterProtocol {
}

func generateRequest(userLocation: UniFFI.UserLocation, waypoints: [UniFFI.GeographicCoordinates]) throws -> UniFFI.RouteRequest {
return UniFFI.RouteRequest.httpPost(url: backendUrl.absoluteString, headers: [:], body: [])
return UniFFI.RouteRequest.httpPost(url: backendUrl.absoluteString, headers: [:], body: Data())
}

func parseResponse(response _: [UInt8]) throws -> [UniFFI.Route] {
func parseResponse(response _: Data) throws -> [UniFFI.Route] {
return routes
}
}
Expand Down
Loading

0 comments on commit 5f2d60d

Please sign in to comment.