Added configparser for rewrite

This commit is contained in:
Benjamyn Love 2024-03-09 22:29:41 +11:00
parent 0d5c0d668e
commit 6cd99b50a9
4 changed files with 51 additions and 7 deletions

7
Cargo.lock generated
View File

@ -11,6 +11,12 @@ dependencies = [
"memchr", "memchr",
] ]
[[package]]
name = "configparser"
version = "3.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ec6d3da8e550377a85339063af6e3735f4b1d9392108da4e083a1b3b9820288"
[[package]] [[package]]
name = "memchr" name = "memchr"
version = "2.7.1" version = "2.7.1"
@ -21,6 +27,7 @@ checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149"
name = "mycelium" name = "mycelium"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"configparser",
"untildify", "untildify",
] ]

View File

@ -4,4 +4,5 @@ version = "0.1.0"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
configparser = "3.0.4"
untildify = "0.1.1" untildify = "0.1.1"

View File

@ -1,6 +1,8 @@
use configparser::ini::Ini;
use core::fmt; use core::fmt;
use std::fs::File; use std::fs::File;
use std::io::prelude::*; use std::io::prelude::*;
use untildify; use untildify;
#[derive(Debug)] #[derive(Debug)]
@ -8,6 +10,18 @@ pub enum WallpaperHandler {
Feh, Feh,
Plasma, Plasma,
Gnome, 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 { impl fmt::Display for WallpaperHandler {
@ -108,10 +122,32 @@ impl fmt::Display for Config {
} }
impl Config { impl Config {
pub fn load_from_file(self, file_path: String) -> String { pub fn parse_config(self, file_path: String) -> Config {
let mut file = File::open(file_path).unwrap(); let mut config = Ini::new();
let mut contents = String::new(); let map = config.load(file_path).unwrap();
file.read_to_string(&mut contents).unwrap(); // let file_contents = Config::load_from_file(file_path);
contents
// 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(),
}
} }
} }

View File

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