From 3ac0b98802c2cd4e2ab0d87416681e6e5614dc54 Mon Sep 17 00:00:00 2001 From: hinto-janaiyo Date: Fri, 16 Dec 2022 19:16:16 -0500 Subject: [PATCH] disk: handle some [.unwrap()]s correctly --- src/disk.rs | 38 ++++++++++++++++++++++++++++++++------ src/main.rs | 12 +++++++++--- src/node.rs | 4 ++-- 3 files changed, 43 insertions(+), 11 deletions(-) diff --git a/src/disk.rs b/src/disk.rs index 7f3ba4f..f94cc0b 100644 --- a/src/disk.rs +++ b/src/disk.rs @@ -347,10 +347,22 @@ impl Node { let size = nodes.keys().len(); let mut vec = Vec::with_capacity(size); for (key, values) in nodes.iter() { + let ip = match values.get("ip") { + Some(ip) => ip.to_string(), + None => { error!("Node | [None] at [ip] parse"); return Err(TomlError::Parse("[None] at [ip] parse")) }, + }; + let rpc = match values.get("rpc") { + Some(rpc) => rpc.to_string(), + None => { error!("Node | [None] at [rpc] parse"); return Err(TomlError::Parse("[None] at [rpc] parse")) }, + }; + let zmq = match values.get("zmq") { + Some(zmq) => zmq.to_string(), + None => { error!("Node | [None] at [zmq] parse"); return Err(TomlError::Parse("[None] at [zmq] parse")) }, + }; let node = Node { - ip: values.get("ip").unwrap().as_str().unwrap().to_string(), - rpc: values.get("rpc").unwrap().as_str().unwrap().to_string(), - zmq: values.get("zmq").unwrap().as_str().unwrap().to_string(), + ip, + rpc, + zmq, }; vec.push((key.clone(), node)); } @@ -459,10 +471,22 @@ impl Pool { let size = pools.keys().len(); let mut vec = Vec::with_capacity(size); for (key, values) in pools.iter() { + let rig = match values.get("rig") { + Some(rig) => rig.to_string(), + None => { error!("Pool | [None] at [rig] parse"); return Err(TomlError::Parse("[None] at [rig] parse")) }, + }; + let ip = match values.get("ip") { + Some(ip) => ip.to_string(), + None => { error!("Pool | [None] at [ip] parse"); return Err(TomlError::Parse("[None] at [ip] parse")) }, + }; + let port = match values.get("port") { + Some(port) => port.to_string(), + None => { error!("Pool | [None] at [port] parse"); return Err(TomlError::Parse("[None] at [port] parse")) }, + }; let pool = Pool { - rig: values.get("rig").unwrap().as_str().unwrap().to_string(), - ip: values.get("ip").unwrap().as_str().unwrap().to_string(), - port: values.get("port").unwrap().as_str().unwrap().to_string(), + rig, + ip, + port, }; vec.push((key.clone(), pool)); } @@ -527,6 +551,7 @@ pub enum TomlError { Deserialize(toml::de::Error), Merge(figment::Error), Format(std::fmt::Error), + Parse(&'static str), } impl Display for TomlError { @@ -539,6 +564,7 @@ impl Display for TomlError { Deserialize(err) => write!(f, "{}: Deserialize | {}", ERROR, err), Merge(err) => write!(f, "{}: Merge | {}", ERROR, err), Format(err) => write!(f, "{}: Format | {}", ERROR, err), + Parse(err) => write!(f, "{}: Parse | {}", ERROR, err), } } } diff --git a/src/main.rs b/src/main.rs index 428913f..a99a97c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -175,7 +175,10 @@ impl App { .with_processes(sysinfo::ProcessRefreshKind::new().with_cpu()) .with_memory()); sysinfo.refresh_all(); - let pid = sysinfo::get_current_pid().unwrap(); + let pid = match sysinfo::get_current_pid() { + Ok(pid) => pid, + Err(e) => { error!("App Init | Failed to get sysinfo PID: {}", e); exit(1) } + }; let pub_sys = Arc::new(Mutex::new(Sys::new())); debug!("App Init | The rest of the [App]..."); @@ -275,6 +278,7 @@ impl App { Deserialize(e) => app.error_state.set(format!("State file: {}", e), ErrorFerris::Panic, ErrorButtons::Quit), Format(e) => app.error_state.set(format!("State file: {}", e), ErrorFerris::Panic, ErrorButtons::Quit), Merge(e) => app.error_state.set(format!("State file: {}", e), ErrorFerris::Error, ErrorButtons::ResetState), + _ => (), }; State::new() }, @@ -291,8 +295,9 @@ impl App { Path(e) => app.error_state.set(format!("Node list: {}", e), ErrorFerris::Panic, ErrorButtons::Quit), Serialize(e) => app.error_state.set(format!("Node list: {}", e), ErrorFerris::Panic, ErrorButtons::Quit), Deserialize(e) => app.error_state.set(format!("Node list: {}", e), ErrorFerris::Panic, ErrorButtons::Quit), - Format(e) => app.error_state.set(format!("Node file: {}", e), ErrorFerris::Panic, ErrorButtons::Quit), + Format(e) => app.error_state.set(format!("Node list: {}", e), ErrorFerris::Panic, ErrorButtons::Quit), Merge(e) => app.error_state.set(format!("Node list: {}", e), ErrorFerris::Error, ErrorButtons::ResetState), + Parse(e) => app.error_state.set(format!("Node list: {}", e), ErrorFerris::Panic, ErrorButtons::Quit), }; Node::new_vec() }, @@ -311,8 +316,9 @@ impl App { Path(e) => app.error_state.set(format!("Pool list: {}", e), ErrorFerris::Panic, ErrorButtons::Quit), Serialize(e) => app.error_state.set(format!("Pool list: {}", e), ErrorFerris::Panic, ErrorButtons::Quit), Deserialize(e) => app.error_state.set(format!("Pool list: {}", e), ErrorFerris::Panic, ErrorButtons::Quit), - Format(e) => app.error_state.set(format!("Pool file: {}", e), ErrorFerris::Panic, ErrorButtons::Quit), + Format(e) => app.error_state.set(format!("Pool list: {}", e), ErrorFerris::Panic, ErrorButtons::Quit), Merge(e) => app.error_state.set(format!("Pool list: {}", e), ErrorFerris::Error, ErrorButtons::ResetState), + Parse(e) => app.error_state.set(format!("Pool list: {}", e), ErrorFerris::Panic, ErrorButtons::Quit), }; Pool::new_vec() }, diff --git a/src/node.rs b/src/node.rs index 6713e92..cda079d 100644 --- a/src/node.rs +++ b/src/node.rs @@ -284,12 +284,12 @@ impl Ping { .header("User-Agent", rand_user_agent) .body(hyper::Body::from(r#"{"jsonrpc":"2.0","id":"0","method":"get_info"}"#)) .unwrap(); - let handle = tokio::spawn(async move { Self::response(client, request, ip, ping, percent, node_vec).await }); + let handle = tokio::task::spawn(async move { Self::response(client, request, ip, ping, percent, node_vec).await; }); handles.push(handle); } for handle in handles { - handle.await?; + handle.await; } let node_vec = std::mem::take(&mut *node_vec.lock().unwrap());