Skip to main content

openmeteo_rs/
units.rs

1//! Request-side unit and location-selection types.
2
3use std::borrow::Cow;
4
5use crate::query::AsApiStr;
6
7/// Temperature unit requested from the API.
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize)]
9#[non_exhaustive]
10pub enum TemperatureUnit {
11    /// Celsius.
12    Celsius,
13    /// Fahrenheit.
14    Fahrenheit,
15}
16
17impl AsApiStr for TemperatureUnit {
18    fn as_api_str(&self) -> Cow<'static, str> {
19        Cow::Borrowed(match self {
20            Self::Celsius => "celsius",
21            Self::Fahrenheit => "fahrenheit",
22        })
23    }
24}
25
26/// Wind-speed unit requested from the API.
27#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize)]
28#[non_exhaustive]
29pub enum WindSpeedUnit {
30    /// Kilometres per hour.
31    Kmh,
32    /// Metres per second.
33    Ms,
34    /// Miles per hour.
35    Mph,
36    /// Knots.
37    Kn,
38}
39
40impl AsApiStr for WindSpeedUnit {
41    fn as_api_str(&self) -> Cow<'static, str> {
42        Cow::Borrowed(match self {
43            Self::Kmh => "kmh",
44            Self::Ms => "ms",
45            Self::Mph => "mph",
46            Self::Kn => "kn",
47        })
48    }
49}
50
51/// Length unit requested from marine APIs.
52#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize)]
53#[non_exhaustive]
54pub enum LengthUnit {
55    /// Metric units.
56    Metric,
57    /// Imperial units.
58    Imperial,
59}
60
61impl AsApiStr for LengthUnit {
62    fn as_api_str(&self) -> Cow<'static, str> {
63        Cow::Borrowed(match self {
64            Self::Metric => "metric",
65            Self::Imperial => "imperial",
66        })
67    }
68}
69
70/// Precipitation unit requested from the API.
71#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize)]
72#[non_exhaustive]
73pub enum PrecipitationUnit {
74    /// Millimetres.
75    Mm,
76    /// Inches.
77    Inch,
78}
79
80impl AsApiStr for PrecipitationUnit {
81    fn as_api_str(&self) -> Cow<'static, str> {
82        Cow::Borrowed(match self {
83            Self::Mm => "mm",
84            Self::Inch => "inch",
85        })
86    }
87}
88
89/// Timestamp format requested from the API.
90#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize)]
91#[non_exhaustive]
92pub enum TimeFormat {
93    /// ISO-8601 local timestamps.
94    Iso8601,
95    /// Unix timestamps.
96    UnixTime,
97}
98
99impl AsApiStr for TimeFormat {
100    fn as_api_str(&self) -> Cow<'static, str> {
101        Cow::Borrowed(match self {
102            Self::Iso8601 => "iso8601",
103            Self::UnixTime => "unixtime",
104        })
105    }
106}
107
108/// Timezone requested from the API.
109#[derive(Debug, Clone, PartialEq, Eq, Hash, serde::Serialize)]
110#[non_exhaustive]
111pub enum Timezone {
112    /// Let Open-Meteo resolve the timezone from the coordinate.
113    Auto,
114    /// Greenwich Mean Time.
115    Gmt,
116    /// IANA timezone name, for example `Europe/Zurich`.
117    Iana(String),
118}
119
120impl AsApiStr for Timezone {
121    fn as_api_str(&self) -> Cow<'static, str> {
122        match self {
123            Self::Auto => Cow::Borrowed("auto"),
124            Self::Gmt => Cow::Borrowed("GMT"),
125            Self::Iana(value) => Cow::Owned(value.clone()),
126        }
127    }
128}
129
130/// Grid-cell selection strategy near coastlines.
131#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize)]
132#[non_exhaustive]
133pub enum CellSelection {
134    /// Prefer land cells.
135    Land,
136    /// Prefer sea cells.
137    Sea,
138    /// Use the nearest cell.
139    Nearest,
140}
141
142impl AsApiStr for CellSelection {
143    fn as_api_str(&self) -> Cow<'static, str> {
144        Cow::Borrowed(match self {
145            Self::Land => "land",
146            Self::Sea => "sea",
147            Self::Nearest => "nearest",
148        })
149    }
150}