diff options
| author | Namilskyy <alive6863@gmail.com> | 2025-03-25 20:09:50 +0300 |
|---|---|---|
| committer | Namilskyy <alive6863@gmail.com> | 2025-03-25 20:09:50 +0300 |
| commit | 0ac84bd50407f2c4747a64b5340003849439fa52 (patch) | |
| tree | f0c905c04dc6bf0c21f6bd2e067c77a8a97f536a | |
| parent | f2ef0f00b48c11f12dc49b9d1e0c27b589e78d87 (diff) | |
Fixed errors and bugs.
| -rw-r--r-- | src/configmanager.rs | 23 | ||||
| -rw-r--r-- | src/main.rs | 82 | ||||
| -rw-r--r-- | 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<dyn std::error::Error>>{ - 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<Self, Box<dyn std::error::Error>> { + 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<dyn std::error::Error>> { + // Пока недоделано + 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<dyn std::error::Error>> { - let matches = Command::new("WeatherFetch") .version("0.1") .author("Borisov Alexey <arcanetmodl@gmail.com>") - .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::<String>("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(<int>, <int>)`, `lon(<int>, <int>)`."); + println!("HINT: And add `lat(<int>)`, `lon(<int>)`."); 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 |
