From d075958732b9deb6a6f39dd1171144e075f013dc Mon Sep 17 00:00:00 2001 From: Namilskyy Date: Wed, 26 Mar 2025 21:23:12 +0300 Subject: Fixing errors, the zsh: command not found: main branch will pushed only after fixing all mistakes and bugs. Now founded 3.] --- src/parser.rs | 66 +++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 44 insertions(+), 22 deletions(-) (limited to 'src/parser.rs') 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, - pub hourly: Vec, - pub daily: Vec, - pub alerts: Vec, + pub minutely: Option>, + pub hourly: Option>, + pub daily: Option>, + pub alerts: Option>, } - #[derive(Debug, Serialize, Deserialize)] pub struct Current { pub dt: u64, @@ -121,26 +123,46 @@ pub struct Alert { pub tags: Vec, } +type BoxedError = Box; + +const CONF: Lazy>> = Lazy::new(|| { + Config::load().map_err(|e| Box::new(e) as Box) +}); +pub fn get_location(coords_args: bool) -> Result<(), BoxedError> { + let config = CONF + .as_ref() + .map_err(|e| Box::::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; - 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()`, `lon()`."); - 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> { + let config = CONF + .as_ref() + .map_err(|e| Box::::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 -- cgit v1.2.3