disk: handle some [.unwrap()]s correctly

This commit is contained in:
hinto-janaiyo 2022-12-16 19:16:16 -05:00
parent ddec9fcb6d
commit 3ac0b98802
No known key found for this signature in database
GPG key ID: B1C5A64B80691E45
3 changed files with 43 additions and 11 deletions

View file

@ -347,10 +347,22 @@ impl Node {
let size = nodes.keys().len(); let size = nodes.keys().len();
let mut vec = Vec::with_capacity(size); let mut vec = Vec::with_capacity(size);
for (key, values) in nodes.iter() { 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 { let node = Node {
ip: values.get("ip").unwrap().as_str().unwrap().to_string(), ip,
rpc: values.get("rpc").unwrap().as_str().unwrap().to_string(), rpc,
zmq: values.get("zmq").unwrap().as_str().unwrap().to_string(), zmq,
}; };
vec.push((key.clone(), node)); vec.push((key.clone(), node));
} }
@ -459,10 +471,22 @@ impl Pool {
let size = pools.keys().len(); let size = pools.keys().len();
let mut vec = Vec::with_capacity(size); let mut vec = Vec::with_capacity(size);
for (key, values) in pools.iter() { 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 { let pool = Pool {
rig: values.get("rig").unwrap().as_str().unwrap().to_string(), rig,
ip: values.get("ip").unwrap().as_str().unwrap().to_string(), ip,
port: values.get("port").unwrap().as_str().unwrap().to_string(), port,
}; };
vec.push((key.clone(), pool)); vec.push((key.clone(), pool));
} }
@ -527,6 +551,7 @@ pub enum TomlError {
Deserialize(toml::de::Error), Deserialize(toml::de::Error),
Merge(figment::Error), Merge(figment::Error),
Format(std::fmt::Error), Format(std::fmt::Error),
Parse(&'static str),
} }
impl Display for TomlError { impl Display for TomlError {
@ -539,6 +564,7 @@ impl Display for TomlError {
Deserialize(err) => write!(f, "{}: Deserialize | {}", ERROR, err), Deserialize(err) => write!(f, "{}: Deserialize | {}", ERROR, err),
Merge(err) => write!(f, "{}: Merge | {}", ERROR, err), Merge(err) => write!(f, "{}: Merge | {}", ERROR, err),
Format(err) => write!(f, "{}: Format | {}", ERROR, err), Format(err) => write!(f, "{}: Format | {}", ERROR, err),
Parse(err) => write!(f, "{}: Parse | {}", ERROR, err),
} }
} }
} }

View file

@ -175,7 +175,10 @@ impl App {
.with_processes(sysinfo::ProcessRefreshKind::new().with_cpu()) .with_processes(sysinfo::ProcessRefreshKind::new().with_cpu())
.with_memory()); .with_memory());
sysinfo.refresh_all(); 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())); let pub_sys = Arc::new(Mutex::new(Sys::new()));
debug!("App Init | The rest of the [App]..."); 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), 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), 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), Merge(e) => app.error_state.set(format!("State file: {}", e), ErrorFerris::Error, ErrorButtons::ResetState),
_ => (),
}; };
State::new() State::new()
}, },
@ -291,8 +295,9 @@ impl App {
Path(e) => app.error_state.set(format!("Node list: {}", e), ErrorFerris::Panic, ErrorButtons::Quit), 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), 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), 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), 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() Node::new_vec()
}, },
@ -311,8 +316,9 @@ impl App {
Path(e) => app.error_state.set(format!("Pool list: {}", e), ErrorFerris::Panic, ErrorButtons::Quit), 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), 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), 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), 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() Pool::new_vec()
}, },

View file

@ -284,12 +284,12 @@ impl Ping {
.header("User-Agent", rand_user_agent) .header("User-Agent", rand_user_agent)
.body(hyper::Body::from(r#"{"jsonrpc":"2.0","id":"0","method":"get_info"}"#)) .body(hyper::Body::from(r#"{"jsonrpc":"2.0","id":"0","method":"get_info"}"#))
.unwrap(); .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); handles.push(handle);
} }
for handle in handles { for handle in handles {
handle.await?; handle.await;
} }
let node_vec = std::mem::take(&mut *node_vec.lock().unwrap()); let node_vec = std::mem::take(&mut *node_vec.lock().unwrap());