Correct stdout, updating lib and some clippy clean
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
+37 -36
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}")