Skip to main content

thrust_wasm/
field15.rs

1use serde::{Deserialize, Serialize};
2use wasm_bindgen::prelude::*;
3
4use thrust::data::field15::{Field15Element, Field15Parser};
5
6/// Parse a raw ICAO field 15 route string into a structured token array.
7///
8/// Returns a JS array of token objects. Each token is one of:
9///   - `{ "waypoint": "LACOU" }` — named waypoint or navaid
10///   - `{ "aerodrome": "LFPG" }` — ICAO aerodrome code
11///   - `{ "coords": [lat, lon] }` — latitude/longitude coordinates
12///   - `{ "point_bearing_distance": { "point": ..., "bearing": 180, "distance": 60 } }`
13///   - `{ "airway": "UM184" }` — ATS route
14///   - `"DCT"` — direct routing
15///   - `{ "SID": "RANUX1A" }` — SID designator
16///   - `{ "STAR": "LORNI1A" }` — STAR designator
17///   - `{ "speed": ..., "altitude": ... }` — speed/altitude modifier
18///   - `"VFR"`, `"IFR"`, `"OAT"`, `"GAT"`, `"IFPSTOP"`, `"IFPSTART"`, `{ "STAY": ... }`, ...
19#[wasm_bindgen(js_name = parseField15)]
20pub fn parse_field15(route: &str) -> Result<JsValue, JsValue> {
21    let elements: Vec<Field15Element> = Field15Parser::parse(route);
22    serde_wasm_bindgen::to_value(&elements).map_err(|e| JsValue::from_str(&e.to_string()))
23}
24
25/// A resolved geographic point in an enriched route.
26#[derive(Debug, Clone, Serialize, Deserialize)]
27pub struct ResolvedPoint {
28    pub latitude: f64,
29    pub longitude: f64,
30    #[serde(skip_serializing_if = "Option::is_none")]
31    pub name: Option<String>,
32    #[serde(skip_serializing_if = "Option::is_none")]
33    pub kind: Option<String>,
34}
35
36/// A resolved route segment (start → end with optional airway name).
37#[derive(Debug, Clone, Serialize, Deserialize)]
38pub struct RouteSegment {
39    pub start: ResolvedPoint,
40    pub end: ResolvedPoint,
41    #[serde(skip_serializing_if = "Option::is_none")]
42    pub name: Option<String>,
43    #[serde(skip_serializing_if = "Option::is_none")]
44    pub segment_type: Option<String>,
45    #[serde(skip_serializing_if = "Option::is_none")]
46    pub connector: Option<String>,
47}