Skip to content

Commit

Permalink
Directory Listing wrapper and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Stridsvagn69420 committed Jun 26, 2023
1 parent f173bcd commit f28b440
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
24 changes: 24 additions & 0 deletions src/dir.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use std::io;
use std::env;
use std::fs;
use std::path::PathBuf;

/// List Current Working Directory
///
/// 1. [Get](env::current_dir) CWD
/// 2. [List](fs::read_dir) CWD
/// 3. Filter for successful [DirEntry](fs::DirEntry)s
/// 4. Filter for files
/// 5. Map to [PathBuf] of files
/// 6. Collect to [Vec]
pub(crate) fn list_cwd() -> io::Result<(PathBuf, Vec<PathBuf>)> {
let cwd = env::current_dir()?;

let fpaths = fs::read_dir(&cwd)?
.filter_map(|x| x.ok())
.filter(|e| e.metadata().is_ok_and(|m| m.is_file()))
.map(|f| f.path())
.collect();

Ok((cwd, fpaths))
}
39 changes: 37 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use std::env;
use std::io::ErrorKind::{NotFound, PermissionDenied};
use std::path::Path;
use std::process::ExitCode;
use kagero::printer::{Printer, Colors};
mod hash;
mod help;
mod dir;

fn main() -> ExitCode {
let mut p = Printer::default();
Expand All @@ -26,14 +28,47 @@ fn main() -> ExitCode {
ExitCode::SUCCESS
},
_ => {
// Convert to Path
// Check File Path
let filepath = Path::new(file);
if !filepath.is_file() {
p.error(file, Colors::RedBright).errorln(" does not exist or is not a file!", Colors::Red);
return ExitCode::FAILURE;
}

println!("{:#?}", hash::hasher(filepath)); // TODO: Do more stuff. This is just a test atm.
// List Files in CWD
let files = match dir::list_cwd() {
Ok(res) => res,
Err(ref e) if vec![NotFound, PermissionDenied].contains(&e.kind()) => {
p.errorln("Could not access directory! Either the current directory does not exist anymore or is unreadable.", Colors::RedBright);
return ExitCode::FAILURE;
},
Err(e) => {
p.error("An unexpected error occured: ", Colors::Red)
.errorln(e.kind().to_string().as_str(), Colors::RedBright);
return ExitCode::FAILURE;
}
};



// ---- TODO: Do more stuff. This is just a test atm. ----
// List files
println!("{:#?}", files);

let input = files.0.join(filepath.file_name().unwrap()); // Create path as if file was in cwd (I test with build.rs as the input file)
let current = files.1.get(1).unwrap().to_owned(); // Get a file's absolute path (build.rs is my 2nd file in the listing)
let ihash = hash::hasher(&input).unwrap();
let chash = hash::hasher(&current).unwrap();
println!("{:#?}", input);
println!("{:#?}", current);
println!("{:#?}", ihash);
println!("{:#?}", chash);
println!("{:#?}", current == input); // Compare the files' path.
println!("{:#?}", ihash == chash); // Also compare their hash value along with their path, like it has to do with every other file.
// This is used so that if the current file matches the input file, it won't be removed. The system only works like this for the current functionality!

// Hash Test
println!("{:#?}", hash::hasher(filepath));

ExitCode::SUCCESS
}
Expand Down

0 comments on commit f28b440

Please sign in to comment.