Skip to content

Commit

Permalink
use 'Self' keyword to refer to own type
Browse files Browse the repository at this point in the history
  • Loading branch information
danieleades committed Apr 9, 2023
1 parent 33a5044 commit 05d2187
Show file tree
Hide file tree
Showing 14 changed files with 48 additions and 47 deletions.
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
clippy::unused_async,
clippy::uninlined_format_args,
clippy::implicit_clone,
clippy::inconsistent_struct_constructor
clippy::inconsistent_struct_constructor,
clippy::use_self
)]

mod modules;
Expand Down
12 changes: 6 additions & 6 deletions src/modules/display/border.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,35 @@ pub enum Border {
impl Border {
pub 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 => "╚",
Expand Down
4 changes: 2 additions & 2 deletions src/modules/display/current.rs
Original file line number Diff line number Diff line change
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 @@ -231,7 +231,7 @@ impl Current {
None
};

Ok(Current {
Ok(Self {
address,
temperature,
apparent_temperature,
Expand Down
4 changes: 2 additions & 2 deletions src/modules/display/day.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,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 @@ -152,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
4 changes: 2 additions & 2 deletions src/modules/display/graph.rs
Original file line number Diff line number Diff line change
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
2 changes: 1 addition & 1 deletion src/modules/display/historical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,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
4 changes: 2 additions & 2 deletions src/modules/display/hourly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ struct WeatherSummery {

impl HourlyForecast {
pub fn render(self, params: &Params) {
let HourlyForecast {
let Self {
heading,
temperatures,
precipitation,
Expand Down Expand Up @@ -283,7 +283,7 @@ impl HourlyForecast {
_ => None,
};

Ok(HourlyForecast {
Ok(Self {
heading: params.texts.weather.hourly_forecast.to_string(),
temperatures: Self::prepare_temperatures(
temperatures,
Expand Down
2 changes: 1 addition & 1 deletion src/modules/display/weathercode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl WeatherCode {
_ => return Err(anyhow!("Unknown weather code")),
};

Ok(WeatherCode {
Ok(Self {
interpretation: res.0.to_string(),
icon: res.1,
})
Expand Down
2 changes: 1 addition & 1 deletion src/modules/display/week.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,6 @@ impl Week {
days.push(day);
}

Ok(Week { days, width })
Ok(Self { days, width })
}
}
32 changes: 16 additions & 16 deletions src/modules/display/wind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ pub enum WindDirection {
impl WindDirection {
pub fn get_direction(wd: f32) -> Result<Self> {
let direction = match wd % 360.0 {
wd if (337.5..=360.0).contains(&wd) || (0.0..22.5).contains(&wd) => WindDirection::N,
wd if (22.5..67.5).contains(&wd) => WindDirection::NE,
wd if (67.5..112.5).contains(&wd) => WindDirection::E,
wd if (112.5..157.5).contains(&wd) => WindDirection::SE,
wd if (157.5..202.5).contains(&wd) => WindDirection::S,
wd if (202.5..247.5).contains(&wd) => WindDirection::SW,
wd if (247.5..292.5).contains(&wd) => WindDirection::W,
wd if (292.5..337.5).contains(&wd) => WindDirection::NW,
wd if (337.5..=360.0).contains(&wd) || (0.0..22.5).contains(&wd) => Self::N,
wd if (22.5..67.5).contains(&wd) => Self::NE,
wd if (67.5..112.5).contains(&wd) => Self::E,
wd if (112.5..157.5).contains(&wd) => Self::SE,
wd if (157.5..202.5).contains(&wd) => Self::S,
wd if (202.5..247.5).contains(&wd) => Self::SW,
wd if (247.5..292.5).contains(&wd) => Self::W,
wd if (292.5..337.5).contains(&wd) => Self::NW,
_ => return Err(anyhow!("Wind from another dimension")),
};

Expand All @@ -32,14 +32,14 @@ impl WindDirection {

pub fn get_icon(&self) -> char {
match *self {
WindDirection::N => '',
WindDirection::NE => '',
WindDirection::E => '',
WindDirection::SE => '',
WindDirection::S => '',
WindDirection::SW => '',
WindDirection::W => '',
WindDirection::NW => '',
Self::N => '',
Self::NE => '',
Self::E => '',
Self::SE => '',
Self::S => '',
Self::SW => '',
Self::W => '',
Self::NW => '',
}
}
}
2 changes: 1 addition & 1 deletion src/modules/localization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ const DATETIME_LOCALES: &str = include_str!(concat!(env!("CARGO_MANIFEST_DIR"),

impl Locales {
pub async fn get(lang: &str) -> Result<Self> {
let mut texts = Locales::default();
let mut texts = Self::default();
let path = Self::get_path(lang);

if let Ok(file) = fs::read_to_string(path) {
Expand Down
14 changes: 7 additions & 7 deletions src/modules/location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl From<&OpenMeteoGeoObj> for Location {
}

impl Location {
pub async fn get(address: &str, lang: &str) -> Result<Location> {
pub async fn get(address: &str, lang: &str) -> Result<Self> {
let client = Client::builder().user_agent("wthrr-the-weathercrab").build()?;
let results = Self::search_osm(&client, address, lang).await;

Expand All @@ -76,7 +76,7 @@ impl Location {
}
}

async fn search_osm(client: &Client, address: &str, language: &str) -> Result<Location> {
async fn search_osm(client: &Client, address: &str, language: &str) -> Result<Self> {
client
.get(
&ApiQuery::location(ApiName::OpenStreetMap, address, language)
Expand All @@ -88,20 +88,20 @@ impl Location {
.json::<Vec<OpenStreetMapGeoObj>>()
.await?
.first()
.ok_or_else(|| anyhow!(Location::error_message()))
.map(Location::from)
.ok_or_else(|| anyhow!(Self::error_message()))
.map(Self::from)
}

async fn search_open_meteo(client: &Client, address: &str, language: &str) -> Result<Location> {
async fn search_open_meteo(client: &Client, address: &str, language: &str) -> Result<Self> {
client
.get(&ApiQuery::location(ApiName::OpenMeteo, address, language).convert().assemble())
.send()
.await?
.json::<Vec<OpenMeteoGeoObj>>()
.await?
.first()
.ok_or_else(|| anyhow!(Location::error_message()))
.map(Location::from)
.ok_or_else(|| anyhow!(Self::error_message()))
.map(Self::from)
}

pub async fn resolve_input(arg_address: &str, config: &Config, texts: &Locales) -> Result<String> {
Expand Down
4 changes: 2 additions & 2 deletions src/modules/units.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ pub enum Precipitation {
}

impl Units {
pub fn merge(arg_units: &[UnitArg], cfg_units: &Units) -> Units {
pub fn merge(arg_units: &[UnitArg], cfg_units: &Self) -> Self {
cfg_units.assign_unit_args(arg_units)
}

pub fn assign_unit_args(mut self, arg_units: &[UnitArg]) -> Units {
pub fn assign_unit_args(mut self, arg_units: &[UnitArg]) -> Self {
for val in arg_units {
if Temperature::VARIANTS.as_ref().contains(&val.as_ref()) {
self.temperature = Temperature::from_str(val.as_ref()).unwrap();
Expand Down
6 changes: 3 additions & 3 deletions src/modules/weather.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub struct Daily {
}

impl Weather {
pub async fn get(lat: f64, lon: f64, units: &Units) -> Result<Weather> {
pub async fn get(lat: f64, lon: f64, units: &Units) -> Result<Self> {
// TODO: conditionally expand api call
let url = format!(
"https://api.open-meteo.com/v1/forecast?
Expand All @@ -100,7 +100,7 @@ latitude={lat}

let res = reqwest::get(url)
.await?
.json::<Weather>()
.json::<Self>()
.await
.with_context(|| "Weather data request failed.")?;

Expand Down Expand Up @@ -142,7 +142,7 @@ latitude={lat}
) -> Result<HashMap<&'a NaiveDate, OptionalWeather>> {
let mut res = HashMap::new();
for date in dates {
res.insert(date, Weather::get_date(*date, lat, lon, units).await?);
res.insert(date, Self::get_date(*date, lat, lon, units).await?);
}

Ok(res)
Expand Down

0 comments on commit 05d2187

Please sign in to comment.