diff --git a/owm_api25/src/config.rs b/owm_api25/src/config.rs index d31d426..7eecc06 100644 --- a/owm_api25/src/config.rs +++ b/owm_api25/src/config.rs @@ -3,16 +3,16 @@ use std::fs; #[derive(Debug, Deserialize)] pub struct General { - pub api_key: String, // Required for API query. Get yours for free from https://openweathermap.org - pub city_id: Option, // Any of these location parameters are required - pub city_name: Option, // Find City ID or Name from https://openweathermap.org/find? - pub lat: Option, // Latitude and Longitude must be used together + pub api_key: String, // Required for API query. Get yours for free from https://openweathermap.org + pub city_id: Option, // Any of these location parameters are required + pub city_name: Option, // Find City ID or Name from https://openweathermap.org/find? + pub lat: Option, // Latitude and Longitude must be used together pub lon: Option, - pub zip: Option, // Zip code + pub zip: Option, // Zip code #[serde(default = "default_units")] - pub units: String, // "metric", "imperial", "standard" (Default) + pub units: String, // "metric", "imperial", "standard" (Default) #[serde(default = "default_lang")] - pub lang: String, // Default: "en" + pub lang: String, // Default: "en" } #[derive(Debug, Deserialize)] @@ -29,6 +29,14 @@ fn default_lang() -> String { } impl Config { + /// Load configuration from `~/.config/candywidgets/openweathermap.toml`. + /// + /// # Errors + /// + /// Returns an error if: + /// - `$XDG_CONFIG_HOME` (or `~/.config`) cannot be determined, + /// - the file cannot be read, + /// - or the TOML cannot be parsed. pub fn load() -> Result> { let mut path = dirs::config_dir().ok_or("No config dir found")?; path.push("candywidgets/openweathermap.toml"); @@ -39,6 +47,12 @@ impl Config { Ok(toml::from_str(&toml_str)?) } + /// Build a query URL for the `OpenWeatherMap` API v2.5. + /// + /// # Errors + /// + /// Returns an error if no valid location parameter is set + /// (`city_id`, `city_name`, `lat`+`lon`, or `zip`). pub fn build_url(&self) -> Result { let base = "https://api.openweathermap.org/data/2.5/weather"; diff --git a/owm_api25/src/forecast.rs b/owm_api25/src/forecast.rs new file mode 100644 index 0000000..8d53cc9 --- /dev/null +++ b/owm_api25/src/forecast.rs @@ -0,0 +1,31 @@ +use serde::Deserialize; + +#[derive(Debug, Deserialize)] +pub struct ForecastWeather { + pub id: u32, + pub main: String, + pub description: String, + pub icon: String, +} + +#[derive(Debug, Deserialize)] +pub struct ForecastMain { + pub temp: f32, + pub pressure: f32, + pub humidity: u8, +} + +#[derive(Debug, Deserialize)] +pub struct ForecastEntry { + pub dt: u64, + pub main: ForecastMain, + pub weather: Vec, + pub dt_txt: String, +} + +#[derive(Debug, Deserialize)] +pub struct ForecastResponse { + pub cod: String, + pub cnt: u32, + pub list: Vec, +} diff --git a/owm_api25/src/lib.rs b/owm_api25/src/lib.rs index 844a605..fe6de35 100644 --- a/owm_api25/src/lib.rs +++ b/owm_api25/src/lib.rs @@ -1,27 +1,3 @@ pub mod config; - -use serde::Deserialize; - -#[derive(Debug, Deserialize)] -pub struct Weather { - pub main: String, - pub icon: String, -} - -#[derive(Debug, Deserialize)] -pub struct Main { - pub temp: f32, -} - -#[derive(Debug, Deserialize)] -pub struct Sys { - pub country: String, -} - -#[derive(Debug, Deserialize)] -pub struct WeatherResponse { - pub name: String, - pub sys: Sys, - pub weather: Vec, - pub main: Main, -} +pub mod forecast; +pub mod weather; diff --git a/owm_api25/src/weather.rs b/owm_api25/src/weather.rs new file mode 100644 index 0000000..a544cfc --- /dev/null +++ b/owm_api25/src/weather.rs @@ -0,0 +1,30 @@ +use serde::Deserialize; + +#[derive(Debug, Deserialize)] +pub struct Weather { + pub main: String, + pub icon: String, + pub description: String, +} + +#[derive(Debug, Deserialize)] +pub struct Main { + pub temp: f32, + pub pressure: u32, + pub humidity: u8, +} + +#[derive(Debug, Deserialize)] +pub struct Sys { + pub country: String, + pub sunrise: u64, + pub sunset: u64, +} + +#[derive(Debug, Deserialize)] +pub struct WeatherResponse { + pub name: String, + pub sys: Sys, + pub weather: Vec, + pub main: Main, +} diff --git a/widget/src/main.rs b/widget/src/main.rs index 5bf20e2..e6b4266 100644 --- a/widget/src/main.rs +++ b/widget/src/main.rs @@ -1,4 +1,4 @@ -use owm_api25::{WeatherResponse, config::Config}; +use owm_api25::{config::Config, weather::WeatherResponse}; use reqwest::blocking; fn main() -> Result<(), Box> {