From 1affcb9d1a4668851a3ecea8a6efa1230bcc093a Mon Sep 17 00:00:00 2001 From: Jorge Bolois Date: Sun, 30 Jul 2023 18:13:42 +0200 Subject: [PATCH] Correct stdout, updating lib and some clippy clean --- Cargo.lock | 114 ++++++++++-------- src/coords.rs | 4 +- src/current_weather_extractor.rs | 3 +- src/current_weather_output.rs | 73 +++++------ src/current_weather_print_params.rs | 28 ----- src/ifconfig/{ifconfig.rs => ifconfig_api.rs} | 0 src/ip_api/{ip_api.rs => ip_api_api.rs} | 0 src/ip_api/ip_location_cache.rs | 9 +- src/main.rs | 24 ++-- .../{open_meteo.rs => open_meteo_api.rs} | 0 10 files changed, 117 insertions(+), 138 deletions(-) rename src/ifconfig/{ifconfig.rs => ifconfig_api.rs} (100%) rename src/ip_api/{ip_api.rs => ip_api_api.rs} (100%) rename src/open_meteo/{open_meteo.rs => open_meteo_api.rs} (100%) diff --git a/Cargo.lock b/Cargo.lock index b74222f..d238892 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -30,9 +30,9 @@ dependencies = [ [[package]] name = "base64" -version = "0.13.1" +version = "0.21.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8" +checksum = "604178f6c5c21f02dc555784810edfb88d34ac2c73b2eae109655649ee73ce3d" [[package]] name = "billboard" @@ -193,9 +193,9 @@ dependencies = [ [[package]] name = "itoa" -version = "1.0.6" +version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "453ad9f582a441959e5f0d088b02ce04cfe8d51a8eaf077f12ac6d3e94164ca6" +checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38" [[package]] name = "js-sys" @@ -214,9 +214,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.146" +version = "0.2.147" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f92be4933c13fd498862a9e02a3055f8a8d9c039ce33db97306fd5a6caa7f29b" +checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3" [[package]] name = "log" @@ -289,18 +289,18 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.60" +version = "1.0.66" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dec2b086b7a862cf4de201096214fa870344cf922b2b30c167badb3af3195406" +checksum = "18fb31db3f9bddb2ea821cde30a9f70117e3f119938b5ee630b7403aa6e2ead9" dependencies = [ "unicode-ident", ] [[package]] name = "quote" -version = "1.0.28" +version = "1.0.32" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b9ab9c7eadfd8df19006f1cf1a4aed13540ed5cbc047010ece5826e10825488" +checksum = "50f3b39ccfb720540debaa0164757101c08ecb8d326b15358ce76a62c7e85965" dependencies = [ "proc-macro2", ] @@ -342,21 +342,41 @@ dependencies = [ [[package]] name = "rustls" -version = "0.20.8" +version = "0.21.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fff78fc74d175294f4e83b28343315ffcfb114b156f0185e9741cb5570f50e2f" +checksum = "79ea77c539259495ce8ca47f53e66ae0330a8819f67e23ac96ca02f50e7b7d36" dependencies = [ "log", "ring", + "rustls-webpki 0.101.2", "sct", - "webpki", +] + +[[package]] +name = "rustls-webpki" +version = "0.100.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6207cd5ed3d8dca7816f8f3725513a34609c0c765bf652b8c3cb4cfd87db46b" +dependencies = [ + "ring", + "untrusted", +] + +[[package]] +name = "rustls-webpki" +version = "0.101.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "513722fd73ad80a71f72b61009ea1b584bcfa1483ca93949c8f290298837fa59" +dependencies = [ + "ring", + "untrusted", ] [[package]] name = "ryu" -version = "1.0.13" +version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f91339c0467de62360649f8d3e185ca8de4224ff281f66000de5eb2a77a79041" +checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741" [[package]] name = "sct" @@ -370,29 +390,29 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.164" +version = "1.0.178" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e8c8cf938e98f769bc164923b06dce91cea1751522f46f8466461af04c9027d" +checksum = "60363bdd39a7be0266a520dab25fdc9241d2f987b08a01e01f0ec6d06a981348" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.164" +version = "1.0.178" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9735b638ccc51c28bf6914d90a2e9725b377144fc612c49a611fddd1b631d68" +checksum = "f28482318d6641454cb273da158647922d1be6b5a2fcc6165cd89ebdd7ed576b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.18", + "syn 2.0.27", ] [[package]] name = "serde_json" -version = "1.0.97" +version = "1.0.104" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bdf3bf93142acad5821c99197022e170842cdbc1c30482b98750c688c640842a" +checksum = "076066c5f1078eac5b722a31827a8832fe108bed65dfa75e233c89f8206e976c" dependencies = [ "itoa", "ryu", @@ -448,9 +468,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.18" +version = "2.0.27" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32d41677bcbe24c20c52e7c70b0d8db04134c5d1066bf98662e2871ad200ea3e" +checksum = "b60f673f44a8255b9c8c657daf66a596d435f2da81a555b06dc644d080ba45e0" dependencies = [ "proc-macro2", "quote", @@ -478,22 +498,22 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.40" +version = "1.0.44" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "978c9a314bd8dc99be594bc3c175faaa9794be04a5a5e153caba6915336cebac" +checksum = "611040a08a0439f8248d1990b111c95baa9c704c805fa1f62104b39655fd7f90" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.40" +version = "1.0.44" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f9456a42c5b0d803c8cd86e73dd7cc9edd429499f37a3550d286d5e86720569f" +checksum = "090198534930841fab3a5d1bb637cde49e339654e606195f8d9c76eeb081dc96" dependencies = [ "proc-macro2", "quote", - "syn 2.0.18", + "syn 2.0.27", ] [[package]] @@ -519,9 +539,9 @@ checksum = "92888ba5573ff080736b3648696b70cafad7d250551175acbaa4e0385b3e1460" [[package]] name = "unicode-ident" -version = "1.0.9" +version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b15811caf2415fb889178633e7724bad2509101cde276048e013b9def5e51fa0" +checksum = "301abaae475aa91687eb82514b328ab47a211a533026cb25fc3e519b86adfc3c" [[package]] name = "unicode-normalization" @@ -552,19 +572,19 @@ checksum = "a156c684c91ea7d62626509bce3cb4e1d9ed5c4d978f7b4352658f96a4c26b4a" [[package]] name = "ureq" -version = "2.6.2" +version = "2.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "338b31dd1314f68f3aabf3ed57ab922df95ffcd902476ca7ba3c4ce7b908c46d" +checksum = "0b11c96ac7ee530603dcdf68ed1557050f374ce55a5a07193ebf8cbc9f8927e9" dependencies = [ "base64", "flate2", "log", "once_cell", "rustls", + "rustls-webpki 0.100.1", "serde", "serde_json", "url", - "webpki", "webpki-roots", ] @@ -618,7 +638,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn 2.0.18", + "syn 2.0.27", "wasm-bindgen-shared", ] @@ -640,7 +660,7 @@ checksum = "54681b18a46765f095758388f2d0cf16eb8d4169b639ab575a8f5693af210c7b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.18", + "syn 2.0.27", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -661,23 +681,13 @@ dependencies = [ "wasm-bindgen", ] -[[package]] -name = "webpki" -version = "0.22.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f095d78192e208183081cc07bc5515ef55216397af48b873e5edcd72637fa1bd" -dependencies = [ - "ring", - "untrusted", -] - [[package]] name = "webpki-roots" -version = "0.22.6" +version = "0.23.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6c71e40d7d2c34a5106301fb632274ca37242cd0c9d3e64dbece371a40a2d87" +checksum = "b03058f88386e5ff5310d9111d53f48b17d732b401aeb83a8d5190f2ac459338" dependencies = [ - "webpki", + "rustls-webpki 0.100.1", ] [[package]] @@ -717,7 +727,7 @@ version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" dependencies = [ - "windows-targets 0.48.0", + "windows-targets 0.48.1", ] [[package]] @@ -737,9 +747,9 @@ dependencies = [ [[package]] name = "windows-targets" -version = "0.48.0" +version = "0.48.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b1eb6f0cd7c80c79759c929114ef071b87354ce476d9d94271031c0497adfd5" +checksum = "05d4b17490f70499f20b9e791dcf6a299785ce8af4d709018206dc5b4953e95f" dependencies = [ "windows_aarch64_gnullvm 0.48.0", "windows_aarch64_msvc 0.48.0", diff --git a/src/coords.rs b/src/coords.rs index feb9192..be2dd08 100644 --- a/src/coords.rs +++ b/src/coords.rs @@ -12,11 +12,11 @@ impl Coordinates { } pub fn is_correct_latitude(&self) -> bool { - return self.latitude > -90.0 && self.latitude < 90.0; + self.latitude > -90.0 && self.latitude < 90.0 } pub fn is_correct_longitude(&self) -> bool { - return self.longitude > -180.0 && self.longitude < 180.0; + self.longitude > -180.0 && self.longitude < 180.0 } } diff --git a/src/current_weather_extractor.rs b/src/current_weather_extractor.rs index d03fd89..fa07d19 100644 --- a/src/current_weather_extractor.rs +++ b/src/current_weather_extractor.rs @@ -54,7 +54,6 @@ impl CurrentWeatherExtractor { if self.params.winddirection || self.params.all { output.data.winddirection = Some(self.current_weather.winddirection); } - - return output; + output } } diff --git a/src/current_weather_output.rs b/src/current_weather_output.rs index 90b3c87..ab6c865 100644 --- a/src/current_weather_output.rs +++ b/src/current_weather_output.rs @@ -1,3 +1,5 @@ +use std::fmt::Display; + use serde::{Deserialize, Serialize}; use crate::{ @@ -16,58 +18,45 @@ pub struct CurrentWeatherOutput { pub data: CurrentWeatherOutputModel, } -impl CurrentWeatherOutput { - pub fn new( - format: DataFormat, - temperature_unit: TempUnit, - speed_unit: SpeedUnit, - ) -> CurrentWeatherOutput { - CurrentWeatherOutput { - format, - temperature_unit, - speed_unit, - data: CurrentWeatherOutputModel::new(), - } - } - - pub fn to_string(&self) -> String { +impl Display for CurrentWeatherOutput { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { if DataFormat::JSON == self.format { - serde_json::to_string(&self.data).unwrap() + write!(f, "{}", serde_json::to_string(&self.data).unwrap()) } else { let mut string_vec: Vec = Vec::new(); if DataFormat::Normal == self.format { string_vec.push(self.create_header()); } else { - if self.data.latitude.is_some() { - string_vec.push(self.data.latitude.unwrap().to_string()) + if let Some(latitude) = self.data.latitude { + string_vec.push(latitude.to_string()) } - if self.data.longitude.is_some() { - string_vec.push(self.data.longitude.unwrap().to_string()) + if let Some(longitude) = self.data.longitude { + string_vec.push(longitude.to_string()) } - if self.data.city.is_some() { - string_vec.push(self.data.city.clone().unwrap().to_string()) + if let Some(city) = &self.data.city { + string_vec.push(city.to_string()) } } let is_day = self.extract_day(); - if is_day.is_some() { - string_vec.push(is_day.unwrap()); + if let Some(is_day) = is_day { + string_vec.push(is_day); } let temperature = self.extract_temperature(); - if temperature.is_some() { - string_vec.push(temperature.unwrap()); + if let Some(temperature) = temperature { + string_vec.push(temperature); } let windspeed = self.extract_wind_speed(); - if windspeed.is_some() { - string_vec.push(windspeed.unwrap()); + if let Some(windspeed) = windspeed { + string_vec.push(windspeed); } let winddirection = self.extract_wind_direction(); - if winddirection.is_some() { - string_vec.push(winddirection.unwrap()); + if let Some(winddirection) = winddirection { + string_vec.push(winddirection); } if DataFormat::Normal == self.format @@ -90,14 +79,26 @@ impl CurrentWeatherOutput { } if DataFormat::Clean == self.format { - let final_string = string_vec.join(","); - final_string + write!(f, "{}", string_vec.join(",")) } else { string_vec.push(self.create_footer()); - string_vec.join("\n") + write!(f, "{}", string_vec.join("\n")) } } } +} + +impl CurrentWeatherOutput { + pub fn new( + format: DataFormat, + temperature_unit: TempUnit, + speed_unit: SpeedUnit, + ) -> CurrentWeatherOutput { + CurrentWeatherOutput { + format, temperature_unit, speed_unit, + data: CurrentWeatherOutputModel::new(), + } + } fn create_header(&self) -> String { let mut title_header: Vec = Vec::new(); @@ -166,15 +167,15 @@ impl CurrentWeatherOutput { fn parse_custom_data(&self, data: &str, custom: &str) -> String { if self.format == DataFormat::Clean { - format!("{data}") + data.to_string() } else { - format!("{custom}") + custom.to_string() } } fn parse_simple_data(&self, data: &str, descriptor: &str, end_text: Option<&str>) -> String { if self.format == DataFormat::Clean { - format!("{data}") + data.to_string() } else { let end_text = end_text.unwrap_or(""); format!("{descriptor}: {data}{end_text}") diff --git a/src/current_weather_print_params.rs b/src/current_weather_print_params.rs index 7ee81bb..533fb05 100644 --- a/src/current_weather_print_params.rs +++ b/src/current_weather_print_params.rs @@ -14,31 +14,3 @@ pub struct CurrentWeatherPrintParams { pub include_city: bool, pub format: DataFormat, } - -impl CurrentWeatherPrintParams { - pub fn new( - all: bool, - is_day: bool, - temperature: bool, - temperature_unit: TempUnit, - windspeed: bool, - speed_unit: SpeedUnit, - winddirection: bool, - include_coords: bool, - include_city: bool, - format: DataFormat, - ) -> CurrentWeatherPrintParams { - CurrentWeatherPrintParams { - all, - is_day, - temperature, - temperature_unit, - windspeed, - speed_unit, - winddirection, - include_coords, - include_city, - format, - } - } -} diff --git a/src/ifconfig/ifconfig.rs b/src/ifconfig/ifconfig_api.rs similarity index 100% rename from src/ifconfig/ifconfig.rs rename to src/ifconfig/ifconfig_api.rs diff --git a/src/ip_api/ip_api.rs b/src/ip_api/ip_api_api.rs similarity index 100% rename from src/ip_api/ip_api.rs rename to src/ip_api/ip_api_api.rs diff --git a/src/ip_api/ip_location_cache.rs b/src/ip_api/ip_location_cache.rs index b101889..c9584c1 100644 --- a/src/ip_api/ip_location_cache.rs +++ b/src/ip_api/ip_location_cache.rs @@ -1,7 +1,7 @@ use std::{ fs::{create_dir_all, File}, io::{Read, Write}, - path::PathBuf, + path::{Path, PathBuf}, }; use crate::ip_api::ip_location::IpLocation; @@ -60,15 +60,13 @@ pub fn get_from_cache(ip: &str) -> Option { } } -fn is_in_cache(path: &PathBuf) -> bool { +fn is_in_cache(path: &Path) -> bool { path.is_file() } fn get_cache_dir() -> Option { let dir = ProjectDirs::from("com", "midefos", "open-meteo-cli"); - if dir.is_some() { - let project_dir = dir.unwrap(); - + if let Some(project_dir) = &dir { let cache_dir = project_dir.cache_dir(); if !cache_dir.exists() { let res = create_dir_all(cache_dir); @@ -76,7 +74,6 @@ fn get_cache_dir() -> Option { return None; } } - Some(cache_dir.to_owned()) } else { None diff --git a/src/main.rs b/src/main.rs index 38791c7..81f8621 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,7 @@ mod current_weather_output_model; mod current_weather_print_params; mod ifconfig { - pub mod ifconfig; + pub mod ifconfig_api; } mod formats { @@ -16,14 +16,14 @@ mod formats { } mod ip_api { - pub mod ip_api; + pub mod ip_api_api; pub mod ip_location; pub mod ip_location_cache; } mod open_meteo { pub mod current_weather; - pub mod open_meteo; + pub mod open_meteo_api; } use billboard::{Alignment, Billboard}; @@ -36,9 +36,9 @@ use cli::Arguments; use coords::Coordinates; use current_weather_extractor::CurrentWeatherExtractor; use current_weather_print_params::CurrentWeatherPrintParams; -use ifconfig::ifconfig::extract_public_ip; -use ip_api::ip_api::extract_coords_and_city; -use open_meteo::open_meteo::request_current_weather; +use ifconfig::ifconfig_api::extract_public_ip; +use ip_api::ip_api_api::extract_coords_and_city; +use open_meteo::open_meteo_api::request_current_weather; fn main() { let opts = Arguments::from_args(); @@ -98,8 +98,8 @@ fn main() { let ip_location; let cache_ip_location = get_from_cache(&ip); - if cache_ip_location.is_some() { - ip_location = cache_ip_location.unwrap(); + if let Some(cache_ip_location) = cache_ip_location { + ip_location = cache_ip_location; } else { let ip_location_res = extract_coords_and_city(&ip); ip_location = ip_location_res.unwrap_or_else(|_| { @@ -130,7 +130,7 @@ fn main() { format = DataFormat::JSON; } - let print_params = CurrentWeatherPrintParams::new( + let print_params = CurrentWeatherPrintParams { all, is_day, temperature, @@ -141,7 +141,7 @@ fn main() { include_coords, include_city, format, - ); + }; let current_weather_printer = CurrentWeatherExtractor::new(current_weather, print_params, coordinates, city); @@ -151,9 +151,9 @@ fn main() { .text_alignment(Alignment::Left) .box_alignment(Alignment::Left) .build() - .eprint(output.to_string()); + .print(output.to_string()); } else { - println!("{}", output.to_string()); + println!("{}", output); } } } diff --git a/src/open_meteo/open_meteo.rs b/src/open_meteo/open_meteo_api.rs similarity index 100% rename from src/open_meteo/open_meteo.rs rename to src/open_meteo/open_meteo_api.rs