1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//! This module contains the `TopologyGraph` and the structures `TopoNode` and `TopoEdge` to represent the nodes and edges.
//!
//! `TopologyGraph` provides a way of maintaining the topology of a graph and mapping between `NodeId`s and `EdgeId`s
//! (custom identifiers) and `NodeIndex` and `EdgeIndex` (indices in the petgraph).
//!
//! `TopoNode` and `TopoEdge` are used to represent nodes and edges within the `TopologyGraph`.
mod repair;
mod topology_graph;

use petgraph::stable_graph::{EdgeIndex, NodeIndex};
use std::fmt;

pub use repair::TopologyGraphRepairer;
pub use topology_graph::TopologyGraph;

use crate::core::{EdgeId, NodeId};

/// Represents a node in the `TopologyGraph`.
///
/// Each node is identified by a `NodeIndex` (which represents the node's position in the petgraph)
/// and a `NodeId` (a custom identifier).
///
/// # Fields
///
/// * `id: NodeIndex` - The index of the node in the petgraph.
/// * `node_id: NodeId` - The custom identifier of the node.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct TopoNode {
    /// The index of the node in the petgraph.
    pub id: NodeIndex,
    /// The custom identifier of the node.
    pub node_id: NodeId,
}

/// Represents an edge in the `TopologyGraph`.
///
/// Each edge is identified by an `EdgeIndex` (which represents the edge's position in the petgraph),
/// a `from` and `to` `NodeId` (representing the nodes that the edge connects),
/// and an `EdgeId` (a custom identifier).
///
/// # Fields
///
/// * `id: EdgeIndex` - The index of the edge in the petgraph.
/// * `from: NodeId` - The custom identifier of the node where the edge originates.
/// * `to: NodeId` - The custom identifier of the node where the edge ends.
/// * `edge_id: EdgeId` - The custom identifier of the edge.
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct TopoEdge {
    /// The index of the edge in the petgraph.
    pub id: EdgeIndex,
    /// The custom identifier of the node where the edge originates.
    pub from: NodeId,
    /// The custom identifier of the node where the edge ends.
    pub to: NodeId,
    /// The custom identifier of the edge.
    pub edge_id: EdgeId,
}

/// Formats the `TopoNode` for display purposes.
impl fmt::Display for TopoNode {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "TopoNode: {{ id: {:?}, node_id: {:?} }}",
            self.id, self.node_id
        )
    }
}

/// Formats the `TopoEdge` for display purposes.
impl fmt::Display for TopoEdge {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "TopoEdge: {{ id: {:?}, from: {:?}, to: {:?}, edge_id: {:?} }}",
            self.id, self.from, self.to, self.edge_id
        )
    }
}