use std::fmt::Display;
use serde::Deserialize;
#[derive(Debug, Clone, Copy, Deserialize, PartialEq, Eq, Hash)]
#[serde(rename_all(deserialize = "lowercase"))]
pub enum UnitSystem {
Metric,
Standard,
Imperial,
}
impl Display for UnitSystem {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
UnitSystem::Standard => write!(f, "standard"),
UnitSystem::Metric => write!(f, "metric"),
UnitSystem::Imperial => write!(f, "imperial"),
}
}
}
#[derive(Debug, Deserialize, Clone)]
pub struct City {
pub name: String,
pub country_code: String,
pub display_name: Option<String>,
}
impl City {
pub fn new(name: &str, country_code: &str) -> City {
City {
name: name.to_string(),
country_code: country_code.to_string(),
display_name: None,
}
}
}
impl Display for City {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match &self.display_name {
Some(display_name) => write!(f, "{display_name}"),
None => write!(f, "{}, {}", self.name, self.country_code),
}
}
}
#[derive(Debug, Deserialize, Clone)]
pub struct CityId {
pub id: u32,
pub display_name: Option<String>,
}
impl CityId {
pub fn new(id: u32) -> CityId {
CityId { id, display_name: None }
}
}
impl Display for CityId {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match &self.display_name {
Some(display_name) => write!(f, "{display_name}"),
None => write!(f, "{}", self.id),
}
}
}
#[derive(Debug, Deserialize, Clone)]
pub struct Coord {
pub lat: f64,
pub lon: f64,
pub display_name: Option<String>,
}
impl Coord {
pub fn new(lat: f64, lon: f64) -> Coord {
Coord {
lat,
lon,
display_name: None,
}
}
}
impl Display for Coord {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match &self.display_name {
Some(display_name) => write!(f, "{display_name}"),
None => write!(f, "lat={}, lon={}", self.lat, self.lon),
}
}
}
#[derive(Debug, Deserialize)]
pub struct CurrentWeather {
pub coord: Coord,
pub weather: Vec<Weather>,
pub base: String,
pub main: Main,
pub visibility: Option<u16>,
pub wind: Wind,
pub clouds: Clouds,
pub rain: Option<PrecipVolume>,
pub snow: Option<PrecipVolume>,
pub dt: i64,
pub sys: Sys,
pub timezone: i64,
pub id: u64,
pub name: String,
pub cod: u64,
}
#[derive(Debug, Deserialize)]
pub struct Weather {
pub id: u64,
pub main: String,
pub description: String,
pub icon: String,
}
#[derive(Debug, Deserialize)]
pub struct Main {
pub temp: f64,
pub feels_like: f64,
pub temp_min: f64,
pub temp_max: f64,
pub pressure: f64,
pub sea_level: Option<f64>,
pub grnd_level: Option<f64>,
pub humidity: f64,
}
#[derive(Debug, Deserialize)]
pub struct Wind {
pub speed: f64,
pub deg: f64,
pub gust: Option<f64>,
}
#[derive(Debug, Deserialize)]
pub struct Clouds {
#[serde(rename(deserialize = "all"))]
pub cloudiness: f64,
}
#[derive(Debug, Deserialize)]
pub struct PrecipVolume {
#[serde(rename(deserialize = "1h"))]
pub one_hour: Option<f64>,
#[serde(rename(deserialize = "3h"))]
pub three_hour: Option<f64>,
}
#[derive(Debug, Deserialize)]
pub struct Sys {
pub type_: Option<u64>,
pub id: Option<u64>,
pub message: Option<f64>,
pub country: Option<String>,
pub sunrise: i64,
pub sunset: i64,
}