Skip to content

Instantly share code, notes, and snippets.

@hswolff
Created October 31, 2020 02:54
Show Gist options
  • Save hswolff/3db03009417255e6c08fd3ce0dce252d to your computer and use it in GitHub Desktop.
Save hswolff/3db03009417255e6c08fd3ce0dce252d to your computer and use it in GitHub Desktop.
use std::fs::read_dir;
use std::path::PathBuf;
fn main() -> std::io::Result<()> {
println!("Hello, world!");
let root_path = PathBuf::from(".");
let coll = walk_files(root_path, None)?;
println!("Coll: {:?}", coll);
Ok(())
}
#[derive(Debug)]
struct FakeFile {
path: PathBuf,
}
fn walk_files(path: PathBuf, coll: Option<Vec<FakeFile>>) -> std::io::Result<Vec<FakeFile>> {
let mut coll: Vec<FakeFile> = if let Some(arg) = coll {
arg
} else {
Vec::new()
};
let result = read_dir(path)?;
for item in result {
let dir_entry = item?;
println!("item: {:?} | type: {:?}", dir_entry, dir_entry.file_type()?);
let canonical_path = dir_entry.path().canonicalize()?;
let skip_list = [".git", "target", ".vscode"];
let should_skip = skip_list
.iter()
.any(|last_path| canonical_path.ends_with(last_path));
println!(
"should skip: {} | yay: {:?}",
should_skip,
canonical_path.ends_with(".git")
);
let file_type = dir_entry.file_type()?;
if file_type.is_dir() && !should_skip {
let mut nested_coll = walk_files(dir_entry.path(), None)?;
coll.append(&mut nested_coll);
}
coll.push(FakeFile {
path: dir_entry.path(),
});
}
Ok(coll)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_whatever() {
main();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment