From 35e4332e2f6a8553a70a339ce20d5474616e990b Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Sun, 10 Jul 2022 13:05:12 +0900 Subject: [PATCH] Refactorings --- src/main.rs | 18 ++++++++---------- src/proc.rs | 6 +++--- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/src/main.rs b/src/main.rs index ddf89dd..85f26d3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -94,14 +94,13 @@ fn main() { // meminfo { let meminfo_result = proc::get_meminfo(); - let meminfo_string: String; - if let Err(e) = meminfo_result { + let meminfo_string: String = if let Err(e) = meminfo_result { let mut stderr_handle = io::stderr().lock(); stderr_handle.write_all(format!("{}\n", e).as_bytes()).ok(); - meminfo_string = String::from("MEMINFO ERROR"); + String::from("MEMINFO ERROR") } else { - meminfo_string = meminfo_result.unwrap(); - } + meminfo_result.unwrap() + }; let meminfo_obj = SwaybarObject::from_string(meminfo_string); array.push_object(meminfo_obj); } @@ -109,14 +108,13 @@ fn main() { // loadavg { let loadavg_result = proc::get_loadavg(); - let loadavg_string: String; - if let Err(e) = loadavg_result { + let loadavg_string: String = if let Err(e) = loadavg_result { let mut stderr_handle = io::stderr().lock(); stderr_handle.write_all(format!("{}\n", e).as_bytes()).ok(); - loadavg_string = String::from("LOADAVG ERROR"); + String::from("LOADAVG ERROR") } else { - loadavg_string = loadavg_result.unwrap(); - } + loadavg_result.unwrap() + }; let loadavg_obj = SwaybarObject::from_string(loadavg_string); array.push_object(loadavg_obj); } diff --git a/src/proc.rs b/src/proc.rs index a867c6e..68dc1b8 100644 --- a/src/proc.rs +++ b/src/proc.rs @@ -52,7 +52,7 @@ impl std::error::Error for Error { Error::IO(e) => e.source(), Error::ParseInt(e) => e.source(), Error::Format(e) => e.source(), - _ => None, + Error::Generic(_) => None, } } } @@ -83,10 +83,10 @@ impl NetInfo { netdev_file.read_to_string(&mut netdev_string)?; } - let mut dev_line: Option = None; + let mut dev_line: Option<&str> = None; for line in netdev_string.lines() { if line.starts_with(&self.dev_name) { - dev_line = Some(line.to_owned()); + dev_line = Some(line); break; } }