use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum TemperatureUnit {
#[default]
Fahrenheit,
Celsius,
}
impl TemperatureUnit {
pub fn symbol(&self) -> &'static str {
match self {
Self::Fahrenheit => "°F",
Self::Celsius => "°C",
}
}
pub fn api_param(&self) -> &'static str {
match self {
Self::Fahrenheit => "fahrenheit",
Self::Celsius => "celsius",
}
}
pub fn format(&self, temp: f32) -> String {
format!("{:.0}{}", temp, self.symbol())
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum PressureUnit {
#[default]
Hpa,
InHg,
Psi,
}
impl PressureUnit {
pub fn symbol(&self) -> &'static str {
match self {
Self::Hpa => "hPa",
Self::InHg => "inHg",
Self::Psi => "PSI",
}
}
pub fn convert(&self, hpa: f32) -> f32 {
match self {
Self::Hpa => hpa,
Self::InHg => hpa / 33.8639,
Self::Psi => hpa / 68.9476,
}
}
pub fn format(&self, hpa: f32) -> String {
let value = self.convert(hpa);
match self {
Self::Hpa => format!("{:.0} {}", value, self.symbol()),
Self::InHg => format!("{:.2} {}", value, self.symbol()),
Self::Psi => format!("{:.1} {}", value, self.symbol()),
}
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum MeasurementSystem {
#[default]
Imperial,
Metric,
}
impl MeasurementSystem {
pub fn wind_speed_unit(&self) -> &'static str {
match self {
Self::Imperial => "mph",
Self::Metric => "km/h",
}
}
pub fn visibility_unit(&self) -> &'static str {
match self {
Self::Imperial => "mi",
Self::Metric => "km",
}
}
pub fn wind_speed_api_param(&self) -> &'static str {
match self {
Self::Imperial => "mph",
Self::Metric => "kmh",
}
}
pub fn convert_visibility(&self, meters: f32) -> f32 {
match self {
Self::Imperial => meters / 1609.34,
Self::Metric => meters / 1000.0,
}
}
}