From 26092b2043649a466d07fbb87078adc8c8612621 Mon Sep 17 00:00:00 2001 From: Namilskyy Date: Tue, 21 Oct 2025 18:11:49 +0300 Subject: Started refactoring bad code. Edited: `parser.rs`, `main.rs`, switched weather-structs to `shared.rs` --- src/ASCII.yaml | 13 ----- src/arts.json | 5 ++ src/configmanager.rs | 46 ---------------- src/main.rs | 11 +++- src/parser.rs | 147 ++++++++++----------------------------------------- src/shared.rs | 115 ++++++++++++++++++++++++++++++++++++++++ src/weather.yaml | 74 ++++++++++++++++++++++++++ 7 files changed, 230 insertions(+), 181 deletions(-) delete mode 100644 src/ASCII.yaml create mode 100644 src/arts.json delete mode 100644 src/configmanager.rs create mode 100644 src/shared.rs create mode 100644 src/weather.yaml diff --git a/src/ASCII.yaml b/src/ASCII.yaml deleted file mode 100644 index 53ff724..0000000 --- a/src/ASCII.yaml +++ /dev/null @@ -1,13 +0,0 @@ -#For ascii arts -Weather: - Sunny: - - - - - Rain: - - - - - Alert: diff --git a/src/arts.json b/src/arts.json new file mode 100644 index 0000000..27fdbe5 --- /dev/null +++ b/src/arts.json @@ -0,0 +1,5 @@ +arts { + weather: { + cold = "" + }; +} diff --git a/src/configmanager.rs b/src/configmanager.rs deleted file mode 100644 index c005d2c..0000000 --- a/src/configmanager.rs +++ /dev/null @@ -1,46 +0,0 @@ -use std::fs; -use serde::Deserialize; -use dirs::home_dir; - -#[derive(Debug, Deserialize, Clone)] -pub struct Config { - pub lat: f64, - pub lon: f64, - pub exclude: String, - pub appid: String, - pub lang: String, - pub units: String, - pub cache: bool, - pub rain: String, - pub sunny: String, - pub snowy: String, -} - -type BoxedError = Box; - - - -pub fn handle_config(_config: &Config) -> Result<(), Box> { - Ok(()) -} - -pub fn gen_standard_conf() { - // TODO: Implement -} - -impl Config { - pub fn load() -> Result> { - let mut path = home_dir().ok_or("Home directory not found")?; - path.push(".config/WeatherFetch/Config.toml"); - - let config_str = fs::read_to_string(&path) - .map_err(|e| format!("Failed to read config: {}", e))?; - - let config: Config = toml::from_str(&config_str) - .map_err(|e| format!("Invalid TOML: {}", e))?; - - Ok(config) - } -} - - diff --git a/src/main.rs b/src/main.rs index db40b5c..ffa33d3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -35,10 +35,17 @@ extern crate image; use clap::{Arg, Command}; use termimage::{Options}; -mod configmanager; mod parser; +use parser::{get_config, Config}; + +// use crate::configmanager::{Config, handle_config}; + +fn process_config() -> Result> { + get_config(); + + Ok(Config::load()?) +} -use crate::configmanager::{Config, handle_config}; fn main() -> Result<(), Box> { let config = Config::load()?; 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>, - pub hourly: Option>, - pub daily: Option>, - pub alerts: Option>, -} -#[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, -} - -#[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, - 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, - pub clouds: u32, - pub pop: f32, - pub rain: Option, - 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; -#[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, -} - -//type BoxedError = Box; +const DEFAULT_PATH: &str = "/home/$USER/.config/WeatherFetch/Config.toml"; - const CONF: Lazy>> = Lazy::new(|| { Config::load().map_err(|e| Box::new(e) as Box) }); @@ -171,4 +59,23 @@ pub async fn parse_weather() -> Result> 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 { + 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 diff --git a/src/shared.rs b/src/shared.rs new file mode 100644 index 0000000..1afee09 --- /dev/null +++ b/src/shared.rs @@ -0,0 +1,115 @@ +//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>, + pub hourly: Option>, + pub daily: Option>, + pub alerts: Option>, +} +#[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, +} + +#[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, + 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, + pub clouds: u32, + pub pop: f32, + pub rain: Option, + 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, +} + +#[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, +} \ No newline at end of file diff --git a/src/weather.yaml b/src/weather.yaml new file mode 100644 index 0000000..36078d3 --- /dev/null +++ b/src/weather.yaml @@ -0,0 +1,74 @@ +Weather: + snowy: + + + () + /\ + //\\ + << >> + () \\// () + ()._____ /\ \\ /\ _____.() + \.--.\ //\\ //\\ //\\ /.--./ + \\__\\/__\//__\//__\\/__// + '--/\\--//\--//\--/\\--' + \\\\///\\//\\\//// + ()-= >>\\< <\\> >\\<< =-() + ////\\\//\\///\\\\ + .--\\/--\//--\//--\//--. + //""/\\""//\""//\""//\""\\ + /'--'/ \\// \\// \\// \'--'\ + ()`"""` \/ // \/ `""""`() + () //\\ () + << >> + jgs \\// + \/ + () + +sunny: + + + / -/ , + # .# -#. + # #, -##. + # ,# %## + # -## /## A + #, ### ### + ##/ ;## ### U + , ####+ ##- ### . + % ,##### ;##- :### -#####/ R + :% ;### ### ##### :##+, + ,#; ###::/########## ,### I + ##/..,- ####/, .-:###; ###= + ,#######: ##= .%#+##### N + ,#######=++ -#####= + ####; .##; K + ##/ ;###% ,%##%- ,# + % ,#:-=##= ,#/::+#= +;=. %###= O + - #. , ###:;#######= + =; +###%# -####, -+ +###- #######; -:=. ? + =#+ :####### -,## ;: ,##%. +#####, + -########### . /: . /#//: ! + .#####:/##% // / + % /+ /- ! + # :# ##/ ,- + /##. +# ########+ 1 + =####/ %## /#########+ + ,######, .; +##%- +#= + ######## :#+#%###/ # =#. + ,%### -## .;#/, ### /. 1 + .:+######% .###. #/%: -#### + =//- =#####+. =, ,#; %###= + #########: ./### #####: + #####% ########### +####= + #### #####% ### /#% + ###/ ##### ### ## + .###. #### /###, ,# + -###= #### %##+ # + -###%, ####. %## -- + ;:. ;###= ## . + .###= +# -joil'99 + =### =/ + ### ,. + ;#: + #. + - + \ No newline at end of file -- cgit v1.2.3