Ordering some files

This commit is contained in:
Jorge Bolois 2023-06-18 13:53:45 +02:00
parent 0c3a3126f8
commit fd4affdb17
14 changed files with 70 additions and 38 deletions

View File

@ -1,6 +1,8 @@
use structopt::StructOpt; 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)] #[derive(Debug, StructOpt)]
#[structopt( #[structopt(

View File

@ -1,7 +1,7 @@
use crate::current_weather_output::CurrentWeatherOutput; use crate::current_weather_output::CurrentWeatherOutput;
use crate::{ use crate::{
coords::Coordinates, current_weather::CurrentWeather, coords::Coordinates, current_weather_print_params::CurrentWeatherPrintParams,
current_weather_print_params::CurrentWeatherPrintParams, open_meteo::current_weather::CurrentWeather,
}; };
pub struct CurrentWeatherExtractor { pub struct CurrentWeatherExtractor {

View File

@ -2,9 +2,9 @@ use serde::{Deserialize, Serialize};
use crate::{ use crate::{
current_weather_output_model::CurrentWeatherOutputModel, current_weather_output_model::CurrentWeatherOutputModel,
data_format::DataFormat, formats::data_format::DataFormat,
speed_unit::{speed_to_unit_string, SpeedUnit}, formats::speed_unit::{speed_to_unit_string, SpeedUnit},
temp_unit::{temp_to_unit_string, TempUnit}, formats::temp_unit::{temp_to_unit_string, TempUnit},
}; };
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
@ -185,8 +185,8 @@ impl CurrentWeatherOutput {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::speed_unit::SpeedUnit; use crate::formats::speed_unit::SpeedUnit;
use crate::temp_unit::TempUnit; use crate::formats::temp_unit::TempUnit;
#[test] #[test]
fn clean_all_data() { fn clean_all_data() {

View File

@ -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 struct CurrentWeatherPrintParams {
pub all: bool, pub all: bool,

View File

@ -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<IpLocation, ureq::Error> { pub fn extract_coords_and_city(ip: &str) -> Result<IpLocation, ureq::Error> {
let url = format!("http://ip-api.com/json/{}?fields=16592", ip); let url = format!("http://ip-api.com/json/{}?fields=16592", ip);

View File

@ -4,7 +4,7 @@ use std::{
path::PathBuf, path::PathBuf,
}; };
use crate::ip_location::IpLocation; use crate::ip_api::ip_location::IpLocation;
use directories::ProjectDirs; use directories::ProjectDirs;
pub fn save_to_cache(ip: &str, data: &IpLocation) { pub fn save_to_cache(ip: &str, data: &IpLocation) {
let cache_dir = get_cache_dir(); 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(); let mut cache_dir = cache_dir.unwrap();
cache_dir.push(format!("{}.{}", ip, "json")); cache_dir.push(format!("{}.{}", ip, "json"));
let json = serde_json::to_string(data).expect("Error al serializar a JSON"); let json = serde_json::to_string(data);
let mut file = File::create(&cache_dir).expect("Error al crear el archivo"); if json.is_err() {
file.write_all(json.as_bytes()) return;
.expect("Error al escribir en el archivo"); }
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<IpLocation> { pub fn get_from_cache(ip: &str) -> Option<IpLocation> {
@ -34,14 +43,21 @@ pub fn get_from_cache(ip: &str) -> Option<IpLocation> {
return None; 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(); let mut content = String::new();
file.read_to_string(&mut content) let res = file.unwrap().read_to_string(&mut content);
.expect("Error al leer el archivo"); if res.is_err() {
return None;
}
let ip_location: IpLocation = if let Ok(ip_location) = serde_json::from_str(&content) {
serde_json::from_str(&content).expect("Error al deserializar JSON");
Some(ip_location) Some(ip_location)
} else {
None
}
} }
fn is_in_cache(path: &PathBuf) -> bool { fn is_in_cache(path: &PathBuf) -> bool {

View File

@ -1,22 +1,34 @@
mod cli; mod cli;
mod coords; mod coords;
mod current_weather;
mod current_weather_extractor; mod current_weather_extractor;
mod current_weather_output; mod current_weather_output;
mod current_weather_output_model; mod current_weather_output_model;
mod current_weather_print_params; mod current_weather_print_params;
mod data_format;
mod ifconfig; mod ifconfig {
mod ip_api; pub mod ifconfig;
mod ip_location; }
mod ip_location_cache;
mod open_meteo; mod formats {
mod speed_unit; pub mod data_format;
mod temp_unit; 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 billboard::{Alignment, Billboard};
use data_format::DataFormat; use formats::data_format::DataFormat;
use ip_location_cache::{get_from_cache, save_to_cache}; use ip_api::ip_location_cache::{get_from_cache, save_to_cache};
use std::process::exit; use std::process::exit;
use structopt::StructOpt; use structopt::StructOpt;
@ -24,9 +36,9 @@ use cli::Arguments;
use coords::Coordinates; use coords::Coordinates;
use current_weather_extractor::CurrentWeatherExtractor; use current_weather_extractor::CurrentWeatherExtractor;
use current_weather_print_params::CurrentWeatherPrintParams; use current_weather_print_params::CurrentWeatherPrintParams;
use ifconfig::extract_public_ip; use ifconfig::ifconfig::extract_public_ip;
use ip_api::extract_coords_and_city; use ip_api::ip_api::extract_coords_and_city;
use open_meteo::request_current_weather; use open_meteo::open_meteo::request_current_weather;
fn main() { fn main() {
let opts = Arguments::from_args(); let opts = Arguments::from_args();

View File

@ -1,8 +1,8 @@
use crate::{ use crate::{
coords::Coordinates, coords::Coordinates,
current_weather::CurrentWeather, formats::speed_unit::{speed_to_string, SpeedUnit},
speed_unit::{speed_to_string, SpeedUnit}, formats::temp_unit::{temp_to_string, TempUnit},
temp_unit::{temp_to_string, TempUnit}, open_meteo::current_weather::CurrentWeather,
}; };
pub fn request_current_weather( pub fn request_current_weather(