Skip to main content

solverforge_maps/routing/
progress.rs

1//! Progress events for routing operations.
2
3use serde::Serialize;
4
5#[derive(Debug, Clone, Serialize)]
6#[serde(tag = "phase", rename_all = "snake_case")]
7pub enum RoutingProgress {
8    CheckingCache {
9        percent: u8,
10    },
11    DownloadingNetwork {
12        percent: u8,
13        bytes: usize,
14    },
15    ParsingOsm {
16        percent: u8,
17        nodes: usize,
18        edges: usize,
19    },
20    BuildingGraph {
21        percent: u8,
22    },
23    ComputingMatrix {
24        percent: u8,
25        row: usize,
26        total: usize,
27    },
28    ComputingGeometries {
29        percent: u8,
30        pair: usize,
31        total: usize,
32    },
33    Complete,
34}
35
36impl RoutingProgress {
37    pub fn percent(&self) -> u8 {
38        match self {
39            Self::CheckingCache { percent } => *percent,
40            Self::DownloadingNetwork { percent, .. } => *percent,
41            Self::ParsingOsm { percent, .. } => *percent,
42            Self::BuildingGraph { percent } => *percent,
43            Self::ComputingMatrix { percent, .. } => *percent,
44            Self::ComputingGeometries { percent, .. } => *percent,
45            Self::Complete => 100,
46        }
47    }
48
49    pub fn phase_message(&self) -> (&'static str, &'static str) {
50        match self {
51            Self::CheckingCache { .. } => ("cache", "Checking cache..."),
52            Self::DownloadingNetwork { .. } => ("network", "Downloading road network..."),
53            Self::ParsingOsm { .. } => ("parsing", "Parsing OSM data..."),
54            Self::BuildingGraph { .. } => ("building", "Building routing graph..."),
55            Self::ComputingMatrix { .. } => ("matrix", "Computing travel times..."),
56            Self::ComputingGeometries { .. } => ("geometry", "Computing route geometries..."),
57            Self::Complete => ("complete", "Ready!"),
58        }
59    }
60
61    pub fn detail(&self) -> Option<String> {
62        match self {
63            Self::DownloadingNetwork { bytes, .. } if *bytes > 0 => {
64                Some(format!("{} KB downloaded", bytes / 1024))
65            }
66            Self::ParsingOsm { nodes, edges, .. } => {
67                Some(format!("{} nodes, {} edges", nodes, edges))
68            }
69            Self::ComputingMatrix { row, total, .. } => Some(format!("Row {}/{}", row, total)),
70            Self::ComputingGeometries { pair, total, .. } => {
71                Some(format!("Pair {}/{}", pair, total))
72            }
73            _ => None,
74        }
75    }
76}