Correct stdout, updating lib and some clippy clean
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
2023-07-30 18:13:42 +02:00
parent fd4affdb17
commit 1affcb9d1a
10 changed files with 117 additions and 138 deletions

View File

@ -12,11 +12,11 @@ impl Coordinates {
}
pub fn is_correct_latitude(&self) -> bool {
return self.latitude > -90.0 && self.latitude < 90.0;
self.latitude > -90.0 && self.latitude < 90.0
}
pub fn is_correct_longitude(&self) -> bool {
return self.longitude > -180.0 && self.longitude < 180.0;
self.longitude > -180.0 && self.longitude < 180.0
}
}

View File

@ -54,7 +54,6 @@ impl CurrentWeatherExtractor {
if self.params.winddirection || self.params.all {
output.data.winddirection = Some(self.current_weather.winddirection);
}
return output;
output
}
}

View File

@ -1,3 +1,5 @@
use std::fmt::Display;
use serde::{Deserialize, Serialize};
use crate::{
@ -16,58 +18,45 @@ pub struct CurrentWeatherOutput {
pub data: CurrentWeatherOutputModel,
}
impl CurrentWeatherOutput {
pub fn new(
format: DataFormat,
temperature_unit: TempUnit,
speed_unit: SpeedUnit,
) -> CurrentWeatherOutput {
CurrentWeatherOutput {
format,
temperature_unit,
speed_unit,
data: CurrentWeatherOutputModel::new(),
}
}
pub fn to_string(&self) -> String {
impl Display for CurrentWeatherOutput {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if DataFormat::JSON == self.format {
serde_json::to_string(&self.data).unwrap()
write!(f, "{}", serde_json::to_string(&self.data).unwrap())
} else {
let mut string_vec: Vec<String> = Vec::new();
if DataFormat::Normal == self.format {
string_vec.push(self.create_header());
} else {
if self.data.latitude.is_some() {
string_vec.push(self.data.latitude.unwrap().to_string())
if let Some(latitude) = self.data.latitude {
string_vec.push(latitude.to_string())
}
if self.data.longitude.is_some() {
string_vec.push(self.data.longitude.unwrap().to_string())
if let Some(longitude) = self.data.longitude {
string_vec.push(longitude.to_string())
}
if self.data.city.is_some() {
string_vec.push(self.data.city.clone().unwrap().to_string())
if let Some(city) = &self.data.city {
string_vec.push(city.to_string())
}
}
let is_day = self.extract_day();
if is_day.is_some() {
string_vec.push(is_day.unwrap());
if let Some(is_day) = is_day {
string_vec.push(is_day);
}
let temperature = self.extract_temperature();
if temperature.is_some() {
string_vec.push(temperature.unwrap());
if let Some(temperature) = temperature {
string_vec.push(temperature);
}
let windspeed = self.extract_wind_speed();
if windspeed.is_some() {
string_vec.push(windspeed.unwrap());
if let Some(windspeed) = windspeed {
string_vec.push(windspeed);
}
let winddirection = self.extract_wind_direction();
if winddirection.is_some() {
string_vec.push(winddirection.unwrap());
if let Some(winddirection) = winddirection {
string_vec.push(winddirection);
}
if DataFormat::Normal == self.format
@ -90,14 +79,26 @@ impl CurrentWeatherOutput {
}
if DataFormat::Clean == self.format {
let final_string = string_vec.join(",");
final_string
write!(f, "{}", string_vec.join(","))
} else {
string_vec.push(self.create_footer());
string_vec.join("\n")
write!(f, "{}", string_vec.join("\n"))
}
}
}
}
impl CurrentWeatherOutput {
pub fn new(
format: DataFormat,
temperature_unit: TempUnit,
speed_unit: SpeedUnit,
) -> CurrentWeatherOutput {
CurrentWeatherOutput {
format, temperature_unit, speed_unit,
data: CurrentWeatherOutputModel::new(),
}
}
fn create_header(&self) -> String {
let mut title_header: Vec<String> = Vec::new();
@ -166,15 +167,15 @@ impl CurrentWeatherOutput {
fn parse_custom_data(&self, data: &str, custom: &str) -> String {
if self.format == DataFormat::Clean {
format!("{data}")
data.to_string()
} else {
format!("{custom}")
custom.to_string()
}
}
fn parse_simple_data(&self, data: &str, descriptor: &str, end_text: Option<&str>) -> String {
if self.format == DataFormat::Clean {
format!("{data}")
data.to_string()
} else {
let end_text = end_text.unwrap_or("");
format!("{descriptor}: {data}{end_text}")

View File

@ -14,31 +14,3 @@ pub struct CurrentWeatherPrintParams {
pub include_city: bool,
pub format: DataFormat,
}
impl CurrentWeatherPrintParams {
pub fn new(
all: bool,
is_day: bool,
temperature: bool,
temperature_unit: TempUnit,
windspeed: bool,
speed_unit: SpeedUnit,
winddirection: bool,
include_coords: bool,
include_city: bool,
format: DataFormat,
) -> CurrentWeatherPrintParams {
CurrentWeatherPrintParams {
all,
is_day,
temperature,
temperature_unit,
windspeed,
speed_unit,
winddirection,
include_coords,
include_city,
format,
}
}
}

View File

@ -1,7 +1,7 @@
use std::{
fs::{create_dir_all, File},
io::{Read, Write},
path::PathBuf,
path::{Path, PathBuf},
};
use crate::ip_api::ip_location::IpLocation;
@ -60,15 +60,13 @@ pub fn get_from_cache(ip: &str) -> Option<IpLocation> {
}
}
fn is_in_cache(path: &PathBuf) -> bool {
fn is_in_cache(path: &Path) -> bool {
path.is_file()
}
fn get_cache_dir() -> Option<PathBuf> {
let dir = ProjectDirs::from("com", "midefos", "open-meteo-cli");
if dir.is_some() {
let project_dir = dir.unwrap();
if let Some(project_dir) = &dir {
let cache_dir = project_dir.cache_dir();
if !cache_dir.exists() {
let res = create_dir_all(cache_dir);
@ -76,7 +74,6 @@ fn get_cache_dir() -> Option<PathBuf> {
return None;
}
}
Some(cache_dir.to_owned())
} else {
None

View File

@ -6,7 +6,7 @@ mod current_weather_output_model;
mod current_weather_print_params;
mod ifconfig {
pub mod ifconfig;
pub mod ifconfig_api;
}
mod formats {
@ -16,14 +16,14 @@ mod formats {
}
mod ip_api {
pub mod ip_api;
pub mod ip_api_api;
pub mod ip_location;
pub mod ip_location_cache;
}
mod open_meteo {
pub mod current_weather;
pub mod open_meteo;
pub mod open_meteo_api;
}
use billboard::{Alignment, Billboard};
@ -36,9 +36,9 @@ use cli::Arguments;
use coords::Coordinates;
use current_weather_extractor::CurrentWeatherExtractor;
use current_weather_print_params::CurrentWeatherPrintParams;
use ifconfig::ifconfig::extract_public_ip;
use ip_api::ip_api::extract_coords_and_city;
use open_meteo::open_meteo::request_current_weather;
use ifconfig::ifconfig_api::extract_public_ip;
use ip_api::ip_api_api::extract_coords_and_city;
use open_meteo::open_meteo_api::request_current_weather;
fn main() {
let opts = Arguments::from_args();
@ -98,8 +98,8 @@ fn main() {
let ip_location;
let cache_ip_location = get_from_cache(&ip);
if cache_ip_location.is_some() {
ip_location = cache_ip_location.unwrap();
if let Some(cache_ip_location) = cache_ip_location {
ip_location = cache_ip_location;
} else {
let ip_location_res = extract_coords_and_city(&ip);
ip_location = ip_location_res.unwrap_or_else(|_| {
@ -130,7 +130,7 @@ fn main() {
format = DataFormat::JSON;
}
let print_params = CurrentWeatherPrintParams::new(
let print_params = CurrentWeatherPrintParams {
all,
is_day,
temperature,
@ -141,7 +141,7 @@ fn main() {
include_coords,
include_city,
format,
);
};
let current_weather_printer =
CurrentWeatherExtractor::new(current_weather, print_params, coordinates, city);
@ -151,9 +151,9 @@ fn main() {
.text_alignment(Alignment::Left)
.box_alignment(Alignment::Left)
.build()
.eprint(output.to_string());
.print(output.to_string());
} else {
println!("{}", output.to_string());
println!("{}", output);
}
}
}