Skip to content

Commit

Permalink
🎨 address a bunch of pedantic clippy lints (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
danieleades authored Apr 9, 2023
1 parent 6822751 commit a02502a
Show file tree
Hide file tree
Showing 22 changed files with 155 additions and 128 deletions.
26 changes: 21 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
#![deny(
clippy::semicolon_if_nothing_returned,
clippy::wildcard_imports,
clippy::cloned_instead_of_copied,
clippy::single_match_else,
clippy::match_wildcard_for_single_variants,
clippy::match_bool,
clippy::if_not_else,
clippy::unused_async,
clippy::uninlined_format_args,
clippy::implicit_clone,
clippy::inconsistent_struct_constructor,
clippy::use_self
)]

mod modules;

use anyhow::Result;
Expand All @@ -13,18 +28,19 @@ async fn main() -> Result<()> {
let config = Config::get();
let params = Params::merge(&config, &args).await?;

run(&params).await?.render(&params).await?;
params.handle_next(args, config).await?;
run(&params).await?.render(&params)?;
params.handle_next(args, config)?;

Ok(())
}

pub async fn run(params: &Params) -> Result<Product> {
let loc = Location::get(&params.config.address, &params.config.language).await?;
let weather = Weather::get(loc.lat, loc.lon, &params.config.units).await?;
let historical_weather = match !params.historical_weather.is_empty() {
true => Some(Weather::get_dates(&params.historical_weather, loc.lat, loc.lon, &params.config.units).await?),
_ => None,
let historical_weather = if params.historical_weather.is_empty() {
None
} else {
Some(Weather::get_dates(&params.historical_weather, loc.lat, loc.lon, &params.config.units).await?)
};

Ok(Product {
Expand Down
2 changes: 1 addition & 1 deletion src/modules/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl<'a> ApiQuery<'a> {
}
}

pub fn location(api: ApiName, address: &'a str, language: &'a str) -> Self {
pub const fn location(api: ApiName, address: &'a str, language: &'a str) -> Self {
Self { api, address, language }
}

Expand Down
7 changes: 4 additions & 3 deletions src/modules/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@ pub enum UnitArg {
}

fn parse_language_code(s: &str) -> Result<String> {
match s.len() < 2 {
true => Err(anyhow!("\n The language code must be at least two characters long.")),
_ => Ok(s.to_string()),
if s.len() < 2 {
Err(anyhow!("\n The language code must be at least two characters long."))
} else {
Ok(s.to_string())
}
}
2 changes: 1 addition & 1 deletion src/modules/config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::Result;
use colored::{Color::Yellow, Colorize};
use directories::ProjectDirs;
use optional_struct::*;
use optional_struct::{optional_struct, Applyable};
use ron::{
extensions::Extensions,
ser::{to_string_pretty, PrettyConfig},
Expand Down
22 changes: 11 additions & 11 deletions src/modules/display/border.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,41 @@ pub enum Border {
}

impl Border {
pub fn fmt<'a>(&self, style: &BorderStyle) -> &'a str {
pub const fn fmt<'a>(&self, style: &BorderStyle) -> &'a str {
match self {
Border::TL => match style {
Self::TL => match style {
BorderStyle::single => "┌",
BorderStyle::solid => "┏",
BorderStyle::double => "╔",
_ => "╭",
BorderStyle::rounded => "╭",
},
Border::T | Border::B => match style {
Self::T | Self::B => match style {
BorderStyle::double => "═",
BorderStyle::solid => "━",
_ => "─",
},
Border::TR => match style {
Self::TR => match style {
BorderStyle::single => "┐",
BorderStyle::solid => "┓",
BorderStyle::double => "╗",
_ => "╮",
BorderStyle::rounded => "╮",
},
Border::R | Border::L => match style {
Self::R | Self::L => match style {
BorderStyle::double => "║",
BorderStyle::solid => "┃",
_ => "│",
},
Border::BR => match style {
Self::BR => match style {
BorderStyle::single => "┘",
BorderStyle::solid => "┛",
BorderStyle::double => "╝",
_ => "╯",
BorderStyle::rounded => "╯",
},
Border::BL => match style {
Self::BL => match style {
BorderStyle::single => "└",
BorderStyle::solid => "┗",
BorderStyle::double => "╚",
_ => "╰",
BorderStyle::rounded => "╰",
},
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/modules/display/current.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use colored::{Color::BrightBlack, Colorize};
use crate::modules::{params::Params, units::Time};

use super::{
border::*,
border::{Border, BorderStyle, Edge, Separator},
gui_config::ColorOption,
hourly::HourlyForecast,
product::{Product, MIN_CELL_WIDTH, MIN_WIDTH},
Expand Down Expand Up @@ -35,7 +35,7 @@ pub struct Dimensions {

impl Current {
pub fn render(self, params: &Params) -> Dimensions {
let Current {
let Self {
address,
temperature,
apparent_temperature,
Expand Down Expand Up @@ -163,11 +163,11 @@ impl Current {
);
let sunrise = match params.config.units.time {
Time::am_pm => format!("{}:{}am", sunrise_hour, &weather.daily.sunrise[0][14..16]),
_ => weather.daily.sunrise[0][11..16].to_string(),
Time::military => weather.daily.sunrise[0][11..16].to_string(),
};
let sunset = match params.config.units.time {
Time::am_pm => format!("{}:{}pm", sunset_hour - 12, &weather.daily.sunset[0][14..16]),
_ => weather.daily.sunset[0][11..16].to_string(),
Time::military => weather.daily.sunset[0][11..16].to_string(),
};
let night = current_hour < sunrise_hour || current_hour > sunset_hour;

Expand Down Expand Up @@ -231,7 +231,7 @@ impl Current {
None
};

Ok(Current {
Ok(Self {
address,
temperature,
apparent_temperature,
Expand Down
22 changes: 13 additions & 9 deletions src/modules/display/day.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ use colored::{Color::BrightBlack, Colorize};
use crate::modules::{display::hourly::WIDTH, localization::Locales, params::Params, units::Time};

use super::{
border::*, gui_config::ColorOption, hourly::HourlyForecast, product::Product, utils::lang_len_diff,
border::{Border, BorderStyle, Edge, Separator},
gui_config::ColorOption,
hourly::HourlyForecast,
product::Product,
utils::lang_len_diff,
weathercode::WeatherCode,
};

Expand All @@ -23,7 +27,7 @@ pub struct Day {

impl Day {
pub fn render(self, params: &Params) {
let Day {
let Self {
address,
temp_max_min,
apparent_temp_max_min,
Expand Down Expand Up @@ -78,7 +82,7 @@ impl Day {
);

// Apparent Temperature & Sun Rise & Sun Set
let sunrise_and_sunset = format!("{} {}", sunrise, sunset);
let sunrise_and_sunset = format!("{sunrise} {sunset}");
println!(
"{} {} {: >WIDTH$} {}",
Border::L.fmt(&gui.border).color_option(BrightBlack, &gui.color),
Expand Down Expand Up @@ -109,11 +113,11 @@ impl Day {
);
let sunrise = match params.config.units.time {
Time::am_pm => format!("{}:{}am", sunrise_hour, &weather.daily.sunrise[day_index][14..16]),
_ => weather.daily.sunrise[day_index][11..16].to_string(),
Time::military => weather.daily.sunrise[day_index][11..16].to_string(),
};
let sunset = match params.config.units.time {
Time::am_pm => format!("{}:{}pm", sunset_hour - 12, &weather.daily.sunset[day_index][14..16]),
_ => weather.daily.sunset[day_index][11..16].to_string(),
Time::military => weather.daily.sunset[day_index][11..16].to_string(),
};
let night = current_hour < sunrise_hour || current_hour > sunset_hour;

Expand All @@ -136,10 +140,10 @@ impl Day {
let lang = &params.config.language;
let date = format!(
" {}",
if !(lang == "en_US" || lang == "en") {
Locales::localize_date(dt.into(), lang)?
} else {
if lang == "en_US" || lang == "en" {
dt.format("%a, %e %b").to_string()
} else {
Locales::localize_date(dt.into(), lang)?
}
);
let sunrise = format!(" {sunrise}");
Expand All @@ -148,7 +152,7 @@ impl Day {
WeatherCode::resolve(weather.daily.weathercode[day_index], night, &params.texts.weather.weather_code)?;
let hourly_forecast = HourlyForecast::prepare(product, params, day_index)?;

Ok(Day {
Ok(Self {
address,
temp_max_min,
apparent_temp_max_min,
Expand Down
20 changes: 10 additions & 10 deletions src/modules/display/graph.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use optional_struct::*;
use optional_struct::{optional_struct, Applyable};
use serde::{Deserialize, Serialize};
use std::cmp::Ordering;

Expand Down Expand Up @@ -62,8 +62,8 @@ struct GraphLvls {
}

impl Graph {
pub fn prepare_graph(temperatures: &[f32], graph_opts: &GraphOpts) -> Graph {
let mut graph = Graph(String::new(), String::new());
pub fn prepare_graph(temperatures: &[f32], graph_opts: &GraphOpts) -> Self {
let mut graph = Self(String::new(), String::new());
let style = graph_opts.style;
let rowspan = graph_opts.rowspan;

Expand Down Expand Up @@ -92,16 +92,16 @@ impl Graph {
match Some(last_lvl.cmp(&graph_lvls.current)) {
Some(o) if o == Ordering::Less => graph.0.push(graph_lvls.glyphs[graph_lvls.get_idx_single(o)]),
Some(o) if o == Ordering::Equal => {
graph.0.push(graph_lvls.glyphs[graph_lvls.get_idx_single(o)])
graph.0.push(graph_lvls.glyphs[graph_lvls.get_idx_single(o)]);
}
Some(o) if o == Ordering::Greater => {
graph.0.push(graph_lvls.glyphs[graph_lvls.get_idx_single(o)])
graph.0.push(graph_lvls.glyphs[graph_lvls.get_idx_single(o)]);
}
_ => {}
}
} else {
// first iteration - without a last_lvl
graph.0.push(graph_lvls.glyphs[graph_lvls.get_idx_single(Ordering::Equal)])
graph.0.push(graph_lvls.glyphs[graph_lvls.get_idx_single(Ordering::Equal)]);
}

// char 2/3
Expand Down Expand Up @@ -284,7 +284,7 @@ impl GraphLvls {
};

if graph_rows == &GraphRows::double {
glyphs.append(&mut glyphs.to_vec())
glyphs.append(&mut glyphs.clone());
}

glyphs
Expand Down Expand Up @@ -337,10 +337,10 @@ impl GraphLvls {
}
Ordering::Equal => {
if self.next > self.current + 1 && self.current < self.glyphs.len() {
if self.current != graph_one_max_idx {
self.current + 1
} else {
if self.current == graph_one_max_idx {
self.current
} else {
self.current + 1
}
// this additional clause should further improve details, but small deviations make the graph look a bit scattered
/* } else if self.next < self.current && self.current > 0 {
Expand Down
6 changes: 3 additions & 3 deletions src/modules/display/gui_config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use colored::{Color, ColoredString, Colorize};
use optional_struct::*;
use optional_struct::{optional_struct, Applyable};
use serde::{Deserialize, Serialize};

use crate::modules::display::{
Expand Down Expand Up @@ -43,8 +43,8 @@ pub trait ColorOption {
impl<'a> ColorOption for &'a str {
fn color_option(self, default_color: Color, config_color: &ColorVariant) -> ColoredString {
match config_color {
&ColorVariant::plain => self.normal(),
_ => self.color(default_color),
ColorVariant::plain => self.normal(),
ColorVariant::default => self.color(default_color),
}
}
}
20 changes: 12 additions & 8 deletions src/modules/display/historical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ use crate::modules::{
};

use super::{
border::*, gui_config::ColorOption, hourly::HourlyForecast, product::Product, utils::lang_len_diff,
border::{Border, BorderStyle, Edge, Separator},
gui_config::ColorOption,
hourly::HourlyForecast,
product::Product,
utils::lang_len_diff,
weathercode::WeatherCode,
};

Expand All @@ -29,7 +33,7 @@ pub struct HistoricalWeather {

impl HistoricalWeather {
pub fn render(self, params: &Params) {
let HistoricalWeather {
let Self {
address,
temp_max_min,
apparent_temp_max_min,
Expand Down Expand Up @@ -84,7 +88,7 @@ impl HistoricalWeather {
);

// Apparent Temperature & Sun Rise & Sun Set
let sunrise_and_sunset = format!("{} {}", sunrise, sunset);
let sunrise_and_sunset = format!("{sunrise} {sunset}");
println!(
"{} {} {: >WIDTH$} {}",
Border::L.fmt(&gui.border).color_option(BrightBlack, &gui.color),
Expand Down Expand Up @@ -141,11 +145,11 @@ impl HistoricalWeather {
// Display Items
let sunrise = match params.config.units.time {
Time::am_pm => format!("{}:{}am", sunrise_hour, &sunrise[0][14..16]),
_ => sunrise[0][11..16].to_string(),
Time::military => sunrise[0][11..16].to_string(),
};
let sunset = match params.config.units.time {
Time::am_pm => format!("{}:{}pm", sunset_hour - 12, &sunset[0][14..16]),
_ => sunset[0][11..16].to_string(),
Time::military => sunset[0][11..16].to_string(),
};
let temp_max_min = format!(
"{:.1}/{:.1}{}",
Expand All @@ -171,10 +175,10 @@ impl HistoricalWeather {
);
let date = format!(
" {}",
if !(lang == "en_US" || lang == "en") {
Locales::localize_date(dt, lang)?
} else {
if lang == "en_US" || lang == "en" {
dt.format("%a, %e %b %Y").to_string()
} else {
Locales::localize_date(dt, lang)?
}
);
let sunrise = format!(" {sunrise}");
Expand Down
Loading

0 comments on commit a02502a

Please sign in to comment.