@@ -28,16 +28,37 @@ pub enum NetworkCommand {
28
28
Export { } ,
29
29
}
30
30
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
+
31
53
pub async fn handle_network_command (
32
54
command : & NetworkCommand ,
33
55
mut params : ObjectParams ,
34
56
) -> anyhow:: Result < ( ) > {
35
57
let ( request, params) = match command {
36
58
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" ) ?;
39
60
params
40
- . insert ( "graph_file" , graph_file_base64 )
61
+ . insert ( "graph_file" , b64_graph )
41
62
. context ( "Add base64 graph file to params" ) ?;
42
63
params
43
64
. insert ( "force" , * force)
@@ -54,22 +75,7 @@ pub async fn handle_network_command(
54
75
55
76
let data = make_rpc_call ( request, params) . await ?;
56
77
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) ,
73
79
"network_start" => {
74
80
todo ! ( "Format this {:?}" , data) ;
75
81
}
0 commit comments