Fixed config parsing

This commit is contained in:
Benjamyn Love 2024-03-09 23:01:29 +11:00
parent 6cd99b50a9
commit ed148b05cb
2 changed files with 44 additions and 18 deletions

View File

@ -1,7 +1,6 @@
use configparser::ini::Ini; use configparser::ini::Ini;
use core::fmt; use core::fmt;
use std::fs::File; use std::env::VarError;
use std::io::prelude::*;
use untildify; use untildify;
@ -35,6 +34,18 @@ pub enum MonitorType {
Ultrawide, Ultrawide,
Horizontal, Horizontal,
Vertical, 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 { impl fmt::Display for MonitorType {
@ -50,9 +61,11 @@ pub struct Monitors {
impl Monitors { impl Monitors {
pub fn new() -> Monitors { pub fn new() -> Monitors {
Monitors { Monitors { monitors: vec![] }
monitors: vec![MonitorType::Vertical], }
}
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 { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "[")?; write!(f, "[")?;
for v in &self.monitors { for v in &self.monitors {
write!(f, "{}", v)?; write!(f, "{}, ", v)?;
} }
write!(f, "]") write!(f, "]")
} }
@ -122,10 +135,9 @@ impl fmt::Display for Config {
} }
impl 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 mut config = Ini::new();
let map = config.load(file_path).unwrap(); let map = config.load(file_path).unwrap();
// let file_contents = Config::load_from_file(file_path);
// Get the wallpaper engine and parse it // Get the wallpaper engine and parse it
let wallpaper_engine = let wallpaper_engine =
@ -134,20 +146,35 @@ impl Config {
// Get the wallpaper dirs and parse them // 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 // 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 { Config {
x_server: config.get("general", "x_server").unwrap(), x_server: config.get("general", "x_server").unwrap(),
wallpaper_engine: wallpaper_engine, wallpaper_engine: wallpaper_engine,
wallpaper_dir: WallpaperDir::new(), wallpaper_dir: wallpaper_dir,
monitors: Monitors::new(), monitors: monitors,
} }
} }
} }

View File

@ -1,10 +1,9 @@
mod config; mod config;
fn main() { fn main() {
let config = config::Config::new(); // let mut config = config::Config::new();
// config.parse_config("/home/ben/.config/wallpaperctl/wallpaperctl.ini".to_owned()) let config = crate::config::Config::from_config(
println!( "/home/ben/.config/wallpaperctl/wallpaperctl.ini".to_owned(),
"{}",
config.parse_config("/home/ben/.config/wallpaperctl/wallpaperctl.ini".to_owned())
); );
println!("{}", config);
} }