Waypoint.res.mjs
1// Generated by ReScript, PLEASE EDIT WITH CARE
2
3import * as Log from "../utils/Log.res.mjs";
4import * as Core__JSON from "@rescript/core/lib/es6/src/Core__JSON.res.mjs";
5import * as Core__Array from "@rescript/core/lib/es6/src/Core__Array.res.mjs";
6import * as Core__Option from "@rescript/core/lib/es6/src/Core__Option.res.mjs";
7
8function decodeRoute(json) {
9 let dict = Core__JSON.Decode.object(json);
10 if (dict === undefined) {
11 return;
12 }
13 let id = Core__Option.flatMap(dict["id"], Core__JSON.Decode.string);
14 let d = Core__Option.flatMap(dict["d"], Core__JSON.Decode.float);
15 let t = Core__Option.flatMap(dict["t"], Core__JSON.Decode.float);
16 let arr = Core__Option.flatMap(dict["bbox"], Core__JSON.Decode.array);
17 let bbox = arr !== undefined ? Core__Array.filterMap(arr, Core__JSON.Decode.float) : undefined;
18 return {
19 id: id,
20 d: d,
21 t: t,
22 bbox: bbox
23 };
24}
25
26function fromJSON(json) {
27 let dict = Core__JSON.Decode.object(json);
28 if (dict === undefined) {
29 return {
30 TAG: "Error",
31 _0: "Waypoint JSON is not an object"
32 };
33 }
34 let id = Core__Option.flatMap(dict["id"], Core__JSON.Decode.string);
35 let arr = Core__Option.flatMap(dict["point"], Core__JSON.Decode.array);
36 let point;
37 if (arr !== undefined) {
38 let lon = Core__Option.flatMap(arr[0], Core__JSON.Decode.float);
39 let lat = Core__Option.flatMap(arr[1], Core__JSON.Decode.float);
40 point = lon !== undefined && lat !== undefined ? [
41 lon,
42 lat
43 ] : undefined;
44 } else {
45 point = undefined;
46 }
47 let label = Core__Option.flatMap(dict["label"], Core__JSON.Decode.string);
48 let gid = Core__Option.flatMap(dict["gid"], Core__JSON.Decode.string);
49 let country = Core__Option.flatMap(dict["country"], Core__JSON.Decode.string);
50 if (id === undefined) {
51 return {
52 TAG: "Error",
53 _0: "Missing required fields in waypoint JSON"
54 };
55 }
56 if (point === undefined) {
57 return {
58 TAG: "Error",
59 _0: "Missing required fields in waypoint JSON"
60 };
61 }
62 if (label === undefined) {
63 return {
64 TAG: "Error",
65 _0: "Missing required fields in waypoint JSON"
66 };
67 }
68 if (gid === undefined) {
69 return {
70 TAG: "Error",
71 _0: "Missing required fields in waypoint JSON"
72 };
73 }
74 if (country === undefined) {
75 return {
76 TAG: "Error",
77 _0: "Missing required fields in waypoint JSON"
78 };
79 }
80 let fromDict = Core__Option.flatMap(dict["from"], Core__JSON.Decode.object);
81 let from = fromDict !== undefined ? Object.fromEntries(Core__Array.filterMap(Object.entries(fromDict), param => {
82 let route = decodeRoute(param[1]);
83 if (route !== undefined) {
84 return [
85 param[0],
86 route
87 ];
88 }
89 })) : undefined;
90 let arr$1 = Core__Option.flatMap(dict["cons"], Core__JSON.Decode.array);
91 let cons = arr$1 !== undefined ? Core__Array.filterMap(arr$1, Core__JSON.Decode.string) : [];
92 let nonroutable = Core__Option.getOr(Core__Option.flatMap(dict["nonroutable"], Core__JSON.Decode.bool), false);
93 let arr$2 = Core__Option.flatMap(dict["error"], Core__JSON.Decode.array);
94 let error;
95 if (arr$2 !== undefined) {
96 let strings = Core__Array.filterMap(arr$2, Core__JSON.Decode.string);
97 error = strings.length > 0 ? strings : undefined;
98 } else {
99 error = undefined;
100 }
101 return {
102 TAG: "Ok",
103 _0: {
104 id: id,
105 point: point,
106 from: from,
107 label: label,
108 gid: gid,
109 country: country,
110 cons: cons,
111 nonroutable: nonroutable,
112 error: error
113 }
114 };
115}
116
117function fromYjsArray(jsonArray) {
118 return Core__Array.filterMap(jsonArray.map(json => {
119 let waypoint = fromJSON(json);
120 if (waypoint.TAG === "Ok") {
121 return waypoint._0;
122 }
123 Log.error("Failed to decode waypoint", {
124 error: waypoint._0
125 });
126 }), x => x);
127}
128
129function extractFromNode(node) {
130 let dict = Core__JSON.Decode.object(node);
131 if (dict === undefined) {
132 return [];
133 }
134 let nodeType = Core__Option.flatMap(dict["type"], Core__JSON.Decode.string);
135 let attrsJson = dict["attrs"];
136 let contentJson = dict["content"];
137 let thisWaypoint;
138 if (nodeType !== undefined) {
139 if (nodeType === "waypoint") {
140 if (attrsJson !== undefined) {
141 let wp = fromJSON(attrsJson);
142 thisWaypoint = wp.TAG === "Ok" ? [wp._0] : [];
143 } else {
144 thisWaypoint = [];
145 }
146 } else {
147 thisWaypoint = [];
148 }
149 } else {
150 thisWaypoint = [];
151 }
152 let childWaypoints;
153 if (contentJson !== undefined) {
154 let arr = Core__JSON.Decode.array(contentJson);
155 childWaypoints = arr !== undefined ? arr.map(extractFromNode).flat() : [];
156 } else {
157 childWaypoints = [];
158 }
159 return thisWaypoint.concat(childWaypoints);
160}
161
162let extractFromDocument = extractFromNode;
163
164function findById(waypoints, id) {
165 return waypoints.find(wp => wp.id === id);
166}
167
168function isOrigin(wp) {
169 if (wp.nonroutable) {
170 return false;
171 }
172 let dict = wp.from;
173 if (dict !== undefined) {
174 return Object.entries(dict).length === 0;
175 } else {
176 return true;
177 }
178}
179
180function getOrigin(waypoints) {
181 return waypoints.find(isOrigin);
182}
183
184export {
185 decodeRoute,
186 fromJSON,
187 fromYjsArray,
188 extractFromNode,
189 extractFromDocument,
190 findById,
191 isOrigin,
192 getOrigin,
193}
194/* No side effect */