From 70562b9756ca8d06e12e66a15474701e363ffd70 Mon Sep 17 00:00:00 2001 From: Namilskyy Date: Mon, 17 Nov 2025 21:36:04 +0300 Subject: Some fixes and first building release --- src/parser.rs | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 51 insertions(+), 7 deletions(-) (limited to 'src/parser.rs') diff --git a/src/parser.rs b/src/parser.rs index 14fe992..f4fd2da 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1,14 +1,19 @@ use reqwest::Client; -use std::fs::File; +use std::fs::{self, File}; use std::io::Read; use toml; +use serde::Deserialize; +use serde_json; // use crate::configmanager::Config; use crate::shared::*; pub type BoxedError = Box; -const DEFAULT_PATH: &str = "/home/$USER/.config/WeatherFetch/Config.toml"; +fn get_config_path() -> Result> { + let home = std::env::var("HOME")?; + Ok(format!("{}/.config/WeatherFetch/Config.toml", home)) +} /* const CONF: Lazy>> = Lazy::new(|| { @@ -17,7 +22,7 @@ const CONF: Lazy>> = Laz */ pub fn get_location(coords_args: bool) -> Result<(), BoxedError> { - let config = get_config()?; + let config = get_config().unwrap(); if (config.lat == 0.0 || config.lon == 0.0) && !coords_args { @@ -39,17 +44,29 @@ pub async fn parse_weather() -> Result> .query(&[ ("latitude", config.lat.to_string()), ("longitude", config.lon.to_string()), - ("current", "temperature_2m,wind_speed_10m"), - ("hourly", "temperature_2m,relative_humidity_2m,wind_speed_10m"), + ("current", "temperature_2m,wind_speed_10m".to_string()), + ("hourly", "temperature_2m,relative_humidity_2m,wind_speed_10m".to_string()), ]) .send() .await?; - let weather_data: WeatherData = response.json().await?; + let response_text = response.text().await?; + + let weather_data: WeatherData = serde_json::from_str(&response_text)?; + + let home = std::env::var("HOME")?; + let cache_dir = format!("{}/.cache/WeatherFetch", home); + fs::create_dir_all(&cache_dir)?; + + let cache_path = format!("{}/weather.json", cache_dir); + let json_data = serde_json::to_string_pretty(&weather_data)?; + fs::write(&cache_path, json_data)?; + Ok(weather_data) } +#[derive(Debug, Deserialize)] pub struct Config { lat: f64, lon: f64, @@ -60,11 +77,38 @@ pub struct Config { } pub fn get_config() -> Result> { - let mut file = File::open(DEFAULT_PATH)?; + let config_path = get_config_path()?; + + if File::open(&config_path).is_err() { + generate_config()?; + } + + let mut file = File::open(&config_path)?; let mut content = String::new(); file.read_to_string(&mut content)?; let config: Config = toml::from_str(&content)?; Ok(config) +} + +pub fn generate_cachedir() -> Result<(), Box> { + let home = std::env::var("HOME")?; + let cache_path = format!("{}/.cache/WeatherFetch/", home); + + let _ = fs::create_dir(cache_path); + + Ok(()) +} + +pub fn generate_config() -> Result<(), Box> { + let config_path = get_config_path()?; + let config = "lat = 55.75\nlon = 37.62\nexclude = \"\"\nappid = \"\"\nunits = \"metric\"\nlang = \"ru\""; + let path = std::path::Path::new(&config_path); + if let Some(parent) = path.parent() { + fs::create_dir_all(parent)?; + } + fs::write(path, config)?; + println!("Config file generated at: {}", config_path); + Ok(()) } \ No newline at end of file -- cgit v1.2.3