solverforge_maps/routing/
config.rs1use std::path::PathBuf;
4use std::time::Duration;
5
6use serde::{Deserialize, Serialize};
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9pub enum ConnectivityPolicy {
10 KeepAll,
11 LargestStronglyConnectedComponent,
12}
13
14#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
17#[serde(rename_all = "snake_case")]
18pub enum RoadClass {
19 Motorway,
20 Trunk,
21 Primary,
22 Secondary,
23 Tertiary,
24 Residential,
25 Unclassified,
26 Service,
27 LivingStreet,
28 Other,
30}
31
32impl RoadClass {
33 pub const ALL: [RoadClass; 10] = [
35 RoadClass::Motorway,
36 RoadClass::Trunk,
37 RoadClass::Primary,
38 RoadClass::Secondary,
39 RoadClass::Tertiary,
40 RoadClass::Residential,
41 RoadClass::Unclassified,
42 RoadClass::Service,
43 RoadClass::LivingStreet,
44 RoadClass::Other,
45 ];
46
47 pub fn from_highway(highway: &str) -> Self {
49 match highway {
50 "motorway" | "motorway_link" => RoadClass::Motorway,
51 "trunk" | "trunk_link" => RoadClass::Trunk,
52 "primary" | "primary_link" => RoadClass::Primary,
53 "secondary" | "secondary_link" => RoadClass::Secondary,
54 "tertiary" | "tertiary_link" => RoadClass::Tertiary,
55 "residential" => RoadClass::Residential,
56 "unclassified" => RoadClass::Unclassified,
57 "service" => RoadClass::Service,
58 "living_street" => RoadClass::LivingStreet,
59 _ => RoadClass::Other,
60 }
61 }
62
63 pub fn as_str(self) -> &'static str {
65 match self {
66 RoadClass::Motorway => "motorway",
67 RoadClass::Trunk => "trunk",
68 RoadClass::Primary => "primary",
69 RoadClass::Secondary => "secondary",
70 RoadClass::Tertiary => "tertiary",
71 RoadClass::Residential => "residential",
72 RoadClass::Unclassified => "unclassified",
73 RoadClass::Service => "service",
74 RoadClass::LivingStreet => "living_street",
75 RoadClass::Other => "other",
76 }
77 }
78
79 pub fn parse(name: &str) -> Option<Self> {
81 RoadClass::ALL
82 .into_iter()
83 .find(|class| class.as_str() == name)
84 }
85}
86
87#[derive(Debug, Clone)]
88pub struct SpeedProfile {
89 pub motorway: f64,
90 pub trunk: f64,
91 pub primary: f64,
92 pub secondary: f64,
93 pub tertiary: f64,
94 pub residential: f64,
95 pub unclassified: f64,
96 pub service: f64,
97 pub living_street: f64,
98 pub default: f64,
99}
100
101impl Default for SpeedProfile {
102 fn default() -> Self {
103 Self {
104 motorway: 100.0,
105 trunk: 80.0,
106 primary: 60.0,
107 secondary: 50.0,
108 tertiary: 40.0,
109 residential: 30.0,
110 unclassified: 30.0,
111 service: 20.0,
112 living_street: 10.0,
113 default: 30.0,
114 }
115 }
116}
117
118impl SpeedProfile {
119 pub fn speed_mps(&self, maxspeed: Option<&str>, highway: &str) -> f64 {
120 let kmh = maxspeed
121 .and_then(|v| self.parse_maxspeed(v))
122 .unwrap_or_else(|| self.highway_speed(highway));
123 kmh * 1000.0 / 3600.0
124 }
125
126 fn highway_speed(&self, highway: &str) -> f64 {
127 match highway {
128 "motorway" | "motorway_link" => self.motorway,
129 "trunk" | "trunk_link" => self.trunk,
130 "primary" | "primary_link" => self.primary,
131 "secondary" | "secondary_link" => self.secondary,
132 "tertiary" | "tertiary_link" => self.tertiary,
133 "residential" => self.residential,
134 "unclassified" => self.unclassified,
135 "service" => self.service,
136 "living_street" => self.living_street,
137 _ => self.default,
138 }
139 }
140
141 fn parse_maxspeed(&self, value: &str) -> Option<f64> {
142 let value = value.trim();
143
144 match value.to_lowercase().as_str() {
145 "walk" => return Some(5.0),
146 "none" | "unlimited" => return None,
147 _ => {}
148 }
149
150 let (num_str, is_mph) = if let Some(s) = value.strip_suffix("mph") {
151 (s.trim(), true)
152 } else if let Some(s) = value.strip_suffix("km/h") {
153 (s.trim(), false)
154 } else if let Some(s) = value.strip_suffix("kmh") {
155 (s.trim(), false)
156 } else {
157 (value, false)
158 };
159
160 let kmh: f64 = num_str.parse().ok()?;
161 if kmh <= 0.0 || kmh > 300.0 {
162 return None;
163 }
164
165 Some(if is_mph { kmh * 1.60934 } else { kmh })
166 }
167}
168
169#[derive(Debug, Clone)]
170pub struct NetworkConfig {
171 pub overpass_url: String,
172 pub overpass_endpoints: Vec<String>,
173 pub overpass_max_retries: usize,
174 pub overpass_retry_backoff: Duration,
175 pub cache_dir: PathBuf,
176 pub connect_timeout: Duration,
177 pub read_timeout: Duration,
178 pub speed_profile: SpeedProfile,
179 pub connectivity_policy: ConnectivityPolicy,
180 pub highway_types: Vec<&'static str>,
181}
182
183impl Default for NetworkConfig {
184 fn default() -> Self {
185 let default_overpass = "https://overpass-api.de/api/interpreter".to_string();
186 Self {
187 overpass_url: default_overpass.clone(),
188 overpass_endpoints: vec![default_overpass],
189 overpass_max_retries: 2,
190 overpass_retry_backoff: Duration::from_secs(2),
191 cache_dir: PathBuf::from(".osm_cache"),
192 connect_timeout: Duration::from_secs(30),
193 read_timeout: Duration::from_secs(180),
194 speed_profile: SpeedProfile::default(),
195 connectivity_policy: ConnectivityPolicy::KeepAll,
196 highway_types: vec![
198 "motorway",
199 "motorway_link",
200 "trunk",
201 "trunk_link",
202 "primary",
203 "primary_link",
204 "secondary",
205 "secondary_link",
206 "tertiary",
207 "tertiary_link",
208 "residential",
209 "unclassified",
210 "service",
211 "living_street",
212 ],
213 }
214 }
215}
216
217impl NetworkConfig {
218 pub fn new() -> Self {
219 Self::default()
220 }
221
222 pub fn overpass_url(mut self, url: impl Into<String>) -> Self {
223 let url = url.into();
224 self.overpass_url = url.clone();
225 self.overpass_endpoints = vec![url];
226 self
227 }
228
229 pub fn overpass_endpoints(mut self, urls: Vec<String>) -> Self {
230 if let Some(primary) = urls.first().cloned() {
231 self.overpass_url = primary;
232 self.overpass_endpoints = urls;
233 }
234 self
235 }
236
237 pub fn overpass_max_retries(mut self, retries: usize) -> Self {
238 self.overpass_max_retries = retries;
239 self
240 }
241
242 pub fn overpass_retry_backoff(mut self, backoff: Duration) -> Self {
243 self.overpass_retry_backoff = backoff;
244 self
245 }
246
247 pub fn cache_dir(mut self, path: impl Into<PathBuf>) -> Self {
248 self.cache_dir = path.into();
249 self
250 }
251
252 pub fn connect_timeout(mut self, timeout: Duration) -> Self {
253 self.connect_timeout = timeout;
254 self
255 }
256
257 pub fn read_timeout(mut self, timeout: Duration) -> Self {
258 self.read_timeout = timeout;
259 self
260 }
261
262 pub fn speed_profile(mut self, profile: SpeedProfile) -> Self {
263 self.speed_profile = profile;
264 self
265 }
266
267 pub fn connectivity_policy(mut self, policy: ConnectivityPolicy) -> Self {
268 self.connectivity_policy = policy;
269 self
270 }
271
272 pub fn highway_types(mut self, types: Vec<&'static str>) -> Self {
273 self.highway_types = types;
274 self
275 }
276
277 pub(crate) fn highway_regex(&self) -> String {
278 format!("^({})$", self.highway_types.join("|"))
279 }
280}