Added configparser for rewrite

This commit is contained in:
benjamyn committed 2024-03-09 22:29:41 +11:00
1 parent 0d5c0d668e
commit 6cd99b50a9
4 files changed
+51 -7

No files matched your search

+41 -5
View File
@@ -1,6 +1,8 @@
use configparser::ini::Ini;
use core::fmt;
use std::fs::File;
use std::io::prelude::*;
use untildify;
#[derive(Debug)]
@@ -8,6 +10,18 @@ pub enum WallpaperHandler {
Feh,
Plasma,
Gnome,
Error,
}
impl WallpaperHandler {
pub fn new(handler: String) -> WallpaperHandler {
match handler.as_str() {
"feh" => WallpaperHandler::Feh,
"plasma" => WallpaperHandler::Plasma,
"gnome" => WallpaperHandler::Gnome,
_ => WallpaperHandler::Error,
}
}
}
impl fmt::Display for WallpaperHandler {
@@ -108,10 +122,32 @@ impl fmt::Display for Config {
}
impl Config {
pub fn load_from_file(self, file_path: String) -> String {
let mut file = File::open(file_path).unwrap();
let mut contents = String::new();
file.read_to_string(&mut contents).unwrap();
contents
pub fn parse_config(self, 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 =
WallpaperHandler::new(config.get("general", "wallpaper_engine").unwrap());
//
// Get the wallpaper dirs and parse them
//
// Get the Monitors and parse them
for mon in map.get("monitors") {
println!("{:?}", mon);
}
println!("{:?}", map.get("monitors"));
//
Config {
x_server: config.get("general", "x_server").unwrap(),
wallpaper_engine: wallpaper_engine,
wallpaper_dir: WallpaperDir::new(),
monitors: Monitors::new(),
}
}
}
+2 -2
View File
@@ -2,9 +2,9 @@ mod config;
fn main() {
let config = config::Config::new();
// config.parse_config("/home/ben/.config/wallpaperctl/wallpaperctl.ini".to_owned())
println!(
"{}",
config.load_from_file("/home/ben/.config/wallpaperctl/wallpaperctl.ini".to_owned())
config.parse_config("/home/ben/.config/wallpaperctl/wallpaperctl.ini".to_owned())
);
}