From 5660b34d5149dce27706ff6daa90b854e6f84e14 Mon Sep 17 00:00:00 2001 From: Jack Wills <32690432+mrjackwills@users.noreply.github.com> Date: Fri, 7 Oct 2022 02:22:26 +0000 Subject: [PATCH] refactor: map_or_else to map_or --- src/app_data/container_state.rs | 16 ++++++++-------- src/app_error.rs | 8 ++++---- src/docker_data/mod.rs | 11 ++++++----- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/app_data/container_state.rs b/src/app_data/container_state.rs index b1b83f5..c299de4 100644 --- a/src/app_data/container_state.rs +++ b/src/app_data/container_state.rs @@ -136,10 +136,10 @@ pub enum State { impl State { pub const fn get_color(self) -> Color { match self { - Self::Running => Color::Green, + Self::Paused => Color::Yellow, Self::Removing => Color::LightRed, Self::Restarting => Color::LightGreen, - Self::Paused => Color::Yellow, + Self::Running => Color::Green, _ => Color::Red, } } @@ -204,19 +204,19 @@ impl fmt::Display for State { #[derive(Debug, Clone, Copy)] pub enum DockerControls { Pause, - Unpause, Restart, - Stop, Start, + Stop, + Unpause, } impl DockerControls { pub const fn get_color(self) -> Color { match self { + Self::Pause => Color::Yellow, + Self::Restart => Color::Magenta, Self::Start => Color::Green, Self::Stop => Color::Red, - Self::Restart => Color::Magenta, - Self::Pause => Color::Yellow, Self::Unpause => Color::Blue, } } @@ -237,10 +237,10 @@ impl fmt::Display for DockerControls { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let disp = match self { Self::Pause => "pause", - Self::Unpause => "unpause", Self::Restart => "restart", - Self::Stop => "stop", Self::Start => "start", + Self::Stop => "stop", + Self::Unpause => "unpause", }; write!(f, "{}", disp) } diff --git a/src/app_error.rs b/src/app_error.rs index 385b227..8770daf 100644 --- a/src/app_error.rs +++ b/src/app_error.rs @@ -5,10 +5,10 @@ use std::fmt; #[allow(unused)] #[derive(Debug, Clone, Copy)] pub enum AppError { + DockerCommand(DockerControls), DockerConnect, DockerInterval, InputPoll, - DockerCommand(DockerControls), MouseCapture(bool), Terminal, } @@ -17,15 +17,15 @@ pub enum AppError { impl fmt::Display for AppError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { + Self::DockerCommand(s) => write!(f, "Unable to {} container", s), Self::DockerConnect => write!(f, "Unable to access docker daemon"), Self::DockerInterval => write!(f, "Docker update interval needs to be greater than 0"), Self::InputPoll => write!(f, "Unable to poll user input"), - Self::Terminal => write!(f, "Unable to draw to terminal"), - Self::DockerCommand(s) => write!(f, "Unable to {} container", s), Self::MouseCapture(x) => { - let reason = if *x { "en" } else { "dis" }; + let reason = if *x { "en" } else { "dis" }; write!(f, "Unbale to {}able mouse capture", reason) } + Self::Terminal => write!(f, "Unable to draw to terminal"), } } } diff --git a/src/docker_data/mod.rs b/src/docker_data/mod.rs index 92e661c..dac0021 100644 --- a/src/docker_data/mod.rs +++ b/src/docker_data/mod.rs @@ -80,7 +80,7 @@ impl DockerData { .cpu_usage .percpu_usage .as_ref() - .map_or_else(|| 0, std::vec::Vec::len) as u64 + .map_or(0, std::vec::Vec::len) as u64 }) as f64; if system_delta > 0.0 && cpu_delta > 0.0 { cpu_percentage = (cpu_delta / system_delta) * online_cpus * 100.0; @@ -122,10 +122,11 @@ impl DockerData { let cpu_stats = Self::calculate_usage(&stats); let (rx, tx) = if let Some(key) = op_key { - match stats.networks.unwrap_or_default().get(&key) { - Some(data) => (data.rx_bytes, data.tx_bytes), - None => (0, 0), - } + stats + .networks + .unwrap_or_default() + .get(&key) + .map_or((0, 0), |f| (f.rx_bytes, f.tx_bytes)) } else { (0, 0) };