refactor: map_or_else to map_or

This commit is contained in:
Jack Wills
2022-10-07 02:22:26 +00:00
parent 3e26f292c7
commit 5660b34d51
3 changed files with 18 additions and 17 deletions
+8 -8
View File
@@ -136,10 +136,10 @@ pub enum State {
impl State { impl State {
pub const fn get_color(self) -> Color { pub const fn get_color(self) -> Color {
match self { match self {
Self::Running => Color::Green, Self::Paused => Color::Yellow,
Self::Removing => Color::LightRed, Self::Removing => Color::LightRed,
Self::Restarting => Color::LightGreen, Self::Restarting => Color::LightGreen,
Self::Paused => Color::Yellow, Self::Running => Color::Green,
_ => Color::Red, _ => Color::Red,
} }
} }
@@ -204,19 +204,19 @@ impl fmt::Display for State {
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub enum DockerControls { pub enum DockerControls {
Pause, Pause,
Unpause,
Restart, Restart,
Stop,
Start, Start,
Stop,
Unpause,
} }
impl DockerControls { impl DockerControls {
pub const fn get_color(self) -> Color { pub const fn get_color(self) -> Color {
match self { match self {
Self::Pause => Color::Yellow,
Self::Restart => Color::Magenta,
Self::Start => Color::Green, Self::Start => Color::Green,
Self::Stop => Color::Red, Self::Stop => Color::Red,
Self::Restart => Color::Magenta,
Self::Pause => Color::Yellow,
Self::Unpause => Color::Blue, Self::Unpause => Color::Blue,
} }
} }
@@ -237,10 +237,10 @@ impl fmt::Display for DockerControls {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let disp = match self { let disp = match self {
Self::Pause => "pause", Self::Pause => "pause",
Self::Unpause => "unpause",
Self::Restart => "restart", Self::Restart => "restart",
Self::Stop => "stop",
Self::Start => "start", Self::Start => "start",
Self::Stop => "stop",
Self::Unpause => "unpause",
}; };
write!(f, "{}", disp) write!(f, "{}", disp)
} }
+3 -3
View File
@@ -5,10 +5,10 @@ use std::fmt;
#[allow(unused)] #[allow(unused)]
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub enum AppError { pub enum AppError {
DockerCommand(DockerControls),
DockerConnect, DockerConnect,
DockerInterval, DockerInterval,
InputPoll, InputPoll,
DockerCommand(DockerControls),
MouseCapture(bool), MouseCapture(bool),
Terminal, Terminal,
} }
@@ -17,15 +17,15 @@ pub enum AppError {
impl fmt::Display for AppError { impl fmt::Display for AppError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self { match self {
Self::DockerCommand(s) => write!(f, "Unable to {} container", s),
Self::DockerConnect => write!(f, "Unable to access docker daemon"), Self::DockerConnect => write!(f, "Unable to access docker daemon"),
Self::DockerInterval => write!(f, "Docker update interval needs to be greater than 0"), Self::DockerInterval => write!(f, "Docker update interval needs to be greater than 0"),
Self::InputPoll => write!(f, "Unable to poll user input"), 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) => { 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) write!(f, "Unbale to {}able mouse capture", reason)
} }
Self::Terminal => write!(f, "Unable to draw to terminal"),
} }
} }
} }
+6 -5
View File
@@ -80,7 +80,7 @@ impl DockerData {
.cpu_usage .cpu_usage
.percpu_usage .percpu_usage
.as_ref() .as_ref()
.map_or_else(|| 0, std::vec::Vec::len) as u64 .map_or(0, std::vec::Vec::len) as u64
}) as f64; }) as f64;
if system_delta > 0.0 && cpu_delta > 0.0 { if system_delta > 0.0 && cpu_delta > 0.0 {
cpu_percentage = (cpu_delta / system_delta) * online_cpus * 100.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 cpu_stats = Self::calculate_usage(&stats);
let (rx, tx) = if let Some(key) = op_key { let (rx, tx) = if let Some(key) = op_key {
match stats.networks.unwrap_or_default().get(&key) { stats
Some(data) => (data.rx_bytes, data.tx_bytes), .networks
None => (0, 0), .unwrap_or_default()
} .get(&key)
.map_or((0, 0), |f| (f.rx_bytes, f.tx_bytes))
} else { } else {
(0, 0) (0, 0)
}; };