Some refactor, better texts, and fixing some issues

This commit is contained in:
Jorge Bolois 2023-05-20 00:05:05 +02:00
parent f7a7b85111
commit baa19a4319
3 changed files with 71 additions and 59 deletions

View File

@ -1,28 +1,34 @@
use structopt::StructOpt; use structopt::StructOpt;
#[derive(Debug, StructOpt)] #[derive(Debug, StructOpt)]
#[structopt(name = "Open Meteo CLI", about = "CLI to extract data from Open Meteo")] #[structopt(
pub enum Options { name = "Open Meteo CLI",
#[structopt(about = "Get current weather from a coordinate")] about = "CLI to extract meteorology data from Open Meteo"
)]
pub enum Arguments {
#[structopt(about = "Gets the current weather for a coordinate")]
CurrentWeather { CurrentWeather {
#[structopt(short = "l", long, help = "Latitude as a decimal number")] #[structopt(short = "l", long, help = "Latitude as a decimal number")]
latitude: f32, latitude: f32,
#[structopt(short = "g", long, help = "Altitude as a decimal number")] #[structopt(short = "L", long, help = "Altitude as a decimal number")]
longitude: f32, longitude: f32,
#[structopt(short = "d", long, help = "Extracts if its day or night")] #[structopt(short = "a", long, help = "Prints all the parameters")]
all: bool,
#[structopt(short = "d", long, help = "Prints if its day or night")]
is_day: bool, is_day: bool,
#[structopt(short = "t", long, help = "Extracts decimal temperature")] #[structopt(short = "t", long, help = "Prints decimal temperature")]
temperature: bool, temperature: bool,
#[structopt(short = "s", long, help = "Extracts decimal wind speed")] #[structopt(short = "w", long, help = "Prints decimal wind speed")]
windspeed: bool, windspeed: bool,
#[structopt(short = "w", long, help = "Extracts angle direction")] #[structopt(short = "W", long, help = "Prints wind direction angle")]
winddirection: bool, winddirection: bool,
#[structopt( #[structopt(
short = "c", short = "c",
long, long,
help = "Clean the output, and only show the values separates by commas. help = "Cleans the output, and only shows the values separates by commas.
The order is: is_day, temperature, windspeed, winddirection" - ORDER: is_day, temperature, windspeed, winddirection
- EXAMPLE: 1,22.4,12.5,170.0"
)] )]
clean: bool, clean: bool,
}, },

22
src/current_weater.rs Normal file
View File

@ -0,0 +1,22 @@
pub struct CurrentWeather {
current_weather: serde_json::Value,
clean: bool,
}
impl CurrentWeather {
pub fn new(current_weather: serde_json::Value, clean: bool) -> CurrentWeather {
CurrentWeather {
current_weather,
clean,
}
}
pub fn extract_data(&self, data_name: &str, data_description: &str) -> String {
let data = &self.current_weather[data_name];
if self.clean {
format!("{data}")
} else {
format!("{data_description}: {data}")
}
}
}

View File

@ -1,74 +1,68 @@
mod cli; mod cli;
mod current_weater;
use cli::Options; use std::process::exit;
use structopt::StructOpt; use structopt::StructOpt;
use cli::Arguments;
use current_weater::CurrentWeather;
fn main() { fn main() {
let opts = Options::from_args(); let opts = Arguments::from_args();
match opts { match opts {
Options::CurrentWeather { Arguments::CurrentWeather {
latitude, latitude,
longitude, longitude,
all,
is_day, is_day,
temperature, temperature,
windspeed, windspeed,
winddirection, winddirection,
clean, clean,
} => { } => {
let result = extract_weather(latitude, longitude); if !all && !is_day && !temperature && !windspeed && !winddirection {
let weather = result.expect("Error requesting weather"); eprintln!("[ERROR] Please provide at least one parameter to print");
exit(1);
}
let mut final_string_vec: Vec<String> = Vec::new(); let result = extract_weather(latitude, longitude);
let mut weather = result.unwrap_or_else(|_| {
eprintln!("[ERROR] Requesting current_weather for Latitude: {latitude}, Longitude: {longitude}");
exit(1);
});
let mut string_vec: Vec<String> = Vec::new();
if !clean { if !clean {
final_string_vec.push(format!( string_vec.push(format!(
"=== Current weather for Latitude: {latitude}, Longitude: {longitude} ===" "=== Current weather for Latitude: {latitude}, Longitude: {longitude} ==="
)); ));
} }
// TODO: Check if any flag has been selected. let current_weather_data = weather["current_weather"].take();
let current_weather = &weather["current_weather"]; let current_weather = CurrentWeather::new(current_weather_data, clean);
if is_day {
let is_day = &current_weather["is_day"] == 1; if is_day || all {
if clean { string_vec.push(current_weather.extract_data("is_day", "Is day"));
final_string_vec.push(format!("{is_day}"));
} else {
final_string_vec.push(format!("Is day: {is_day}"));
}
} }
if temperature { if temperature || all {
let temperature = &current_weather["temperature"]; string_vec.push(current_weather.extract_data("temperature", "Temperature"));
if clean {
final_string_vec.push(format!("{temperature}"));
} else {
final_string_vec.push(format!("Temperature: {temperature}"));
}
} }
if windspeed { if windspeed || all {
let windspeed = &current_weather["windspeed"]; string_vec.push(current_weather.extract_data("windspeed", "Wind speed"));
if clean {
final_string_vec.push(format!("{windspeed}"));
} else {
final_string_vec.push(format!("Wind speed: {windspeed}"));
}
} }
if winddirection { if winddirection || all {
let winddirection = &current_weather["winddirection"]; string_vec.push(current_weather.extract_data("winddirection", "Wind direction"));
if clean {
final_string_vec.push(format!("{winddirection}"));
} else {
final_string_vec.push(format!("Wind direction: {winddirection}"));
}
} }
if clean { if clean {
let final_string = final_string_vec.join(","); let final_string = string_vec.join(",");
println!("{final_string}"); println!("{final_string}");
} else { } else {
let final_string = final_string_vec.join("\n"); let final_string = string_vec.join("\n");
println!("{final_string}"); println!("{final_string}");
} }
} }
@ -78,15 +72,5 @@ fn main() {
fn extract_weather(latitude: f32, longitude: f32) -> Result<serde_json::Value, ureq::Error> { fn extract_weather(latitude: f32, longitude: f32) -> Result<serde_json::Value, ureq::Error> {
let url = format!("https://api.open-meteo.com/v1/forecast?latitude={latitude}&longitude={longitude}&current_weather=true"); let url = format!("https://api.open-meteo.com/v1/forecast?latitude={latitude}&longitude={longitude}&current_weather=true");
let body: serde_json::Value = ureq::get(&url).call()?.into_json()?; let body: serde_json::Value = ureq::get(&url).call()?.into_json()?;
// TODO: We can also add this data.
// let elevation = &body["elevation"];
// println!("[D] Elevation: {elevation}");
// let timezone = &body["timezone"];
// println!("[D] Timezone: {timezone}");
// let current_weather = &body["current_weather"];
// let time = &current_weather["time"];
// println!("[D] Time: {time}");
Ok(body) Ok(body)
} }