summaryrefslogtreecommitdiff
path: root/src/parser.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/parser.rs')
-rw-r--r--src/parser.rs147
1 files changed, 27 insertions, 120 deletions
diff --git a/src/parser.rs b/src/parser.rs
index ddf3b0a..33c679d 100644
--- a/src/parser.rs
+++ b/src/parser.rs
@@ -3,129 +3,17 @@ use reqwest::{Client};
use chrono::{DateTime, Utc, prelude::*};
use serde::{Serialize, Deserialize};
use once_cell::sync::Lazy;
-use std::error::Error;
+use std::{error::Error, fs::{read, File}, io::Read};
+use toml::Deserializer;
-use crate::configmanager::Config;
+// use crate::configmanager::Config;
+mod shared;
+use crate::shared::*;
-//API answer struct`s
-#[derive(Debug, Serialize, Deserialize)]
-pub struct WeatherData {
- pub lat: f64,
- pub lon: f64,
- pub timezone: String,
- pub timezone_offset: i32,
- pub current: Current,
- 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,
- pub sunrise: u64,
- pub sunset: u64,
- pub temp: f32,
- pub feels_like: f32,
- pub pressure: u32,
- pub humidity: u32,
- pub dew_point: f32,
- pub uvi: f32,
- pub clouds: u32,
- pub visibility: u32,
- pub wind_speed: f32,
- pub wind_deg: u32,
- pub wind_gust: f32,
- pub weather: Vec<Weather>,
-}
-
-#[derive(Debug, Serialize, Deserialize)]
-pub struct Weather {
- pub id: u32,
- pub main: String,
- pub description: String,
- pub icon: String,
-}
-
-#[derive(Debug, Serialize, Deserialize)]
-pub struct Minutely {
- pub dt: u64,
- pub precipitation: f32,
-}
-
-#[derive(Debug, Serialize, Deserialize)]
-pub struct Hourly {
- pub dt: u64,
- pub temp: f32,
- pub feels_like: f32,
- pub pressure: u32,
- pub humidity: u32,
- pub dew_point: f32,
- pub uvi: f32,
- pub clouds: u32,
- pub visibility: u32,
- pub wind_speed: f32,
- pub wind_deg: u32,
- pub wind_gust: f32,
- pub weather: Vec<Weather>,
- pub pop: f32,
-}
-
-#[derive(Debug, Serialize, Deserialize)]
-pub struct Daily {
- pub dt: u64,
- pub sunrise: u64,
- pub sunset: u64,
- pub moonrise: u64,
- pub moonset: u64,
- pub moon_phase: f32,
- pub summary: String,
- pub temp: Temp,
- pub feels_like: FeelsLike,
- pub pressure: u32,
- pub humidity: u32,
- pub dew_point: f32,
- pub wind_speed: f32,
- pub wind_deg: u32,
- pub wind_gust: f32,
- pub weather: Vec<Weather>,
- pub clouds: u32,
- pub pop: f32,
- pub rain: Option<f32>,
- pub uvi: f32,
-}
-
-#[derive(Debug, Serialize, Deserialize)]
-pub struct Temp {
- pub day: f32,
- pub min: f32,
- pub max: f32,
- pub night: f32,
- pub eve: f32,
- pub morn: f32,
-}
-
-#[derive(Debug, Serialize, Deserialize)]
-pub struct FeelsLike {
- pub day: f32,
- pub night: f32,
- pub eve: f32,
- pub morn: f32,
-}
+//fftype BoxedError = Box<dyn std::error::Error + Send + Sync>;
-#[derive(Debug, Serialize, Deserialize)]
-pub struct Alert {
- pub sender_name: String,
- pub event: String,
- pub start: u64,
- pub end: u64,
- pub description: String,
- pub tags: Vec<String>,
-}
-
-//type BoxedError = Box<dyn std::error::Error + Send + Sync>;
+const DEFAULT_PATH: &str = "/home/$USER/.config/WeatherFetch/Config.toml";
-
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>)
});
@@ -171,4 +59,23 @@ pub async fn parse_weather() -> Result<WeatherData, Box<dyn std::error::Error>>
let weather_data: WeatherData = response.json().await?;
Ok(weather_data)
-} \ No newline at end of file
+}
+
+pub struct Config {
+ lat: f64,
+ lon: f64,
+ exclude: String,
+ appid: String,
+ units: String,
+ lang: String,
+}
+
+pub fn get_config() -> Result<Config, dyn std::error::Error> {
+ let mut file = read(DefaultConfig)?;
+ let mut content: String = file.read_to_string(&mut contents)?;
+
+ let mut deserialized_cfg = toml::Deserializer(&content)?;
+ let config: Config = Deserialize::deserialize(&mut deserialized_cfg)?;
+
+ Ok(config)
+ } \ No newline at end of file