diff options
-rw-r--r-- | TODO | 7 | ||||
-rw-r--r-- | src/lib.rs | 281 | ||||
-rw-r--r-- | src/main.rs | 3 |
3 files changed, 171 insertions, 120 deletions
@@ -1,4 +1,7 @@ -Check contents of files being read Rework Config struct Use Config values in assigning the build_dir location -Use Config values for check_path()
\ No newline at end of file +Use Config values for check_path() + +In files +- NEED TO FOR LOOP ON COMMANDS AND FILES PREFIXES +- GET EXCLUDE AND DNS LISTS
\ No newline at end of file @@ -45,7 +45,10 @@ impl Config { let file_string = n.as_str().unwrap(); let file_string = match fs::read_to_string(file_string) { Ok(n) => n, - Err(_) => {println!("Could not load commands from file"); String::from("[commands]")}, + Err(_) => { + println!("Could not load commands from file"); + String::from("[commands]") + }, }; toml::from_str(file_string.as_str()).expect("Invalid toml") } else { @@ -77,139 +80,183 @@ impl Config { } } - -fn create_dir(path: &path::PathBuf) -> Result<(), ()> { - match fs::create_dir(path) { - Ok(_) => (), - Err(n) => match n.kind() { - ErrorKind::AlreadyExists => (), - _ => return Err(()), +pub struct S3G { + config: Config, +} +impl S3G { + pub fn new(config: Config) -> S3G { + S3G { + config, } } - return Ok(()) -} - -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(); - - return path -} - -fn append_path_head(path: &path::PathBuf, head: &str) -> path::PathBuf { - let mut head = path::PathBuf::from(head); - head.push(path); - return head -} - -fn replace_path_head(path: &path::PathBuf, head: &str) -> path::PathBuf { - let path = remove_path_head(path); - append_path_head(&path, head) -} -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 - let excludes: Vec<path::PathBuf> = vec![path::PathBuf::from("nav.html")]; - let dns: Vec<path::PathBuf> = vec![path::PathBuf::from("assets/")]; - - // Convert path to str for comparisons - let path = path.to_str().unwrap(); + pub fn run(&self) { + let source = self.config.get_str("source").unwrap(); + let build = self.config.get_str("build_dir").unwrap(); + + // Create build directory or panic + S3G::create_dir(&path::PathBuf::from(build)).unwrap(); + + + self.directory_scan(path::PathBuf::from(source)); + } - // CHECK FOR EXCLUDES AND DO NOT SCANS - // Excludes - for ex_path in excludes { - let ex_path = ex_path.to_str().unwrap(); - if &path.contains(&ex_path) == &true { - return PathType::Exclude; + fn create_dir(path: &path::PathBuf) -> Result<(), ()> { + match fs::create_dir(path) { + Ok(_) => (), + Err(n) => match n.kind() { + ErrorKind::AlreadyExists => (), + _ => return Err(()), + } } + return Ok(()) + } + + 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(); + + return path + } + + fn append_path_head(path: &path::PathBuf, head: &str) -> path::PathBuf { + let mut head = path::PathBuf::from(head); + head.push(path); + return head + } + + fn replace_path_head(path: &path::PathBuf, head: &str) -> path::PathBuf { + let path = S3G::remove_path_head(path); + S3G::append_path_head(&path, head) } - // Do not scans (copy files) - for dns_path in dns { - let dns_path = dns_path.to_str().unwrap(); - if &path.contains(&dns_path) == &true { - return PathType::DoNotScan; + fn check_path(&self, path: &path::PathBuf) -> PathType { + // GET EXCLUDE AND DNS LISTS + let excludes: Vec<path::PathBuf> = vec![path::PathBuf::from("nav.html")]; + let dns: Vec<path::PathBuf> = vec![path::PathBuf::from("assets/")]; + + // Convert path to str for comparisons + let path = path.to_str().unwrap(); + + // Excludes + for ex_path in excludes { + let ex_path = ex_path.to_str().unwrap(); + if &path.contains(&ex_path) == &true { + return PathType::Exclude; + } } + + // Do not scans (copy files) + for dns_path in dns { + let dns_path = dns_path.to_str().unwrap(); + if &path.contains(&dns_path) == &true { + return PathType::DoNotScan; + } + } + + return PathType::Okay; } - - return PathType::Okay; -} - -fn copy_file(path: &path::PathBuf, config: &Config) { - let build_path = replace_path_head(&path, "build/"); - match fs::copy(path, build_path) { - Ok(_) => (), - Err(n) => { - if n.kind() != ErrorKind::AlreadyExists { - panic!() + + fn copy_file(&self, path: &path::PathBuf) { + let build_path = S3G::replace_path_head(&path, "build/"); + match fs::copy(path, build_path) { + Ok(_) => (), + Err(n) => { + if n.kind() != ErrorKind::AlreadyExists { + panic!() + } + }, + } + } + + fn directory_scan(&self, path: path::PathBuf) { + println!("Scanning: {:?}", &path); + + // Check the path + match self.check_path(&path) { + PathType::Exclude => return, + _ => (), + }; + + // Create the dir in the build tree + let build_path = S3G::replace_path_head(&path, "build/"); + S3G::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() { + self.directory_scan(x); + } else { + match self.file_scan(&x) { + None => (), + Some(n) => self.file_write(&x, n).unwrap(), + } } - }, + + + } } -} - -fn directory_scan(path: path::PathBuf, config: &Config) { - println!("Scanning: {:?}", &path); - - // Check the path - match check_path(&path, config) { - PathType::Exclude => return, - _ => (), - }; + + fn file_scan(&self, path: &path::PathBuf) -> Option<String> { + // Check path + match self.check_path(&path) { + PathType::Okay => println!("Scanning: \"{}\"", &path.to_str().unwrap()), + PathType::DoNotScan => { + self.copy_file(&path); + println!("Not scanning: \"{}\"", &path.to_str().unwrap()); + return None + }, + PathType::Exclude => (), + }; + + // File scanning + let mut file_string = String::new(); + + for line in fs::read_to_string(&path).unwrap().lines() { + + // 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(""), + // Scan the file from the path + n => self.file_scan(&path::PathBuf::from(n)).unwrap_or(String::from("")), + }; - // Create the dir in the build tree - let build_path = replace_path_head(&path, "build/"); - create_dir(&build_path).unwrap(); + // Push matching and scanned string + file_string.push_str(&s); + file_string.push('\n'); + } + } else { + // Push unmatching line + file_string.push_str(line); + file_string.push('\n'); + } + } - // 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); + // Return the final file_string + if file_string != "" { + return Some(file_string); } else { - file_scan(x, &config); + return None; } - - } -} - -fn file_scan(path: path::PathBuf, config: &Config) { - // Check path - match check_path(&path, config) { - PathType::Okay => (), - PathType::DoNotScan => {copy_file(&path, &config); 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(); + + fn file_write(&self, path: &path::PathBuf, contents: String) -> Result<(),()> { - 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') + match self.check_path(&path) { + PathType::Exclude => return Ok(()), + _ => (), + } + + let dest = S3G::replace_path_head(&path, "build/"); + match fs::write(dest, contents) { + Ok(_) => Ok(()), + Err(_) => Err(()), } } - - let dest = replace_path_head(&path, "build/"); - fs::write(dest, file_string).expect("Could not write file"); }
\ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 3b75b65..8e2a6ae 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,5 +8,6 @@ fn main() { }; // Run - s3g::run(config); + let s3g = s3g::S3G::new(config); + s3g.run(); }
\ No newline at end of file |