From 0ac84bd50407f2c4747a64b5340003849439fa52 Mon Sep 17 00:00:00 2001 From: Namilskyy Date: Tue, 25 Mar 2025 20:09:50 +0300 Subject: Fixed errors and bugs. --- src/configmanager.rs | 23 ++++++++++++--- src/main.rs | 82 +++++++++++++++------------------------------------- src/parser.rs | 17 +++++++---- 3 files changed, 55 insertions(+), 67 deletions(-) diff --git a/src/configmanager.rs b/src/configmanager.rs index c124419..fb8ccc1 100644 --- a/src/configmanager.rs +++ b/src/configmanager.rs @@ -1,5 +1,7 @@ use std::fs; use serde::Deserialize; +use dirs::home_dir; +use std::path::PathBuf; impl WeatherFetch{ #[derive(Deserialize)] @@ -10,19 +12,32 @@ pub struct Config { appid: String, //Your OpenWeatherMap API key lang: String, //Output language units: String, //Units of measurement - cache: bool //Cacheing next Weather to dont use internet in next call + cache: bool, //Cacheing next Weather to dont use internet in next call rain: String, //Path to rain image (png/jpg) or ASCII art int .txt sunny: String, //Path to sunny image (png/jpg) or ASCII art int .txt snowy: String, } //More info: OpenWeatherMap.org/api/one-call3 -pub fn handle_config() -> Result<(), Box>{ - let Config_str = fs::read_to_string("~/.config/WeatherFetch/Config.toml"); - let Config_parse: Config = toml::from_str(&data)?.except(); +impl Config{ + pub fn load() -> Result> { + let mut path = home_dir().ok_or("Не удалось найти домашнюю директорию")?; + path.push(".config/WeatherFetch/Config.toml"); + + let config_str = fs::read_to_string(path)?; + let config: Config = toml::from_str(&config_str)?; + Ok(config) + } } + +pub fn handle_config(config: &Config) -> Result<(), Box> { + // Пока недоделано + Ok(()) } pub fn gen_standart_conf() { } + +} + diff --git a/src/main.rs b/src/main.rs index 73c8403..568ef9a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,76 +11,42 @@ mod configmanager; use parser::{parse_weather, Weather}; use configmanager::{Config, handle_config}; +f// main.rs +use clap::{Arg, Command}; +use termimage::Image; + +mod configmanager; +mod parser; + +use configmanager::{Config, handle_config}; + fn main() -> Result<(), Box> { - let matches = Command::new("WeatherFetch") .version("0.1") .author("Borisov Alexey ") - .about("Weather fetch like fastfetch with image and ASCII art support. Just useless pet project.") - .arg( - Arg::new("image") - .short("i") - .long("image") - .value_name("PATH") - .help("Path to image file") - .takes_value(true), - ) - .arg( - Arg::new("exclude") - .short("e") - .long("exclude") - .value_name("TYPE") - .possible_values(["current", "minutely", "hourly", "daily", "alerts"]) - .help("Exclude specific weather data type") - .takes_value(true), - ) - .arg( - Arg::new("help") - .short("h") - .long("help") - .help("Print help information") - .takes_value(false), - ) - .arg( - Arg::new(lat) - .short("lt") - .long("lat") - .value_name("TWO DIGITS") - .help("Coordinates for weather API call. Latitude.") - .takes_value(true) - ) - .arg( - Arg::new("lon") - .short("ln") - .long("lon") - .value_name("TWO DIGITS") - .help("Coordinates for weather API call. Latitude.") - .takes_value(true) - ) + .about("Weather fetch like fastfetch with image and ASCII art support") + .arg(Arg::new("image").short('i').long("image").value_name("PATH")) + .arg(Arg::new("exclude") + .short('e') + .long("exclude") + .value_name("TYPE") + .possible_values(["current", "minutely", "hourly", "daily", "alerts"])) + .arg(Arg::new("help").short('h').long("help")) + .arg(Arg::new("lat").short('t').long("lat").value_name("LATITUDE")) + .arg(Arg::new("lon").short('n').long("lon").value_name("LONGITUDE")) .get_matches(); - - if matches.is_present("help") { - + if matches.contains_id("help") { + println!("Usage: ..."); return Ok(()); } - - if let Some(img_path) = matches.value_of("image") { - let img = Image::from_path(img_path)?; - + if let Some(img_path) = matches.get_one::("image") { + let _img = Image::from_path(img_path)?; } - - if let Some(exclude_type) = matches.value_of("exclude") { - - - } - - let config = Config::load()?; handle_config(&config)?; Ok(()) -} - +} \ No newline at end of file diff --git a/src/parser.rs b/src/parser.rs index 62af697..f9976e6 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -130,17 +130,24 @@ pub struct Alert { -pub fn get_location(let ip: String) -> Reslut<(), >{ +pub fn get_location(let coords_args: bool, config: &Config) -> Reslut<(), >{ //Get the lat and lon for API call - if configmanager::Config.lat.is_empty() || configmanager::Config.lon.is_empty(){ + configmanager::handle_config(); + if configmanager::Config.lat.is_empty() || configmanager::Config.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: 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()) + } else { + Ok(()) } } -async pub fn parse_wheather() -> Result<(), Error>{ -} +pub async fn parse_weather(config: &Config) -> Result<(), reqwest::Error> { + + Ok(()) +} \ No newline at end of file -- cgit v1.2.3