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 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(

View File

@ -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 {

View File

@ -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() {

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 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> {
let url = format!("http://ip-api.com/json/{}?fields=16592", ip);

View File

@ -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<IpLocation> {
@ -34,14 +43,21 @@ pub fn get_from_cache(ip: &str) -> Option<IpLocation> {
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");
if let Ok(ip_location) = serde_json::from_str(&content) {
Some(ip_location)
} else {
None
}
}
fn is_in_cache(path: &PathBuf) -> bool {

View File

@ -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();

View File

@ -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(