Better print values and updating README

This commit is contained in:
2023-05-21 02:25:27 +02:00
parent 807e8071da
commit 87cd8701e7
3 changed files with 101 additions and 12 deletions

View File

@ -1,3 +1,5 @@
use serde_json::Value;
pub struct CurrentWeather {
current_weather: serde_json::Value,
clean: bool,
@ -11,12 +13,60 @@ impl CurrentWeather {
}
}
pub fn extract_data(&self, data_name: &str, data_description: &str) -> String {
let data = &self.current_weather[data_name];
pub fn extract_simple_data(
&self,
key: &str,
description: &str,
end_text: Option<&str>,
) -> String {
let data = self.extract_raw(key);
self.parse_simple_data(data, description, end_text)
}
pub fn extract_raw(&self, key: &str) -> &Value {
&self.current_weather[key]
}
fn parse_simple_data(&self, data: &Value, descriptor: &str, end_text: Option<&str>) -> String {
if self.clean {
format!("{data}")
} else {
format!("{data_description}: {data}")
let end_text = end_text.unwrap_or("");
format!("{descriptor}: {data}{end_text}")
}
}
pub fn parse_custom_data(&self, data: &Value, custom: &str) -> String {
if self.clean {
format!("{data}")
} else {
format!("{custom}")
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_extract_data_clean() {
let current_weather = serde_json::json!({
"temperature": 25,
});
let weather = CurrentWeather::new(current_weather, true);
assert_eq!(weather.extract_simple_data("temperature", "", None), "25");
}
#[test]
fn test_extract_data_not_clean() {
let current_weather = serde_json::json!({
"temperature": 25,
});
let weather = CurrentWeather::new(current_weather, false);
assert_eq!(
weather.extract_simple_data("temperature", "Temperature", Some("°C")),
"Temperature: 25°C"
);
}
}

View File

@ -26,7 +26,7 @@ fn main() {
}
let result = extract_weather(latitude, longitude);
let mut weather = result.unwrap_or_else(|_| {
let mut weather = result.unwrap_or_else(|_| {
eprintln!("[ERROR] Requesting current_weather for Latitude: {latitude}, Longitude: {longitude}");
exit(1);
});
@ -43,25 +43,43 @@ fn main() {
let current_weather = CurrentWeather::new(current_weather_data, clean);
if is_day || all {
string_vec.push(current_weather.extract_data("is_day", "Is day"));
let is_day = current_weather.extract_raw("is_day");
if is_day == "1" {
string_vec.push(current_weather.parse_custom_data(is_day, "Day"));
} else {
string_vec.push(current_weather.parse_custom_data(is_day, "Night"));
}
}
if temperature || all {
string_vec.push(current_weather.extract_data("temperature", "Temperature"));
string_vec.push(current_weather.extract_simple_data(
"temperature",
"Temperature",
Some("°C"),
));
}
if windspeed || all {
string_vec.push(current_weather.extract_data("windspeed", "Wind speed"));
string_vec.push(current_weather.extract_simple_data(
"windspeed",
"Wind speed",
Some(" km/h"),
));
}
if winddirection || all {
string_vec.push(current_weather.extract_data("winddirection", "Wind direction"));
string_vec.push(current_weather.extract_simple_data(
"winddirection",
"Wind direction",
Some("°"),
));
}
if clean {
let final_string = string_vec.join(",");
println!("{final_string}");
} else {
string_vec.push(String::from("=== Weather data by Open-Meteo.com ==="));
let final_string = string_vec.join("\n");
println!("{final_string}");
}