From 952b0ec05828116e228bdc71f65014f9be976ad3 Mon Sep 17 00:00:00 2001 From: curly Date: Wed, 4 Jan 2023 14:45:20 -0700 Subject: config redone --- TODO | 4 --- src/lib.rs | 93 ++++++++++++++++++++++++++++++++++---------------------------- 2 files changed, 51 insertions(+), 46 deletions(-) diff --git a/TODO b/TODO index 06f475e..39575e9 100644 --- a/TODO +++ b/TODO @@ -1,8 +1,4 @@ -Rework Config struct -Use Config values in assigning the build_dir location -Use Config values for check_path() "for s in v {" needs to push the surrounding text to the string In files - NEED TO FOR LOOP ON COMMANDS AND FILES PREFIXES -- GET EXCLUDE AND DNS LISTS \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 17b1c6f..5264ad8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,27 +9,27 @@ enum PathType { } pub struct Config { - config: toml::Value, - commands: toml::Value, + pub source: String, + pub build_dir: String, + + pub do_not_scan: Vec, + pub excludes: Vec, + + pub file_delim: String, + pub command_delim: String, + pub commands: Vec<(String, String)> } impl Config { pub fn new() -> Config { println!("Using built-in config"); Config { - config: toml::toml!{ - source = "src" - build_dir = "build" - - do_not_scan = [] - exclude = [] - - file_delim = ":?" - command_delim = ":!" - commands_file = "commands.toml" - }, - commands: toml::toml!{ - empty = "" - } + source: String::from("src"), + build_dir: String::from("build"), + do_not_scan: vec![], + excludes: vec![], + file_delim: String::from(":?"), + command_delim: String::from(":!"), + commands: vec![], } } @@ -59,23 +59,31 @@ impl Config { }; let commands = commands.get("commands").expect("Could not find \"[commands]\" field"); - Config { - config: config.clone(), - commands: commands.clone(), + let mut commands_vec = vec![]; + let commands = commands.as_table().unwrap(); + for x in commands.keys() { + let x = commands.get(x); + commands_vec.push((String::from(x.unwrap().as_str().unwrap()), String::from(x.unwrap().as_str().unwrap()))) } - } - pub fn get_str(&self, string: &str) -> Option<&str> { - match self.config.get(string) { - Some(n) => n.as_str(), - None => None + let mut do_not_scan = vec![]; + for x in config["do_not_scan"].as_array().unwrap() { + do_not_scan.push(String::from(x.as_str().unwrap())) } - } - - pub fn get_cmd_str(&self, string: &str) -> Option<&str> { - match self.commands.get(string) { - Some(n) => n.as_str(), - None => None + + let mut exclude = vec![]; + for x in config["exclude"].as_array().unwrap() { + exclude.push(String::from(x.as_str().unwrap())) + } + + Config { + source: String::from(config["source"].as_str().unwrap()), + build_dir: String::from(config["build_dir"].as_str().unwrap()), + do_not_scan: do_not_scan, + excludes: exclude, + file_delim: String::from(config["file_delim"].as_str().unwrap()), + command_delim: String::from(config["command_delim"].as_str().unwrap()), + commands: commands_vec, } } } @@ -91,8 +99,8 @@ impl S3G { } pub fn run(&self) { - let source = self.config.get_str("source").unwrap(); - let build = self.config.get_str("build_dir").unwrap(); + let source = &self.config.source; + let build = &self.config.build_dir; // Create build directory or panic S3G::create_dir(&path::PathBuf::from(build)).unwrap(); @@ -132,9 +140,9 @@ impl S3G { } fn check_path(&self, path: &path::PathBuf) -> PathType { - // GET EXCLUDE AND DNS LISTS - let excludes: Vec = vec![path::PathBuf::from("nav.html")]; - let dns: Vec = vec![path::PathBuf::from("assets/")]; + // Get Exclude and DNS lists + let excludes: Vec = self.config.excludes.clone().into_iter().map(|x| path::PathBuf::from(x)).collect(); + let dns: Vec = self.config.do_not_scan.clone().into_iter().map(|x| path::PathBuf::from(x)).collect(); // Convert path to str for comparisons let path = path.to_str().unwrap(); @@ -159,7 +167,7 @@ impl S3G { } fn copy_file(&self, path: &path::PathBuf) { - let build_path = S3G::replace_path_head(&path, "build/"); + let build_path = S3G::replace_path_head(&path, &self.config.build_dir); match fs::copy(path, build_path) { Ok(_) => (), Err(n) => { @@ -171,7 +179,7 @@ impl S3G { } fn directory_scan(&self, path: path::PathBuf) { - println!("Scanning: {:?}", &path); + // println!("Looking in: {:?}", &path); // Check the path match self.check_path(&path) { @@ -180,7 +188,7 @@ impl S3G { }; // Create the dir in the build tree - let build_path = S3G::replace_path_head(&path, "build/"); + let build_path = S3G::replace_path_head(&path, &self.config.build_dir); S3G::create_dir(&build_path).unwrap(); // Iterate over path contents @@ -202,10 +210,11 @@ impl S3G { fn file_scan(&self, path: &path::PathBuf) -> Option { // Check path match self.check_path(&path) { - PathType::Okay => println!("Scanning: \"{}\"", &path.to_str().unwrap()), + // PathType::Okay => println!("Scanning: \"{}\"", &path.to_str().unwrap()), + PathType::Okay => (), PathType::DoNotScan => { self.copy_file(&path); - println!("Not scanning: \"{}\"", &path.to_str().unwrap()); + // println!("Not scanning: \"{}\"", &path.to_str().unwrap()); return None }, PathType::Exclude => (), @@ -227,7 +236,6 @@ impl S3G { // NEED TO FOR LOOP ON COMMANDS AND FILES PREFIXES if line.contains(":?") { let v: Vec<&str> = line.trim().split(":?").collect(); - println!("{:?}", v); for s in v { let s = match s { "" => String::from(""), @@ -257,11 +265,12 @@ impl S3G { fn file_write(&self, path: &path::PathBuf, contents: String) -> Result<(),()> { match self.check_path(&path) { + // PathType::Exclude => {println!("Excluding: \"{}\"", &path.to_str().unwrap()); return Ok(())}, PathType::Exclude => return Ok(()), _ => (), } - let dest = S3G::replace_path_head(&path, "build/"); + let dest = S3G::replace_path_head(&path, &self.config.build_dir); match fs::write(dest, contents) { Ok(_) => Ok(()), Err(_) => Err(()), -- cgit v1.2.3