diff options
| author | Namilskyy <alive6863@gmail.com> | 2025-03-26 21:23:12 +0300 |
|---|---|---|
| committer | Namilskyy <alive6863@gmail.com> | 2025-03-26 21:23:31 +0300 |
| commit | d075958732b9deb6a6f39dd1171144e075f013dc (patch) | |
| tree | 741962f319d6c633d71b932dc56bebc9ce37b236 /src/parser.rs | |
| parent | 3d642eae40c80b31c5078d4b1d62a0a533f230d2 (diff) | |
Fixing errors, the zsh: command not found: main branch will pushed only after fixing all mistakes and bugs. Now founded 3.]
Diffstat (limited to 'src/parser.rs')
| -rw-r--r-- | src/parser.rs | 66 |
1 files changed, 44 insertions, 22 deletions
diff --git a/src/parser.rs b/src/parser.rs index 5a670b1..e97db13 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1,8 +1,11 @@ -use reqwest::{Error, Client, get}; +//use clap::error::ErrorKind; +use reqwest::{Client}; use chrono::{DateTime, Utc, prelude::*}; use serde::{Serialize, Deserialize}; +use once_cell::sync::Lazy; +use std::error::Error; -use crate::configmanager; +use crate::configmanager::Config; //API answer struct`s #[derive(Debug, Serialize, Deserialize)] @@ -12,12 +15,11 @@ pub struct WeatherData { pub timezone: String, pub timezone_offset: i32, pub current: Current, - pub minutely: Vec<Minutely>, - pub hourly: Vec<Hourly>, - pub daily: Vec<Daily>, - pub alerts: Vec<Alert>, + pub minutely: Option<Vec<Minutely>>, + pub hourly: Option<Vec<Hourly>>, + pub daily: Option<Vec<Daily>>, + pub alerts: Option<Vec<Alert>>, } - #[derive(Debug, Serialize, Deserialize)] pub struct Current { pub dt: u64, @@ -121,26 +123,46 @@ pub struct Alert { pub tags: Vec<String>, } +type BoxedError = Box<dyn std::error::Error + Send + Sync>; + +const CONF: Lazy<Result<Config, Box<dyn std::error::Error + Send + Sync>>> = Lazy::new(|| { + Config::load().map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send + Sync>) +}); +pub fn get_location(coords_args: bool) -> Result<(), BoxedError> { + let config = CONF + .as_ref() + .map_err(|e| Box::<dyn std::error::Error + Send + Sync>::from(e.to_string()))? + .clone(); -pub fn get_location(coords_args: bool) -> Result<(), String>{ - //Get the lat and lon for API call - let conf: Option<configmanager::load()>; - if conf.lat.is_empty() || conf.lon.is_empty() && !coords_args{ - println!("No coordinates in configuration file or conf not founded."); - println!("HINT: Try create ~/.config/WeatherFetch/Config.toml"); - println!("HINT: And add `lat(<int>)`, `lon(<int>)`."); - println!("HINT: To get more info check https://openweathermap.org/api/one-call-3"); - - Err("No coordinates in config or args.".into()) + if (config.lat == 0.0 || config.lon == 0.0) && !coords_args { + Err("Invalid coordinates in config".into()) } else { Ok(()) } -} - - -pub async fn parse_weather(_config: &Config) -> Result<(), reqwest::Error> { - Ok(()) } + +pub async fn parse_weather() -> Result<WeatherData, Box<dyn std::error::Error>> { + let config = CONF + .as_ref() + .map_err(|e| Box::<dyn std::error::Error>::from(e.to_string()))? + .clone(); + + let client = Client::new(); + let response = client.get("https://api.openweathermap.org/data/3.0/onecall") + .query(&[ + ("lat", config.lat.to_string()), + ("lon", config.lon.to_string()), + ("exclude", config.exclude), + ("appid", config.appid), + ("units", config.units), + ("lang", config.lang), + ]) + .send() + .await?; + + let weather_data: WeatherData = response.json().await?; + Ok(weather_data) +}
\ No newline at end of file |
