From fd4affdb17413a5e478cd350a353278f0976e6f1 Mon Sep 17 00:00:00 2001 From: Jorge Bolois Date: Sun, 18 Jun 2023 13:53:45 +0200 Subject: [PATCH] Ordering some files --- src/cli.rs | 4 ++- src/current_weather_extractor.rs | 4 +-- src/current_weather_output.rs | 10 +++---- src/current_weather_print_params.rs | 4 ++- src/{ => formats}/data_format.rs | 0 src/{ => formats}/speed_unit.rs | 0 src/{ => formats}/temp_unit.rs | 0 src/{ => ifconfig}/ifconfig.rs | 0 src/{ => ip_api}/ip_api.rs | 2 +- src/{ => ip_api}/ip_location.rs | 0 src/{ => ip_api}/ip_location_cache.rs | 38 ++++++++++++++++------- src/main.rs | 40 ++++++++++++++++--------- src/{ => open_meteo}/current_weather.rs | 0 src/{ => open_meteo}/open_meteo.rs | 6 ++-- 14 files changed, 70 insertions(+), 38 deletions(-) rename src/{ => formats}/data_format.rs (100%) rename src/{ => formats}/speed_unit.rs (100%) rename src/{ => formats}/temp_unit.rs (100%) rename src/{ => ifconfig}/ifconfig.rs (100%) rename src/{ => ip_api}/ip_api.rs (88%) rename src/{ => ip_api}/ip_location.rs (100%) rename src/{ => ip_api}/ip_location_cache.rs (63%) rename src/{ => open_meteo}/current_weather.rs (100%) rename src/{ => open_meteo}/open_meteo.rs (82%) diff --git a/src/cli.rs b/src/cli.rs index ad7f138..e6a0c72 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,6 +1,8 @@ use structopt::StructOpt; -use crate::{data_format::DataFormat, speed_unit::SpeedUnit, temp_unit::TempUnit}; +use crate::{ + formats::data_format::DataFormat, formats::speed_unit::SpeedUnit, formats::temp_unit::TempUnit, +}; #[derive(Debug, StructOpt)] #[structopt( diff --git a/src/current_weather_extractor.rs b/src/current_weather_extractor.rs index 71ef20f..d03fd89 100644 --- a/src/current_weather_extractor.rs +++ b/src/current_weather_extractor.rs @@ -1,7 +1,7 @@ use crate::current_weather_output::CurrentWeatherOutput; use crate::{ - coords::Coordinates, current_weather::CurrentWeather, - current_weather_print_params::CurrentWeatherPrintParams, + coords::Coordinates, current_weather_print_params::CurrentWeatherPrintParams, + open_meteo::current_weather::CurrentWeather, }; pub struct CurrentWeatherExtractor { diff --git a/src/current_weather_output.rs b/src/current_weather_output.rs index 2e4a4ca..90b3c87 100644 --- a/src/current_weather_output.rs +++ b/src/current_weather_output.rs @@ -2,9 +2,9 @@ use serde::{Deserialize, Serialize}; use crate::{ current_weather_output_model::CurrentWeatherOutputModel, - data_format::DataFormat, - speed_unit::{speed_to_unit_string, SpeedUnit}, - temp_unit::{temp_to_unit_string, TempUnit}, + formats::data_format::DataFormat, + formats::speed_unit::{speed_to_unit_string, SpeedUnit}, + formats::temp_unit::{temp_to_unit_string, TempUnit}, }; #[derive(Serialize, Deserialize, Debug)] @@ -185,8 +185,8 @@ impl CurrentWeatherOutput { #[cfg(test)] mod tests { use super::*; - use crate::speed_unit::SpeedUnit; - use crate::temp_unit::TempUnit; + use crate::formats::speed_unit::SpeedUnit; + use crate::formats::temp_unit::TempUnit; #[test] fn clean_all_data() { diff --git a/src/current_weather_print_params.rs b/src/current_weather_print_params.rs index 2801552..7ee81bb 100644 --- a/src/current_weather_print_params.rs +++ b/src/current_weather_print_params.rs @@ -1,4 +1,6 @@ -use crate::{data_format::DataFormat, speed_unit::SpeedUnit, temp_unit::TempUnit}; +use crate::{ + formats::data_format::DataFormat, formats::speed_unit::SpeedUnit, formats::temp_unit::TempUnit, +}; pub struct CurrentWeatherPrintParams { pub all: bool, diff --git a/src/data_format.rs b/src/formats/data_format.rs similarity index 100% rename from src/data_format.rs rename to src/formats/data_format.rs diff --git a/src/speed_unit.rs b/src/formats/speed_unit.rs similarity index 100% rename from src/speed_unit.rs rename to src/formats/speed_unit.rs diff --git a/src/temp_unit.rs b/src/formats/temp_unit.rs similarity index 100% rename from src/temp_unit.rs rename to src/formats/temp_unit.rs diff --git a/src/ifconfig.rs b/src/ifconfig/ifconfig.rs similarity index 100% rename from src/ifconfig.rs rename to src/ifconfig/ifconfig.rs diff --git a/src/ip_api.rs b/src/ip_api/ip_api.rs similarity index 88% rename from src/ip_api.rs rename to src/ip_api/ip_api.rs index 8bdcc36..a108019 100644 --- a/src/ip_api.rs +++ b/src/ip_api/ip_api.rs @@ -1,4 +1,4 @@ -use crate::ip_location::IpLocation; +use crate::ip_api::ip_location::IpLocation; pub fn extract_coords_and_city(ip: &str) -> Result { let url = format!("http://ip-api.com/json/{}?fields=16592", ip); diff --git a/src/ip_location.rs b/src/ip_api/ip_location.rs similarity index 100% rename from src/ip_location.rs rename to src/ip_api/ip_location.rs diff --git a/src/ip_location_cache.rs b/src/ip_api/ip_location_cache.rs similarity index 63% rename from src/ip_location_cache.rs rename to src/ip_api/ip_location_cache.rs index d8acbb6..b101889 100644 --- a/src/ip_location_cache.rs +++ b/src/ip_api/ip_location_cache.rs @@ -4,7 +4,7 @@ use std::{ path::PathBuf, }; -use crate::ip_location::IpLocation; +use crate::ip_api::ip_location::IpLocation; use directories::ProjectDirs; pub fn save_to_cache(ip: &str, data: &IpLocation) { let cache_dir = get_cache_dir(); @@ -15,10 +15,19 @@ pub fn save_to_cache(ip: &str, data: &IpLocation) { let mut cache_dir = cache_dir.unwrap(); cache_dir.push(format!("{}.{}", ip, "json")); - let json = serde_json::to_string(data).expect("Error al serializar a JSON"); - let mut file = File::create(&cache_dir).expect("Error al crear el archivo"); - file.write_all(json.as_bytes()) - .expect("Error al escribir en el archivo"); + let json = serde_json::to_string(data); + if json.is_err() { + return; + } + let file = File::create(&cache_dir); + if file.is_err() { + return; + } + + let res = file.unwrap().write_all(json.unwrap().as_bytes()); + if res.is_err() { + println!("[WARN] Error saving to cache IpLocation"); + } } pub fn get_from_cache(ip: &str) -> Option { @@ -34,14 +43,21 @@ pub fn get_from_cache(ip: &str) -> Option { return None; } - let mut file = File::open(&cache_dir).expect("Error al abrir el archivo"); + let file = File::open(&cache_dir); + if file.is_err() { + return None; + } let mut content = String::new(); - file.read_to_string(&mut content) - .expect("Error al leer el archivo"); + let res = file.unwrap().read_to_string(&mut content); + if res.is_err() { + return None; + } - let ip_location: IpLocation = - serde_json::from_str(&content).expect("Error al deserializar JSON"); - Some(ip_location) + if let Ok(ip_location) = serde_json::from_str(&content) { + Some(ip_location) + } else { + None + } } fn is_in_cache(path: &PathBuf) -> bool { diff --git a/src/main.rs b/src/main.rs index a37e322..38791c7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,22 +1,34 @@ mod cli; mod coords; -mod current_weather; mod current_weather_extractor; mod current_weather_output; mod current_weather_output_model; mod current_weather_print_params; -mod data_format; -mod ifconfig; -mod ip_api; -mod ip_location; -mod ip_location_cache; -mod open_meteo; -mod speed_unit; -mod temp_unit; + +mod ifconfig { + pub mod ifconfig; +} + +mod formats { + pub mod data_format; + pub mod speed_unit; + pub mod temp_unit; +} + +mod ip_api { + pub mod ip_api; + pub mod ip_location; + pub mod ip_location_cache; +} + +mod open_meteo { + pub mod current_weather; + pub mod open_meteo; +} use billboard::{Alignment, Billboard}; -use data_format::DataFormat; -use ip_location_cache::{get_from_cache, save_to_cache}; +use formats::data_format::DataFormat; +use ip_api::ip_location_cache::{get_from_cache, save_to_cache}; use std::process::exit; use structopt::StructOpt; @@ -24,9 +36,9 @@ use cli::Arguments; use coords::Coordinates; use current_weather_extractor::CurrentWeatherExtractor; use current_weather_print_params::CurrentWeatherPrintParams; -use ifconfig::extract_public_ip; -use ip_api::extract_coords_and_city; -use open_meteo::request_current_weather; +use ifconfig::ifconfig::extract_public_ip; +use ip_api::ip_api::extract_coords_and_city; +use open_meteo::open_meteo::request_current_weather; fn main() { let opts = Arguments::from_args(); diff --git a/src/current_weather.rs b/src/open_meteo/current_weather.rs similarity index 100% rename from src/current_weather.rs rename to src/open_meteo/current_weather.rs diff --git a/src/open_meteo.rs b/src/open_meteo/open_meteo.rs similarity index 82% rename from src/open_meteo.rs rename to src/open_meteo/open_meteo.rs index 76354f3..f7a77c0 100644 --- a/src/open_meteo.rs +++ b/src/open_meteo/open_meteo.rs @@ -1,8 +1,8 @@ use crate::{ coords::Coordinates, - current_weather::CurrentWeather, - speed_unit::{speed_to_string, SpeedUnit}, - temp_unit::{temp_to_string, TempUnit}, + formats::speed_unit::{speed_to_string, SpeedUnit}, + formats::temp_unit::{temp_to_string, TempUnit}, + open_meteo::current_weather::CurrentWeather, }; pub fn request_current_weather(