Skip to content

Commit

Permalink
Initial draft of adding TypeScript types
Browse files Browse the repository at this point in the history
  • Loading branch information
CatMe0w committed Aug 12, 2024
1 parent dca060d commit eea95c0
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 0 deletions.
2 changes: 2 additions & 0 deletions common/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[env]
TS_RS_EXPORT_DIR = { value = "ferrostar/pkg", relative = true }
41 changes: 41 additions & 0 deletions common/Cargo.lock

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

1 change: 1 addition & 0 deletions common/ferrostar/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ proptest = { version = "1.4.0", default-features = false }
insta = { version = "1.33.0", features = ["yaml"] }
rstest = "0.21.0"
wasm-bindgen-test = "0.3"
ts-rs = { git = "https://github.com/Aleph-Alpha/ts-rs", features = ["uuid-impl"] }

[lib]
crate-type = ["cdylib", "staticlib", "lib"]
6 changes: 6 additions & 0 deletions common/ferrostar/src/deviation_detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use {
navigation_controller::test_helpers::{gen_dummy_route_step, gen_route_from_steps},
},
proptest::prelude::*,
ts_rs::TS,
};

#[cfg(all(test, feature = "std", not(feature = "web-time")))]
Expand All @@ -40,6 +41,8 @@ use web_time::SystemTime;
#[derive(Clone)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
#[cfg_attr(feature = "wasm-bindgen", derive(Deserialize))]
#[cfg_attr(test, derive(TS))]
#[cfg_attr(test, ts(export, export_to = "types.d.ts"))]
pub enum RouteDeviationTracking {
/// No checks will be done, and we assume the user is always following the route.
None,
Expand All @@ -59,6 +62,7 @@ pub enum RouteDeviationTracking {
/// An arbitrary user-defined implementation.
/// You decide with your own [`RouteDeviationDetector`] implementation!
#[cfg_attr(feature = "wasm-bindgen", serde(skip))]
#[cfg_attr(test, ts(skip))]
Custom {
detector: Arc<dyn RouteDeviationDetector>,
},
Expand Down Expand Up @@ -112,6 +116,8 @@ impl RouteDeviationTracking {
#[derive(Debug, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
#[cfg_attr(feature = "wasm-bindgen", derive(Serialize, Deserialize))]
#[cfg_attr(test, derive(TS))]
#[cfg_attr(test, ts(export, export_to = "types.d.ts"))]
pub enum RouteDeviation {
/// The user is proceeding on course within the expected tolerances; everything is normal.
NoDeviation,
Expand Down
59 changes: 59 additions & 0 deletions common/ferrostar/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ use web_time::SystemTime;
#[cfg(any(test, feature = "wasm-bindgen"))]
use serde::Serialize;

#[cfg(feature = "wasm-bindgen")]
use wasm_bindgen::prelude::*;

#[cfg(test)]
use ts_rs::TS;

use uuid::Uuid;

#[derive(Debug)]
Expand All @@ -40,6 +46,8 @@ pub enum ModelError {
#[derive(Clone, Copy, PartialEq, PartialOrd, Debug)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
#[cfg_attr(any(feature = "wasm-bindgen", test), derive(Serialize, Deserialize))]
#[cfg_attr(test, derive(TS))]
#[cfg_attr(test, ts(export, export_to = "types.d.ts"))]
pub struct GeographicCoordinate {
/// The latitude (in degrees).
pub lat: f64,
Expand Down Expand Up @@ -94,6 +102,8 @@ impl From<GeographicCoordinate> for Point {
#[derive(Clone, Copy, PartialEq, PartialOrd, Debug)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
#[cfg_attr(any(feature = "wasm-bindgen", test), derive(Serialize, Deserialize))]
#[cfg_attr(test, derive(TS))]
#[cfg_attr(test, ts(export, export_to = "types.d.ts"))]
pub struct Waypoint {
pub coordinate: GeographicCoordinate,
pub kind: WaypointKind,
Expand All @@ -103,6 +113,8 @@ pub struct Waypoint {
#[derive(Clone, Copy, PartialEq, PartialOrd, Debug)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
#[cfg_attr(any(feature = "wasm-bindgen", test), derive(Serialize, Deserialize))]
#[cfg_attr(test, derive(TS))]
#[cfg_attr(test, ts(export, export_to = "types.d.ts"))]
pub enum WaypointKind {
/// Starts or ends a leg of the trip.
///
Expand All @@ -116,6 +128,8 @@ pub enum WaypointKind {
#[derive(Clone, Copy, PartialEq, PartialOrd, Debug)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
#[cfg_attr(any(feature = "wasm-bindgen", test), derive(Serialize, Deserialize))]
#[cfg_attr(test, derive(TS))]
#[cfg_attr(test, ts(export, export_to = "types.d.ts"))]
pub struct BoundingBox {
/// The southwest corner of the bounding box.
pub sw: GeographicCoordinate,
Expand Down Expand Up @@ -148,6 +162,8 @@ pub struct Heading {
#[derive(Clone, Copy, PartialEq, PartialOrd, Debug)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
#[cfg_attr(any(feature = "wasm-bindgen", test), derive(Serialize, Deserialize))]
#[cfg_attr(test, derive(TS))]
#[cfg_attr(test, ts(export, export_to = "types.d.ts"))]
pub struct CourseOverGround {
/// The direction in which the user's device is traveling, measured in clockwise degrees from
/// true north (N = 0, E = 90, S = 180, W = 270).
Expand All @@ -166,6 +182,8 @@ impl CourseOverGround {
#[derive(Clone, Copy, PartialEq, PartialOrd, Debug)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
#[cfg_attr(any(feature = "wasm-bindgen", test), derive(Serialize, Deserialize))]
#[cfg_attr(test, derive(TS))]
#[cfg_attr(test, ts(export, export_to = "types.d.ts"))]
pub struct Speed {
/// The user's speed in meters per second.
pub value: f64,
Expand Down Expand Up @@ -214,13 +232,16 @@ mod system_time_format {
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
#[cfg_attr(any(feature = "wasm-bindgen", test), derive(Serialize, Deserialize))]
#[cfg_attr(feature = "wasm-bindgen", serde(rename_all = "camelCase"))]
#[cfg_attr(test, derive(TS))]
#[cfg_attr(test, ts(export, export_to = "types.d.ts"))]
pub struct UserLocation {
pub coordinates: GeographicCoordinate,
/// The estimated accuracy of the coordinate (in meters)
pub horizontal_accuracy: f64,
pub course_over_ground: Option<CourseOverGround>,
#[cfg_attr(test, serde(skip_serializing))]
#[cfg_attr(feature = "wasm-bindgen", serde(with = "system_time_format"))]
#[cfg_attr(test, ts(as = "u64"))]
pub timestamp: SystemTime,
pub speed: Option<Speed>,
}
Expand All @@ -238,6 +259,8 @@ impl From<UserLocation> for Point {
#[derive(Clone, Debug)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
#[cfg_attr(any(feature = "wasm-bindgen", test), derive(Serialize, Deserialize))]
#[cfg_attr(test, derive(TS))]
#[cfg_attr(test, ts(export, export_to = "types.d.ts"))]
pub struct Route {
pub geometry: Vec<GeographicCoordinate>,
pub bbox: BoundingBox,
Expand Down Expand Up @@ -269,6 +292,8 @@ fn get_route_polyline(route: &Route, precision: u32) -> Result<String, ModelErro
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
#[cfg_attr(any(feature = "wasm-bindgen", test), derive(Serialize, Deserialize))]
#[cfg_attr(feature = "wasm-bindgen", serde(rename_all = "camelCase"))]
#[cfg_attr(test, derive(TS))]
#[cfg_attr(test, ts(export, export_to = "types.d.ts"))]
pub struct RouteStep {
pub geometry: Vec<GeographicCoordinate>,
/// The distance, in meters, to travel along the route after the maneuver to reach the next step.
Expand Down Expand Up @@ -338,13 +363,36 @@ impl RouteStep {
}
}

// #[cfg(feature = "wasm-bindgen")]
// mod uuid_format {
// use serde::{self, Deserialize, Deserializer, Serializer};
// use uuid::Uuid;

// pub fn serialize<S>(uuid: &Uuid, serializer: S) -> Result<S::Ok, S::Error>
// where
// S: Serializer,
// {
// serializer.serialize_str(&uuid.to_string())
// }

// pub fn deserialize<'de, D>(deserializer: D) -> Result<Uuid, D::Error>
// where
// D: Deserializer<'de>,
// {
// let s = String::deserialize(deserializer)?;
// Uuid::parse_str(&s).map_err(serde::de::Error::custom)
// }
// }

/// An instruction that can be synthesized using a TTS engine to announce an upcoming maneuver.
///
/// Note that these do not have any locale information attached.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
#[cfg_attr(any(feature = "wasm-bindgen", test), derive(Serialize, Deserialize))]
#[cfg_attr(feature = "wasm-bindgen", serde(rename_all = "camelCase"))]
#[cfg_attr(test, derive(TS))]
#[cfg_attr(test, ts(export, export_to = "types.d.ts"))]
pub struct SpokenInstruction {
/// Plain-text instruction which can be synthesized with a TTS engine.
pub text: String,
Expand All @@ -370,6 +418,8 @@ pub struct SpokenInstruction {
#[derive(Deserialize, Debug, Copy, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
#[cfg_attr(any(test, feature = "wasm-bindgen"), derive(Serialize))]
#[cfg_attr(test, derive(TS))]
#[cfg_attr(test, ts(export, export_to = "types.d.ts"))]
#[serde(rename_all = "lowercase")]
pub enum ManeuverType {
Turn,
Expand Down Expand Up @@ -401,6 +451,8 @@ pub enum ManeuverType {
#[derive(Deserialize, Debug, Copy, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
#[cfg_attr(any(test, feature = "wasm-bindgen"), derive(Serialize))]
#[cfg_attr(test, derive(TS))]
#[cfg_attr(test, ts(export, export_to = "types.d.ts"))]
#[serde(rename_all = "lowercase")]
pub enum ManeuverModifier {
UTurn,
Expand All @@ -422,6 +474,8 @@ pub enum ManeuverModifier {
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
#[cfg_attr(any(feature = "wasm-bindgen", test), derive(Serialize, Deserialize))]
#[cfg_attr(feature = "wasm-bindgen", serde(rename_all = "camelCase"))]
#[cfg_attr(test, derive(TS))]
#[cfg_attr(test, ts(export, export_to = "types.d.ts"))]
pub struct VisualInstructionContent {
/// The text to display.
pub text: String,
Expand All @@ -442,6 +496,8 @@ pub struct VisualInstructionContent {
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
#[cfg_attr(any(feature = "wasm-bindgen", test), derive(Serialize, Deserialize))]
#[cfg_attr(feature = "wasm-bindgen", serde(rename_all = "camelCase"))]
#[cfg_attr(test, derive(TS))]
#[cfg_attr(test, ts(export, export_to = "types.d.ts"))]
pub struct VisualInstruction {
/// The primary instruction content.
///
Expand All @@ -453,6 +509,9 @@ pub struct VisualInstruction {
pub trigger_distance_before_maneuver: f64,
}

#[cfg_attr(feature = "wasm-bindgen", wasm_bindgen(typescript_custom_section))]
const TS_TYPES: &'static str = r#"/// <reference path="types.d.ts" />"#;

#[cfg(test)]
#[cfg(feature = "uniffi")]
mod tests {
Expand Down
11 changes: 11 additions & 0 deletions common/ferrostar/src/navigation_controller/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ use geo::LineString;
#[cfg(feature = "wasm-bindgen")]
use serde::{Deserialize, Serialize};

#[cfg(test)]
use ts_rs::TS;

/// High-level state describing progress through a route.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
#[cfg_attr(feature = "wasm-bindgen", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "wasm-bindgen", serde(rename_all = "camelCase"))]
#[cfg_attr(test, derive(TS))]
#[cfg_attr(test, ts(export, export_to = "types.d.ts"))]
pub struct TripProgress {
/// The distance to the next maneuver, in meters.
pub distance_to_next_maneuver: f64,
Expand All @@ -28,6 +33,8 @@ pub struct TripProgress {
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
#[cfg_attr(feature = "wasm-bindgen", derive(Serialize, Deserialize))]
#[cfg_attr(test, derive(TS))]
#[cfg_attr(test, ts(export, export_to = "types.d.ts"))]
pub enum TripState {
#[cfg_attr(feature = "wasm-bindgen", serde(rename_all = "camelCase"))]
Navigating {
Expand Down Expand Up @@ -76,6 +83,8 @@ pub enum StepAdvanceStatus {
#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
#[cfg_attr(feature = "wasm-bindgen", derive(Deserialize))]
#[cfg_attr(test, derive(TS))]
#[cfg_attr(test, ts(export, export_to = "types.d.ts"))]
pub enum StepAdvanceMode {
/// Never advances to the next step automatically;
/// requires calling [`NavigationController::advance_to_next_step`](super::NavigationController::advance_to_next_step).
Expand Down Expand Up @@ -108,6 +117,8 @@ pub enum StepAdvanceMode {
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
#[cfg_attr(feature = "wasm-bindgen", derive(Deserialize))]
#[cfg_attr(feature = "wasm-bindgen", serde(rename_all = "camelCase"))]
#[cfg_attr(test, derive(TS))]
#[cfg_attr(test, ts(export, export_to = "types.d.ts"))]
pub struct NavigationControllerConfig {
pub step_advance: StepAdvanceMode,
pub route_deviation_tracking: RouteDeviationTracking,
Expand Down

0 comments on commit eea95c0

Please sign in to comment.