From ac660445084271b219f6b522e98ca898a33f8a88 Mon Sep 17 00:00:00 2001 From: Pihkaal Date: Sat, 23 Mar 2024 05:24:29 +0100 Subject: [PATCH] chore: remove returns because it's bad code style (i've read that on stack overflow) --- src/config.rs | 24 ++++++++++-------------- src/main.rs | 2 +- src/modes/chrono.rs | 6 +++--- src/modes/clock.rs | 4 ++-- src/modes/debug.rs | 5 +++-- src/modes/timer.rs | 6 +++--- src/rendering/color.rs | 21 ++++++++++----------- src/rendering/mod.rs | 7 ++++--- src/utils.rs | 2 +- 9 files changed, 37 insertions(+), 40 deletions(-) diff --git a/src/config.rs b/src/config.rs index 1eb2b26..167e22b 100644 --- a/src/config.rs +++ b/src/config.rs @@ -23,15 +23,13 @@ pub fn load_from_file(path: PathBuf, debug_mode: bool) -> Config { let mut ini = Ini::new(); ini.load(&path.to_str().unwrap()).unwrap(); - let config = Config { + Config { be_polite: ini.getbool("general", "polite").unwrap().unwrap(), fps: ini.getuint("general", "fps").unwrap().unwrap(), color: load_color(&ini, debug_mode), time_format: ini.get("format", "time").unwrap(), date_format: ini.get("format", "date").unwrap(), - }; - - return config; + } } pub fn write_default_config(path: PathBuf) -> () { @@ -47,19 +45,17 @@ fn load_color(ini: &Ini, debug_mode: bool) -> ComputableColor { match color_mode.as_str() { "term" => { let color = ini.getint("styling", "color_term").unwrap().unwrap(); - return ComputableColor::from(load_term_color(color)); + ComputableColor::from(load_term_color(color)) } "hex" => { let color = ini.get("styling", "color_hex").unwrap(); - return ComputableColor::from(load_hex_color(&color)); + ComputableColor::from(load_hex_color(&color)) } "ansi" => { let color = ini.getint("styling", "color_ansi").unwrap().unwrap(); - return ComputableColor::from(load_ansi_color(color)); - } - "gradient" => { - return load_gradient(ini, debug_mode); + ComputableColor::from(load_ansi_color(color)) } + "gradient" => load_gradient(ini, debug_mode), _ => panic!("ERROR: Invalid color mode: {}", color_mode), } } @@ -88,15 +84,15 @@ fn load_term_color(value: i64) -> Color { fn load_hex_color(value: &str) -> Color { let rgb = parse_hex_color(value); - return Color::Rgb { + Color::Rgb { r: rgb.0, g: rgb.1, b: rgb.2, - }; + } } fn load_ansi_color(value: i64) -> Color { - return Color::AnsiValue(value.try_into().unwrap()); + Color::AnsiValue(value.try_into().unwrap()) } fn load_gradient(ini: &Ini, debug_mode: bool) -> ComputableColor { @@ -131,5 +127,5 @@ fn load_gradient(ini: &Ini, debug_mode: bool) -> ComputableColor { .try_into() .unwrap() }; - return generate_gradient(keys, steps - 1); + generate_gradient(keys, steps - 1) } diff --git a/src/main.rs b/src/main.rs index 0517de5..258c2a4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -136,5 +136,5 @@ fn main() -> io::Result<()> { println!("CTRL-C pressed, bye!\n"); } - return Ok(()); + Ok(()) } diff --git a/src/modes/chrono.rs b/src/modes/chrono.rs index b240c25..31acb41 100644 --- a/src/modes/chrono.rs +++ b/src/modes/chrono.rs @@ -54,7 +54,7 @@ impl Chronometer { } fn is_paused(&self) -> bool { - return self.start_time.is_none(); + self.start_time.is_none() } fn elapsed(&self) -> Duration { @@ -146,7 +146,7 @@ pub fn main_loop(config: &mut Config) -> io::Result<()> { thread::sleep(Duration::from_millis(1000 / config.fps)); } - return Ok(()); + Ok(()) } fn render_frame( @@ -202,5 +202,5 @@ fn render_frame( rendering::draw_text(text, x, y, color)?; } - return Ok(()); + Ok(()) } diff --git a/src/modes/clock.rs b/src/modes/clock.rs index c0a9bde..999ca32 100644 --- a/src/modes/clock.rs +++ b/src/modes/clock.rs @@ -50,7 +50,7 @@ pub fn main_loop(config: &mut Config) -> io::Result<()> { thread::sleep(Duration::from_millis(1000 / config.fps)); } - return Ok(()); + Ok(()) } fn render_frame(config: &Config) -> io::Result<()> { @@ -73,5 +73,5 @@ fn render_frame(config: &Config) -> io::Result<()> { let y = height / 2 + symbols::SYMBOL_HEIGHT as i16 / 2 + 2; rendering::draw_text(&date, x, y - 1, color)?; - return Ok(()); + Ok(()) } diff --git a/src/modes/debug.rs b/src/modes/debug.rs index 98cce79..6900f1b 100644 --- a/src/modes/debug.rs +++ b/src/modes/debug.rs @@ -50,7 +50,8 @@ pub fn print_debug_infos(config: &mut Config) -> io::Result<()> { writeln!(stdout)?; queue!(stdout, style::ResetColor)?; let _ = stdout.flush(); - return Ok(()); + + Ok(()) } fn print_debug_label(key: &str) -> io::Result<()> { @@ -60,5 +61,5 @@ fn print_debug_label(key: &str) -> io::Result<()> { write!(stdout, "{}: ", key)?; queue!(stdout, style::ResetColor)?; - return Ok(()); + Ok(()) } diff --git a/src/modes/timer.rs b/src/modes/timer.rs index 48fbeb8..923ad0f 100644 --- a/src/modes/timer.rs +++ b/src/modes/timer.rs @@ -61,7 +61,7 @@ impl Timer { } fn is_paused(&self) -> bool { - return self.end_time.is_none(); + self.end_time.is_none() } fn reset(&mut self) { @@ -116,7 +116,7 @@ pub fn main_loop(config: &mut Config, duration: &str) -> io::Result<()> { thread::sleep(Duration::from_millis(1000 / config.fps)); } - return Ok(()); + Ok(()) } fn render_frame(config: &Config, timer: &Timer) -> io::Result<()> { @@ -146,5 +146,5 @@ fn render_frame(config: &Config, timer: &Timer) -> io::Result<()> { rendering::draw_text(text, x, y, color)?; } - return Ok(()); + Ok(()) } diff --git a/src/rendering/color.rs b/src/rendering/color.rs index a7d77c2..6c85486 100644 --- a/src/rendering/color.rs +++ b/src/rendering/color.rs @@ -7,10 +7,10 @@ pub struct ComputableColor { impl ComputableColor { pub fn from(color: Color) -> ComputableColor { - return ComputableColor { + ComputableColor { values: vec![color], current: 0, - }; + } } pub fn update(&mut self) -> () { @@ -18,27 +18,26 @@ impl ComputableColor { } pub fn get_value(&self) -> Color { - return *self.values.get(self.current).unwrap(); + *self.values.get(self.current).unwrap() } pub fn get_keys_count(&self) -> usize { - return self.values.len(); + self.values.len() } } fn clamp01(v: f32) -> f32 { - return if v < 0.0 { + if v < 0.0 { 0.0 } else if v > 1.0 { 1.0 } else { v - }; + } } fn lerp(a: u8, b: u8, t: f32) -> u8 { - let v = a as f32 + (b as f32 - a as f32) as f32 * clamp01(t); - return v as u8; + (a as f32 + (b as f32 - a as f32) as f32 * clamp01(t)) as u8 } pub fn generate_gradient(keys: Vec<(u8, u8, u8)>, steps: usize) -> ComputableColor { @@ -61,10 +60,10 @@ pub fn generate_gradient(keys: Vec<(u8, u8, u8)>, steps: usize) -> ComputableCol } } - return ComputableColor { + ComputableColor { values: gradient, current: 0, - }; + } } pub fn parse_hex_color(value: &str) -> (u8, u8, u8) { @@ -91,5 +90,5 @@ pub fn parse_hex_color(value: &str) -> (u8, u8, u8) { let g = u8::from_str_radix(&value[2..4], 16).unwrap(); let b = u8::from_str_radix(&value[4..6], 16).unwrap(); - return (r, g, b); + (r, g, b) } diff --git a/src/rendering/mod.rs b/src/rendering/mod.rs index 63eed8b..3916040 100644 --- a/src/rendering/mod.rs +++ b/src/rendering/mod.rs @@ -40,7 +40,7 @@ pub fn draw_time(time: &str, color: Color) -> io::Result<()> { } } - return Ok(()); + Ok(()) } pub fn draw_text(mut string: &str, mut x: i16, y: i16, color: Color) -> io::Result<()> { @@ -63,7 +63,8 @@ pub fn draw_text(mut string: &str, mut x: i16, y: i16, color: Color) -> io::Resu style::SetAttribute(Attribute::Bold) )?; write!(stdout, "{}", string)?; - return Ok(()); + + Ok(()) } fn draw_time_width(time: &str) -> i16 { @@ -113,5 +114,5 @@ fn draw_time_symbol(symbol: char, x: i16, y: i16, color: Color) -> io::Result<() } } - return Ok(()); + Ok(()) } diff --git a/src/utils.rs b/src/utils.rs index 6909eae..de6f054 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -6,5 +6,5 @@ pub fn format_duration(duration: time::Duration) -> String { let minutes = (seconds % 3600) / 60; let seconds = seconds % 60; - return format!("{:02}:{:02}:{:02}", hours, minutes, seconds); + format!("{:02}:{:02}:{:02}", hours, minutes, seconds) }