Skip to content

Commit

Permalink
Support arbitrary Valhalla options (formerly we only merged costing o…
Browse files Browse the repository at this point in the history
…pts) (#268)

* Support arbitrary Valhalla options (formerly we only merged costing options)

* Fix argument

* Improve docs of some convenience initializers

* Align Android with iOS costing options

* swiftformat

* Don't forget the web ;)

* Improve comment

* Cleanup

* Apply automatic changes

* Doesn't need a full string ref

* Update error name in iOS app

---------

Co-authored-by: ianthetechie <[email protected]>
  • Loading branch information
ianthetechie and ianthetechie authored Sep 27, 2024
1 parent f9e91fb commit 3d46f51
Show file tree
Hide file tree
Showing 19 changed files with 194 additions and 68 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ if useLocalFramework {
path: "./common/target/ios/libferrostar-rs.xcframework"
)
} else {
let releaseTag = "0.14.0"
let releaseTag = "0.15.0"
let releaseChecksum = "f8503dc1657b99c83489bac78dd2f4c014ae3d20e9d83f3e779260d255b6cbbb"
binaryTarget = .binaryTarget(
name: "FerrostarCoreRS",
Expand Down
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ plugins {

allprojects {
group = "com.stadiamaps.ferrostar"
version = "0.14.0"
version = "0.15.0"
}
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ class ValhallaCoreTest {
navigationControllerConfig =
NavigationControllerConfig(
StepAdvanceMode.Manual, RouteDeviationTracking.None, CourseFiltering.RAW),
costingOptions = mapOf("auto" to mapOf("useTolls" to 0)))
options = mapOf("costing_options" to mapOf("auto" to mapOf("useTolls" to 0))))

return runTest {
val routes =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,11 @@ class FerrostarCore(
locationProvider: LocationProvider,
navigationControllerConfig: NavigationControllerConfig,
foregroundServiceManager: ForegroundServiceManager? = null,
costingOptions: Map<String, Any> = emptyMap(),
options: Map<String, Any> = emptyMap(),
) : this(
RouteProvider.RouteAdapter(
RouteAdapter.newValhallaHttp(
valhallaEndpointURL.toString(), profile, jsonAdapter.toJson(costingOptions))),
valhallaEndpointURL.toString(), profile, jsonAdapter.toJson(options))),
httpClient,
locationProvider,
foregroundServiceManager,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ object AppModule {
minimumHorizontalAccuracy = 25U, automaticAdvanceDistance = 10U),
RouteDeviationTracking.StaticThreshold(15U, 25.0),
CourseFiltering.SNAP_TO_ROUTE),
costingOptions = mapOf("bicycle" to mapOf("use_roads" to 0.2)))
options = mapOf("costingOptions" to mapOf("bicycle" to mapOf("use_roads" to 0.2))))

// Not all navigation apps will require this sort of extra configuration.
// In fact, we hope that most don't!
Expand Down
2 changes: 1 addition & 1 deletion apple/DemoApp/Demo/DemoNavigationView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ struct DemoNavigationView: View {
profile: "bicycle",
locationProvider: locationProvider,
navigationControllerConfig: config,
costingOptions: ["bicycle": ["use_roads": 0.2]]
options: ["costing_options": ["bicycle": ["use_roads": 0.2]]]
)
// NOTE: Not all applications will need a delegate. Read the NavigationDelegate documentation for details.
ferrostarCore.delegate = navigationDelegate
Expand Down
25 changes: 20 additions & 5 deletions apple/Sources/FerrostarCore/FerrostarCore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ public protocol FerrostarCoreDelegate: AnyObject {

private var config: SwiftNavigationControllerConfig

/// Initializes a core instance with the given parameters.
///
/// This designated initializer is the most flexible, but the convenience ones may be easier to use.
/// for common configuraitons.
public init(
routeProvider: RouteProvider,
locationProvider: LocationProviding,
Expand All @@ -110,25 +114,36 @@ public protocol FerrostarCoreDelegate: AnyObject {
locationProvider.delegate = self
}

/// Initializes a core instance for a Valhalla API accessed over HTTP.
///
/// - Parameters
/// - valhallaEndpointUrl: The URL of the Valhalla endpoint you're trying to hit for route requests. If necessary,
/// include your API key here.
/// - profile: The Valhalla costing model to use for route requests.
/// - navigationControllerConfig: Configuration of the navigation session.
/// - options: A dictionary of options to include in the request. The Valhalla request generator sets several
/// automatically (like `format`), but this lets you add arbitrary options so you can access the full API.
/// - networkSession: The network session to use. Don't set this unless you need to replace the networking stack
/// (ex: for testing).
public convenience init(
valhallaEndpointUrl: URL,
profile: String,
locationProvider: LocationProviding,
navigationControllerConfig: SwiftNavigationControllerConfig,
costingOptions: [String: Any] = [:],
options: [String: Any] = [:],
networkSession: URLRequestLoading = URLSession.shared
) throws {
guard let jsonCostingOptions = try String(
data: JSONSerialization.data(withJSONObject: costingOptions),
guard let jsonOptions = try String(
data: JSONSerialization.data(withJSONObject: options),
encoding: .utf8
) else {
throw InstantiationError.JsonError
throw InstantiationError.OptionsJsonParseError
}

let adapter = try RouteAdapter.newValhallaHttp(
endpointUrl: valhallaEndpointUrl.absoluteString,
profile: profile,
costingOptionsJson: jsonCostingOptions
optionsJson: jsonOptions
)
self.init(
routeProvider: .routeAdapter(adapter),
Expand Down
18 changes: 9 additions & 9 deletions apple/Sources/UniFFI/ferrostar.swift

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion apple/Tests/FerrostarCoreTests/FerrostarCoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ final class FerrostarCoreTests: XCTestCase {
routeDeviationTracking: .none,
snappedLocationCourseFiltering: .raw
),
costingOptions: ["low_speed_vehicle": ["vehicle_type": "golf_cart"]],
options: ["costing_options": ["low_speed_vehicle": ["vehicle_type": "golf_cart"]]],
networkSession: mockSession
)

Expand Down
2 changes: 1 addition & 1 deletion common/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion common/ferrostar/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ lints.workspace = true

[package]
name = "ferrostar"
version = "0.14.0"
version = "0.15.0"
readme = "README.md"
description = "The core of modern turn-by-turn navigation."
keywords = ["navigation", "routing", "valhalla", "osrm"]
Expand Down
14 changes: 6 additions & 8 deletions common/ferrostar/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,13 @@ impl UniffiCustomTypeConverter for Uuid {
fn create_valhalla_request_generator(
endpoint_url: String,
profile: String,
costing_options_json: Option<String>,
options_json: Option<String>,
) -> Result<Arc<dyn RouteRequestGenerator>, InstantiationError> {
Ok(Arc::new(
ValhallaHttpRequestGenerator::with_costing_options_json(
endpoint_url,
profile,
costing_options_json,
)?,
))
Ok(Arc::new(ValhallaHttpRequestGenerator::with_options_json(
endpoint_url,
profile,
options_json.as_deref(),
)?))
}

/// Creates a [`RouteResponseParser`] capable of parsing OSRM responses.
Expand Down
9 changes: 6 additions & 3 deletions common/ferrostar/src/routing_adapters/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ use alloc::string::{String, ToString};
#[cfg_attr(feature = "uniffi", derive(uniffi::Error))]
#[cfg_attr(feature = "std", derive(thiserror::Error))]
pub enum InstantiationError {
#[cfg_attr(feature = "std", error("Error generating JSON for the request."))]
JsonError,
#[cfg_attr(
feature = "std",
error("Error parsing the JSON options for the request.")
)]
OptionsJsonParseError,
}

// TODO: See comment above
Expand Down Expand Up @@ -40,7 +43,7 @@ impl From<uniffi::UnexpectedUniFFICallbackError> for RoutingRequestGenerationErr

impl From<serde_json::Error> for InstantiationError {
fn from(_: serde_json::Error) -> Self {
InstantiationError::JsonError
InstantiationError::OptionsJsonParseError
}
}

Expand Down
6 changes: 3 additions & 3 deletions common/ferrostar/src/routing_adapters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,12 @@ impl RouteAdapter {
pub fn new_valhalla_http(
endpoint_url: String,
profile: String,
costing_options_json: Option<String>,
options_json: Option<String>,
) -> Result<Self, InstantiationError> {
let request_generator = Arc::new(ValhallaHttpRequestGenerator::with_costing_options_json(
let request_generator = Arc::new(ValhallaHttpRequestGenerator::with_options_json(
endpoint_url,
profile,
costing_options_json,
options_json.as_deref(),
)?);
let response_parser = Arc::new(OsrmResponseParser::new(6));
Ok(Self::new(request_generator, response_parser))
Expand Down
Loading

0 comments on commit 3d46f51

Please sign in to comment.