Skip to content

Commit ccc778f

Browse files
committed
cleanup network fns
1 parent 36ced3e commit ccc778f

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

src/rust-cli/network.rs

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,37 @@ pub enum NetworkCommand {
2828
Export {},
2929
}
3030

31+
fn graph_file_to_b64(graph_file: &PathBuf) -> anyhow::Result<String> {
32+
let file_contents = std::fs::read(graph_file).context("Failed to read graph file")?;
33+
Ok(general_purpose::STANDARD.encode(file_contents))
34+
}
35+
36+
fn handle_network_status_response(data: serde_json::Value) {
37+
if let serde_json::Value::Array(items) = &data {
38+
for item in items {
39+
if let (Some(tank_index), Some(bitcoin_status)) = (
40+
item.get("tank_index").and_then(|v| v.as_i64()),
41+
item.get("bitcoin_status").and_then(|v| v.as_str()),
42+
) {
43+
println!("Tank: {:<6} Bitcoin: {}", tank_index, bitcoin_status);
44+
} else {
45+
println!("Error: Response item is missing expected fields");
46+
}
47+
}
48+
} else {
49+
println!("Error: Expected an array in the response");
50+
}
51+
}
52+
3153
pub async fn handle_network_command(
3254
command: &NetworkCommand,
3355
mut params: ObjectParams,
3456
) -> anyhow::Result<()> {
3557
let (request, params) = match command {
3658
NetworkCommand::Start { graph_file, force } => {
37-
let file_contents = std::fs::read(graph_file).context("Failed to read graph file")?;
38-
let graph_file_base64 = general_purpose::STANDARD.encode(file_contents);
59+
let b64_graph = graph_file_to_b64(graph_file).context("Read graph file")?;
3960
params
40-
.insert("graph_file", graph_file_base64)
61+
.insert("graph_file", b64_graph)
4162
.context("Add base64 graph file to params")?;
4263
params
4364
.insert("force", *force)
@@ -54,22 +75,7 @@ pub async fn handle_network_command(
5475

5576
let data = make_rpc_call(request, params).await?;
5677
match request {
57-
"network_status" => {
58-
if let serde_json::Value::Array(items) = &data {
59-
for item in items {
60-
if let (Some(tank_index), Some(bitcoin_status)) = (
61-
item.get("tank_index").and_then(|v| v.as_i64()),
62-
item.get("bitcoin_status").and_then(|v| v.as_str()),
63-
) {
64-
println!("Tank: {:<6} Bitcoin: {}", tank_index, bitcoin_status);
65-
} else {
66-
println!("Error: Response item is missing expected fields");
67-
}
68-
}
69-
} else {
70-
println!("Error: Expected an array in the response");
71-
}
72-
}
78+
"network_status" => handle_network_status_response(data),
7379
"network_start" => {
7480
todo!("Format this {:?}", data);
7581
}

0 commit comments

Comments
 (0)