Skip to content

Commit 0da9fba

Browse files
committed
use petgraph directly
1 parent 05b8514 commit 0da9fba

File tree

4 files changed

+28
-172
lines changed

4 files changed

+28
-172
lines changed

Cargo.lock

Lines changed: 1 addition & 158 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ anyhow = "1.0.81"
1010
base64 = "0.22.0"
1111
clap = { version = "4.5.4", features = ["derive"] }
1212
jsonrpsee = { version = "0.22.3", features = ["http-client", "client"] }
13+
petgraph = "0.6.4"
1314
petgraph-graphml = "3.0.0"
1415
prettytable-rs = "0.10.0"
1516
rand = "0.8.5"
1617
rust-ini = "0.21.0"
17-
rustworkx-core = "0.14.2"
1818
serde = { version = "1.0.197", features = ["derive"] }
1919
serde_json = "1.0.115"
2020
tokio = { version = "1.36.0", features = ["rt-multi-thread"] }

src/rust-cli/graph.rs

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use anyhow::Context;
22
use clap::Subcommand;
3+
use petgraph::graph::{DiGraph, NodeIndex};
34
use petgraph_graphml::GraphMl;
4-
use rustworkx_core::generators::cycle_graph;
5-
use rustworkx_core::petgraph;
65
use std::borrow::Cow;
76
use std::fs::File;
87
use std::io::Cursor;
@@ -28,11 +27,27 @@ pub enum GraphCommand {
2827
},
2928
}
3029

31-
fn create_graph(number: usize) -> anyhow::Result<petgraph::graph::DiGraph<(), ()>> {
30+
fn create_graph(number: usize) -> anyhow::Result<DiGraph<(), ()>> {
3231
// 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+
}
3651

3752
// Add more outbound connections to each node
3853
for node in graph.node_indices() {
@@ -55,7 +70,6 @@ fn create_graph(number: usize) -> anyhow::Result<petgraph::graph::DiGraph<(), ()
5570
}
5671

5772
fn handle_bitcoin_conf(bitcoin_conf: Option<&Path>) -> String {
58-
// handle custom bitcoin.conf
5973
let mut conf_contents: String = String::new();
6074
if bitcoin_conf.is_some() {
6175
let conf = parse_bitcoin_conf(bitcoin_conf);
@@ -159,16 +173,15 @@ pub async fn handle_graph_command(command: &GraphCommand) -> anyhow::Result<()>
159173
let version_str: &str = version.as_deref().unwrap_or("26.0");
160174

161175
// 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")?;
164177

165-
// Parse any bitcoin conf arg
178+
// Parse any custom bitcoin conf
166179
let bitcoin_conf: String = handle_bitcoin_conf(bitcoin_conf.as_deref());
167180

168181
// Dump graph to graphml format
169182
let graphml_buf: Vec<u8> = convert_to_graphml(&graph).context("Convert to graphml")?;
170183

171-
// Graphml output settings
184+
// Configure graphml output settings
172185
let graphml_config = EmitterConfig {
173186
write_document_declaration: true,
174187
perform_indent: true,
@@ -177,7 +190,7 @@ pub async fn handle_graph_command(command: &GraphCommand) -> anyhow::Result<()>
177190
..Default::default() // Keep other defaults
178191
};
179192

180-
// Add custom elements
193+
// Add custom elements to graph
181194
let modified_graphml: xmltree::Element =
182195
add_custom_attributes(graphml_buf, version_str, bitcoin_conf.as_str());
183196

src/rust-cli/util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use anyhow::Context;
22
use ini::Ini;
33
use serde_json::Value;
4-
use std::path::{Path, PathBuf};
4+
use std::path::Path;
55

66
pub fn pretty_print_value(value: &Value) -> anyhow::Result<()> {
77
match value {

0 commit comments

Comments
 (0)