transit_grid/graphs/topology/mod.rs
1//! This module contains the `TopologyGraph` and the structures `TopoNode` and `TopoEdge` to represent the nodes and edges.
2//!
3//! `TopologyGraph` provides a way of maintaining the topology of a graph and mapping between `NodeId`s and `EdgeId`s
4//! (custom identifiers) and `NodeIndex` and `EdgeIndex` (indices in the petgraph).
5//!
6//! `TopoNode` and `TopoEdge` are used to represent nodes and edges within the `TopologyGraph`.
7mod repair;
8mod topology_graph;
9
10use petgraph::stable_graph::{EdgeIndex, NodeIndex};
11use std::fmt;
12
13pub use repair::TopologyGraphRepairer;
14pub use topology_graph::TopologyGraph;
15
16use crate::core::{EdgeId, NodeId};
17
18/// Represents a node in the `TopologyGraph`.
19///
20/// Each node is identified by a `NodeIndex` (which represents the node's position in the petgraph)
21/// and a `NodeId` (a custom identifier).
22///
23/// # Fields
24///
25/// * `id: NodeIndex` - The index of the node in the petgraph.
26/// * `node_id: NodeId` - The custom identifier of the node.
27#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
28pub struct TopoNode {
29 /// The index of the node in the petgraph.
30 pub id: NodeIndex,
31 /// The custom identifier of the node.
32 pub node_id: NodeId,
33}
34
35/// Represents an edge in the `TopologyGraph`.
36///
37/// Each edge is identified by an `EdgeIndex` (which represents the edge's position in the petgraph),
38/// a `from` and `to` `NodeId` (representing the nodes that the edge connects),
39/// and an `EdgeId` (a custom identifier).
40///
41/// # Fields
42///
43/// * `id: EdgeIndex` - The index of the edge in the petgraph.
44/// * `from: NodeId` - The custom identifier of the node where the edge originates.
45/// * `to: NodeId` - The custom identifier of the node where the edge ends.
46/// * `edge_id: EdgeId` - The custom identifier of the edge.
47#[derive(Debug, Clone, Eq, PartialEq)]
48pub struct TopoEdge {
49 /// The index of the edge in the petgraph.
50 pub id: EdgeIndex,
51 /// The custom identifier of the node where the edge originates.
52 pub from: NodeId,
53 /// The custom identifier of the node where the edge ends.
54 pub to: NodeId,
55 /// The custom identifier of the edge.
56 pub edge_id: EdgeId,
57}
58
59/// Formats the `TopoNode` for display purposes.
60impl fmt::Display for TopoNode {
61 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
62 write!(
63 f,
64 "TopoNode: {{ id: {:?}, node_id: {:?} }}",
65 self.id, self.node_id
66 )
67 }
68}
69
70/// Formats the `TopoEdge` for display purposes.
71impl fmt::Display for TopoEdge {
72 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
73 write!(
74 f,
75 "TopoEdge: {{ id: {:?}, from: {:?}, to: {:?}, edge_id: {:?} }}",
76 self.id, self.from, self.to, self.edge_id
77 )
78 }
79}