From ed148b05cbeea9b881194705b38bb581fb989ad3 Mon Sep 17 00:00:00 2001 From: Benjamyn Love Date: Sat, 9 Mar 2024 23:01:29 +1100 Subject: [PATCH] Fixed config parsing --- src/config.rs | 53 ++++++++++++++++++++++++++++++++++++++------------- src/main.rs | 9 ++++----- 2 files changed, 44 insertions(+), 18 deletions(-) diff --git a/src/config.rs b/src/config.rs index 5bc2a89..f1d2f6a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,7 +1,6 @@ use configparser::ini::Ini; use core::fmt; -use std::fs::File; -use std::io::prelude::*; +use std::env::VarError; use untildify; @@ -35,6 +34,18 @@ pub enum MonitorType { Ultrawide, Horizontal, Vertical, + Error, +} + +impl MonitorType { + fn new(mon_type: String) -> MonitorType { + match mon_type.as_str() { + "ultrawide" => MonitorType::Ultrawide, + "vertical" => MonitorType::Vertical, + "horizontal" => MonitorType::Horizontal, + _ => MonitorType::Error, + } + } } impl fmt::Display for MonitorType { @@ -50,9 +61,11 @@ pub struct Monitors { impl Monitors { pub fn new() -> Monitors { - Monitors { - monitors: vec![MonitorType::Vertical], - } + Monitors { monitors: vec![] } + } + + fn add(&mut self, mon_type: MonitorType) { + self.monitors.push(mon_type); } } @@ -60,7 +73,7 @@ impl fmt::Display for Monitors { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "[")?; for v in &self.monitors { - write!(f, "{}", v)?; + write!(f, "{}, ", v)?; } write!(f, "]") } @@ -122,10 +135,9 @@ impl fmt::Display for Config { } impl Config { - pub fn parse_config(self, file_path: String) -> Config { + pub fn from_config(file_path: String) -> Config { let mut config = Ini::new(); let map = config.load(file_path).unwrap(); - // let file_contents = Config::load_from_file(file_path); // Get the wallpaper engine and parse it let wallpaper_engine = @@ -134,20 +146,35 @@ impl Config { // Get the wallpaper dirs and parse them + let ultrawide_dir = config.get("general", "ultra_wall_dir").unwrap(); + let horizontal_dir = config.get("general", "hor_wall_dir").unwrap(); + let vert_wall_dir = config.get("general", "vert_wall_dir").unwrap(); + let wallpaper_dir = WallpaperDir { + ultrawide: ultrawide_dir, + horizontal: horizontal_dir, + vertical: vert_wall_dir, + }; // // Get the Monitors and parse them - for mon in map.get("monitors") { - println!("{:?}", mon); + + let mut monitors = Monitors { + monitors: Vec::new(), + }; + for mon in map.get("monitors").iter() { + for key in mon.keys() { + let monitor = mon.get(key).unwrap().as_ref().unwrap(); + // Add monitor type to Monitors vector + monitors.add(MonitorType::new(monitor.to_string())); + } } - println!("{:?}", map.get("monitors")); // Config { x_server: config.get("general", "x_server").unwrap(), wallpaper_engine: wallpaper_engine, - wallpaper_dir: WallpaperDir::new(), - monitors: Monitors::new(), + wallpaper_dir: wallpaper_dir, + monitors: monitors, } } } diff --git a/src/main.rs b/src/main.rs index c2eae3f..5fd6161 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,9 @@ mod config; fn main() { - let config = config::Config::new(); - // config.parse_config("/home/ben/.config/wallpaperctl/wallpaperctl.ini".to_owned()) - println!( - "{}", - config.parse_config("/home/ben/.config/wallpaperctl/wallpaperctl.ini".to_owned()) + // let mut config = config::Config::new(); + let config = crate::config::Config::from_config( + "/home/ben/.config/wallpaperctl/wallpaperctl.ini".to_owned(), ); + println!("{}", config); }