weather_util_rust/
lib.rs

1#![allow(clippy::cast_possible_truncation)]
2#![allow(clippy::cast_precision_loss)]
3#![allow(clippy::cast_lossless)]
4#![allow(clippy::too_many_lines)]
5#![allow(clippy::module_name_repetitions)]
6#![allow(clippy::similar_names)]
7
8//! Utility to retreive and format weather data from openweathermap.org
9//!
10//! ```bash
11//! Please specify one of zipcode(country_code), city_name, or lat and lon.
12//!
13//! USAGE:
14//! weather-util-rust [OPTIONS]
15//!
16//! FLAGS:
17//! -h, --help       Prints help information
18//! -V, --version    Prints version information
19//!
20//! OPTIONS:
21//! -k, --api-key <api-key>              Api key (optional but either this or API_KEY environemnt variable must exist)
22//!     --city-name <city-name>          City Name (optional)
23//! -c, --country-code <country-code>    Country Code (optional), if not specified `us` will be assumed
24//!     --lat <lat>                      Latitude (must also specify Longitude)
25//!     --lon <lon>                      Longitude (must also specify Latitude)
26//! -z, --zipcode <zipcode>              Zipcode (optional)
27
28pub mod angle;
29/// Configuration data
30pub mod config;
31/// Direction in degrees
32pub mod direction;
33/// Distance in meters
34pub mod distance;
35/// Relative Humidity in percent
36pub mod humidity;
37/// Latitude
38pub mod latitude;
39/// Longitude
40pub mod longitude;
41/// Precipitation (rain/snow) in mm
42pub mod precipitation;
43/// Pressure module: conversions between hPa, kPa, Pa
44pub mod pressure;
45/// Speed as meters per second
46pub mod speed;
47/// Temperature module: conversions between Kelvin, Ceclius and Fahrenheit
48pub mod temperature;
49/// Serialize/Deserialize Unix Timetstamp to/from `DateTime`
50pub mod timestamp;
51/// Timezone offset as seconds before / after UTC
52pub mod timezone;
53/// Reqwest Client
54pub mod weather_api;
55/// Representation of Weather Data from openweathermap.org
56pub mod weather_data;
57/// Representation of Weather Forecast from openweathermap.org
58pub mod weather_forecast;
59/// CLI App Options and implementation
60pub mod weather_opts;
61
62/// `WeatherUtil` Error
63pub mod error;
64
65pub use error::Error;
66use time::{OffsetDateTime, macros::datetime};
67
68#[must_use]
69pub fn default_datetime() -> OffsetDateTime {
70    datetime!(1970-01-01 00:00:00).assume_utc()
71}
72
73#[cfg(feature = "stackstring")]
74use stack_string::{SmallString, StackString};
75#[cfg(feature = "stackstring")]
76pub type StringType = StackString;
77#[cfg(feature = "stackstring")]
78pub type ApiStringType = SmallString<32>;
79
80#[cfg(feature = "stackstring")]
81pub fn apistringtype_from_display(buf: impl std::fmt::Display) -> ApiStringType {
82    SmallString::from_display(buf)
83}
84
85#[cfg(not(feature = "stackstring"))]
86pub type StringType = String;
87#[cfg(not(feature = "stackstring"))]
88pub type ApiStringType = String;
89
90#[cfg(not(feature = "stackstring"))]
91pub fn apistringtype_from_display(buf: impl std::fmt::Display) -> ApiStringType {
92    format!("{buf}")
93}
94
95#[cfg(feature = "stackstring")]
96#[macro_export]
97macro_rules! format_string {
98    ($($arg:tt)*) => {
99        {
100            use std::fmt::Write;
101            let mut buf = stack_string::StackString::new();
102            std::write!(buf, "{}", std::format_args!($($arg)*)).unwrap();
103            buf
104        }
105    };
106}
107
108#[cfg(not(feature = "stackstring"))]
109#[macro_export]
110macro_rules! format_string {
111    ($($arg:tt)*) => {
112        {
113            use std::fmt::Write;
114            let mut buf = String::new();
115            std::write!(buf, "{}", std::format_args!($($arg)*)).unwrap();
116            buf
117        }
118    };
119}