1
1
use anyhow:: Context ;
2
2
use clap:: Subcommand ;
3
+ use petgraph:: graph:: { DiGraph , NodeIndex } ;
3
4
use petgraph_graphml:: GraphMl ;
4
- use rustworkx_core:: generators:: cycle_graph;
5
- use rustworkx_core:: petgraph;
6
5
use std:: borrow:: Cow ;
7
6
use std:: fs:: File ;
8
7
use std:: io:: Cursor ;
@@ -28,11 +27,27 @@ pub enum GraphCommand {
28
27
} ,
29
28
}
30
29
31
- fn create_graph ( number : usize ) -> anyhow:: Result < petgraph :: graph :: DiGraph < ( ) , ( ) > > {
30
+ fn create_graph ( number : usize ) -> anyhow:: Result < DiGraph < ( ) , ( ) > > {
32
31
// Create initial cycle graph
33
- let mut graph: petgraph:: graph:: DiGraph < ( ) , ( ) > =
34
- cycle_graph ( Some ( number) , None , || { } , || { } , false )
35
- . context ( "Create initial cycle graph" ) ?;
32
+ let mut graph = DiGraph :: new ( ) ;
33
+ let mut last_node: Option < NodeIndex > = None ;
34
+ let mut first_node: Option < NodeIndex > = None ;
35
+
36
+ for _ in 0 ..number {
37
+ let new_node = graph. add_node ( ( ) ) ;
38
+ if let Some ( ln) = last_node {
39
+ graph. add_edge ( ln, new_node, ( ) ) ; // Add an edge from the last node to the new one
40
+ } else {
41
+ first_node = Some ( new_node) ;
42
+ }
43
+ last_node = Some ( new_node) ;
44
+ }
45
+
46
+ if number > 0 {
47
+ if let ( Some ( first) , Some ( last) ) = ( first_node, last_node) {
48
+ graph. add_edge ( last, first, ( ) ) ; // Connect the last node to the first to complete the cycle
49
+ }
50
+ }
36
51
37
52
// Add more outbound connections to each node
38
53
for node in graph. node_indices ( ) {
@@ -55,7 +70,6 @@ fn create_graph(number: usize) -> anyhow::Result<petgraph::graph::DiGraph<(), ()
55
70
}
56
71
57
72
fn handle_bitcoin_conf ( bitcoin_conf : Option < & Path > ) -> String {
58
- // handle custom bitcoin.conf
59
73
let mut conf_contents: String = String :: new ( ) ;
60
74
if bitcoin_conf. is_some ( ) {
61
75
let conf = parse_bitcoin_conf ( bitcoin_conf) ;
@@ -159,16 +173,15 @@ pub async fn handle_graph_command(command: &GraphCommand) -> anyhow::Result<()>
159
173
let version_str: & str = version. as_deref ( ) . unwrap_or ( "26.0" ) ;
160
174
161
175
// Create empty graph
162
- let graph: petgraph:: graph:: DiGraph < ( ) , ( ) > =
163
- create_graph ( * number) . context ( "creating graph" ) ?;
176
+ let graph: DiGraph < ( ) , ( ) > = create_graph ( * number) . context ( "creating graph" ) ?;
164
177
165
- // Parse any bitcoin conf arg
178
+ // Parse any custom bitcoin conf
166
179
let bitcoin_conf: String = handle_bitcoin_conf ( bitcoin_conf. as_deref ( ) ) ;
167
180
168
181
// Dump graph to graphml format
169
182
let graphml_buf: Vec < u8 > = convert_to_graphml ( & graph) . context ( "Convert to graphml" ) ?;
170
183
171
- // Graphml output settings
184
+ // Configure graphml output settings
172
185
let graphml_config = EmitterConfig {
173
186
write_document_declaration : true ,
174
187
perform_indent : true ,
@@ -177,7 +190,7 @@ pub async fn handle_graph_command(command: &GraphCommand) -> anyhow::Result<()>
177
190
..Default :: default ( ) // Keep other defaults
178
191
} ;
179
192
180
- // Add custom elements
193
+ // Add custom elements to graph
181
194
let modified_graphml: xmltree:: Element =
182
195
add_custom_attributes ( graphml_buf, version_str, bitcoin_conf. as_str ( ) ) ;
183
196
0 commit comments