diff options
author | curly <curlybryce@protonmail.com> | 2023-01-03 12:53:09 -0700 |
---|---|---|
committer | curly <curlybryce@protonmail.com> | 2023-01-03 12:53:09 -0700 |
commit | 6c3ab59c9acdc9528786993f47d225358e8b951a (patch) | |
tree | 6ca9c7cc4884f4a359495dc4323d807e85b7b9ab /src/lib.rs | |
parent | f31d940ed0b9b67b92212adcc01b44fc4d528f69 (diff) | |
download | s3g-6c3ab59c9acdc9528786993f47d225358e8b951a.tar.gz s3g-6c3ab59c9acdc9528786993f47d225358e8b951a.tar.bz2 s3g-6c3ab59c9acdc9528786993f47d225358e8b951a.zip |
almost done probably
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 107 |
1 files changed, 99 insertions, 8 deletions
@@ -2,6 +2,12 @@ use std::{fs, path}; use std::io::ErrorKind; use toml; +enum PathType { + Exclude, + DoNotScan, + Okay, +} + pub struct Config { config: toml::Value, commands: toml::Value, @@ -72,8 +78,8 @@ impl Config { } -pub fn create_dir(path: &str) -> Result<(), ()> { - match fs::create_dir(std::path::PathBuf::from(path)) { +fn create_dir(path: &path::PathBuf) -> Result<(), ()> { + match fs::create_dir(path) { Ok(_) => (), Err(n) => match n.kind() { ErrorKind::AlreadyExists => (), @@ -83,7 +89,7 @@ pub fn create_dir(path: &str) -> Result<(), ()> { return Ok(()) } -pub fn remove_path_head(path: &path::PathBuf) -> path::PathBuf { +fn remove_path_head(path: &path::PathBuf) -> path::PathBuf { let mut path = path.to_str().unwrap().split("/"); path.next(); let path: path::PathBuf = path.collect(); @@ -91,18 +97,103 @@ pub fn remove_path_head(path: &path::PathBuf) -> path::PathBuf { return path } -pub fn append_path_head(path: &path::PathBuf, head: &str) -> path::PathBuf { +fn append_path_head(path: &path::PathBuf, head: &str) -> path::PathBuf { let mut head = path::PathBuf::from(head); head.push(path); return head } -pub fn replace_path_head(path: &path::PathBuf, head: &str) -> path::PathBuf { +fn replace_path_head(path: &path::PathBuf, head: &str) -> path::PathBuf { let path = remove_path_head(path); append_path_head(&path, head) } -pub fn remove_path_tail(path: &mut path::PathBuf) -> &path::PathBuf { - path.pop(); - path +pub fn run(config: Config) { + let source = config.get_str("source").unwrap(); + let build = config.get_str("build_dir").unwrap(); + + // Create build directory or panic + create_dir(&path::PathBuf::from(build)).unwrap(); + + + directory_scan(path::PathBuf::from(source), &config); +} + +fn check_path(path: &path::PathBuf, config: &Config) -> PathType { + // GET EXCLUDE AND DNS LISTS + + // CHECK FOR EXCLUDES AND DO NOT SCANS + // Excludes + // for ex_path in excludes { + // if &ex_path == &path { + // return PathType::Exclude; + // } + // } + + // // Do not scans (copy files) + // for dns_path in dns { + // if &dns_path == &path { + + // return PathType::DoNotScan; + // } + // } + + PathType::Exclude +} + +fn copy_file(path: &path::PathBuf) {} + +fn directory_scan(path: path::PathBuf, config: &Config) { + println!("Scanning: {:?}", &path); + + // Check the path + match check_path(&path, config) { + PathType::Exclude => return, + _ => (), + }; + + // Create the dir in the build tree + let build_path = replace_path_head(&path, "build/"); + create_dir(&build_path).unwrap(); + + // Iterate over path contents + for x in fs::read_dir(path).unwrap() { + let x = x.as_ref().unwrap().path(); + if x.is_dir() { + directory_scan(x, &config); + } else { + file_scan(x, &config); + } + + + } +} + +fn file_scan(path: path::PathBuf, config: &Config) { + // Check path + match check_path(&path, config) { + PathType::Okay => (), + PathType::DoNotScan => {copy_file(&path); return}, + PathType::Exclude => return, + }; + + // File scanning + let mut file_string = String::new(); + + for line in fs::read_to_string(&path).unwrap().lines() { + if line.contains(":?") { + let v: Vec<&str> = line.trim().split(":?").collect(); + let v = v.get(1).unwrap(); + + println!("INSERTING {} into {:?}", v, path); + + file_string.push_str(fs::read_to_string(v).unwrap().as_ref()); + } else { + file_string.push_str(line); + file_string.push('\n') + } + } + + let dest = replace_path_head(&path, "build/"); + fs::write(dest, file_string).expect("Could not write file"); }
\ No newline at end of file |