From 5220991d1b7db3671e9945a5e63a16858439ae69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Radek=20Dvo=C5=99=C3=A1k?= Date: Mon, 29 Nov 2021 14:57:27 +0100 Subject: [PATCH] Feature/skip internal (#7) * Updated uom to compile * Skipped internal fields Sys.type has been missing from the response suddenly * Fixups from clippy --- Cargo.lock | 6 +++-- Cargo.toml | 2 +- src/app/publisher.rs | 18 +++++++------- src/arguments.rs | 44 +++++++++++++++++------------------ src/domain/current_weather.rs | 2 +- src/location_specifier.rs | 12 +++++----- src/weather_client.rs | 2 +- src/weather_types.rs | 5 +++- 8 files changed, 48 insertions(+), 43 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e4736eb..c71a352 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,5 +1,7 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +version = 3 + [[package]] name = "adler32" version = "1.0.4" @@ -1408,9 +1410,9 @@ checksum = "a156c684c91ea7d62626509bce3cb4e1d9ed5c4d978f7b4352658f96a4c26b4a" [[package]] name = "uom" -version = "0.27.0" +version = "0.31.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "51fc04fb44bcb7806da71885872cb15d123b681e459a476ef8a0bab287bee0cd" +checksum = "b1ee6bfd0a27bf614353809a035cf6880b74239ec6c5e39a7b2860ca16809137" dependencies = [ "num-traits", "typenum", diff --git a/Cargo.toml b/Cargo.toml index 93d7103..e086429 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,7 @@ serde_derive = "^1.0" serde_json = "^1.0" structopt = { version = "^0.3.11" } tokio = { version = "^0.2", features = ["full"] } -uom = "^0.27.0" +uom = "^0.31.1" url = "^2.1.1" sloggers = "^0.3.5" slog = "^2.5.2" diff --git a/src/app/publisher.rs b/src/app/publisher.rs index ad292f1..7ba6292 100644 --- a/src/app/publisher.rs +++ b/src/app/publisher.rs @@ -30,9 +30,9 @@ impl<'a> Temperature<'a> { pub fn from_publishing_args(args: &'a dyn PublishingInfo) -> Self { Self::new( - &args.get_prefix(), - &args.get_device_name(), - &args.get_channel_thermometer(), + args.get_prefix(), + args.get_device_name(), + args.get_channel_thermometer(), ) } } @@ -65,9 +65,9 @@ impl<'a> Pressure<'a> { pub fn from_publishing_args(args: &'a dyn PublishingInfo) -> Self { Self::new( - &args.get_prefix(), - &args.get_device_name(), - &args.get_channel_barometer(), + args.get_prefix(), + args.get_device_name(), + args.get_channel_barometer(), ) } } @@ -100,9 +100,9 @@ impl<'a> Humidity<'a> { pub fn from_publishing_args(args: &'a dyn PublishingInfo) -> Self { Self::new( - &args.get_prefix(), - &args.get_device_name(), - &args.get_channel_hygrometer(), + args.get_prefix(), + args.get_device_name(), + args.get_channel_hygrometer(), ) } } diff --git a/src/arguments.rs b/src/arguments.rs index e763a5a..38e18ec 100644 --- a/src/arguments.rs +++ b/src/arguments.rs @@ -32,7 +32,7 @@ pub struct Args { #[structopt(env)] pub city_id: u32, - #[structopt(short, long, env, default_value = & Units::CELSIUS.value().unwrap(), possible_values = & Units::variants())] + #[structopt(short, long, env, default_value = & Units::Celsius.value().unwrap(), possible_values = & Units::variants())] pub units: Units, #[structopt(flatten)] @@ -122,25 +122,25 @@ impl PublishingInfo for MqttPublishingArgs { #[derive(Debug)] pub enum Units { - KELVIN, - FAHRENHEIT, - CELSIUS, + Kelvin, + Fahrenheit, + Celsius, } impl Units { pub fn convert_temperature(&self, temperature: ThermodynamicTemperature) -> f32 { match *self { - Units::KELVIN => temperature.get::(), - Units::FAHRENHEIT => temperature.get::(), - Units::CELSIUS => temperature.get::(), + Units::Kelvin => temperature.get::(), + Units::Fahrenheit => temperature.get::(), + Units::Celsius => temperature.get::(), } } pub fn value(&self) -> Option<&'static str> { match *self { - Units::CELSIUS => Some("celsius"), - Units::FAHRENHEIT => Some("fahrenheit"), - Units::KELVIN => None, + Units::Celsius => Some("celsius"), + Units::Fahrenheit => Some("fahrenheit"), + Units::Kelvin => None, } } @@ -153,9 +153,9 @@ impl FromStr for Units { type Err = ParseError; fn from_str(day: &str) -> Result { match day { - "celsius" => Ok(Units::CELSIUS), - "fahrenheit" => Ok(Units::FAHRENHEIT), - "kelvin" => Ok(Units::KELVIN), + "celsius" => Ok(Units::Celsius), + "fahrenheit" => Ok(Units::Fahrenheit), + "kelvin" => Ok(Units::Kelvin), _ => unreachable!(), } } @@ -166,9 +166,9 @@ pub struct ApiKey { value: String, } -impl Into for ApiKey { - fn into(self) -> String { - self.value +impl From for String { + fn from(k: ApiKey) -> Self { + k.value } } @@ -193,9 +193,9 @@ impl FromStr for User { } } -impl Into for User { - fn into(self) -> String { - self.0 +impl From for String { + fn from(u: User) -> Self { + u.0 } } @@ -210,8 +210,8 @@ impl FromStr for Password { } } -impl Into for Password { - fn into(self) -> String { - self.0 +impl From for String { + fn from(p: Password) -> Self { + p.0 } } diff --git a/src/domain/current_weather.rs b/src/domain/current_weather.rs index 5fe8c7f..3ecaaba 100644 --- a/src/domain/current_weather.rs +++ b/src/domain/current_weather.rs @@ -71,7 +71,7 @@ impl Humidity { #[allow(dead_code)] pub fn is_valid(humidity: f32) -> bool { let rounded = Self::round(humidity); - rounded >= 0.0 && rounded <= 100.0 + (0.0..=100.0).contains(&rounded) } } diff --git a/src/location_specifier.rs b/src/location_specifier.rs index 821e53f..a552387 100644 --- a/src/location_specifier.rs +++ b/src/location_specifier.rs @@ -37,10 +37,10 @@ impl<'a> LocationSpecifier<'a> { pub fn format(&'a self) -> Vec<(String, String)> { match &self { LocationSpecifier::CityAndCountryName { city, country } => { - if *country == "" { - return vec![("q".to_string(), (*city).to_string())]; + return if country.is_empty() { + vec![("q".to_string(), (*city).to_string())] } else { - return vec![("q".to_string(), format!("{},{}", city, country))]; + vec![("q".to_string(), format!("{},{}", city, country))] } } LocationSpecifier::CityId(id) => { @@ -53,10 +53,10 @@ impl<'a> LocationSpecifier<'a> { ]; } LocationSpecifier::ZipCode { zip, country } => { - if *country == "" { - return vec![("zip".to_string(), (*zip).to_string())]; + return if country.is_empty() { + vec![("zip".to_string(), (*zip).to_string())] } else { - return vec![("zip".to_string(), format!("{},{}", zip, country))]; + vec![("zip".to_string(), format!("{},{}", zip, country))] } } LocationSpecifier::BoundingBox { diff --git a/src/weather_client.rs b/src/weather_client.rs index c502a93..0e10ed6 100644 --- a/src/weather_client.rs +++ b/src/weather_client.rs @@ -55,7 +55,7 @@ where { pub fn new(location_specifier: LocationSpecifier<'a>, api_key: T) -> Self { let default_base_url = "https://api.openweathermap.org/data/2.5/"; - let base_url: Url = Url::parse(&default_base_url) + let base_url: Url = Url::parse(default_base_url) .unwrap_or_else(|_| panic!("Broken default hardcoded base URL {}", &default_base_url)); OpenWeatherMapClientBuilder { diff --git a/src/weather_types.rs b/src/weather_types.rs index c2d67a4..ce29028 100644 --- a/src/weather_types.rs +++ b/src/weather_types.rs @@ -58,9 +58,11 @@ pub struct ErrorReport { #[derive(Serialize, Deserialize, Debug)] pub struct Sys { - #[serde(rename = "type")] + #[serde(rename = "type", skip_deserializing)] pub message_type: u32, + #[serde(skip_deserializing)] pub id: u32, + #[serde(skip_deserializing)] pub message: Option, pub country: String, pub sunrise: u64, @@ -80,5 +82,6 @@ pub struct WeatherReportCurrent { pub sys: Sys, pub id: u64, pub name: String, + #[serde(skip_deserializing)] pub cod: u16, }