Julio Biason
7 months ago
4 changed files with 70 additions and 0 deletions
@ -0,0 +1,7 @@ |
|||||||
|
# This file is automatically @generated by Cargo. |
||||||
|
# It is not intended for manual editing. |
||||||
|
version = 3 |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "yielddirs" |
||||||
|
version = "0.1.0" |
@ -0,0 +1,8 @@ |
|||||||
|
[package] |
||||||
|
name = "yielddirs" |
||||||
|
version = "0.1.0" |
||||||
|
edition = "2021" |
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html |
||||||
|
|
||||||
|
[dependencies] |
@ -0,0 +1,4 @@ |
|||||||
|
# YieldDirs |
||||||
|
|
||||||
|
Trying to make an iterator over the filesystem, which spews out some specific |
||||||
|
directories. |
@ -0,0 +1,51 @@ |
|||||||
|
use std::fs::ReadDir; |
||||||
|
use std::path::Path; |
||||||
|
use std::path::PathBuf; |
||||||
|
|
||||||
|
struct Walker { |
||||||
|
path: PathBuf |
||||||
|
} |
||||||
|
|
||||||
|
impl Walker { |
||||||
|
pub fn new(base: &Path) -> Self { |
||||||
|
println!("base={base:?}"); |
||||||
|
println!("is_dir={}", base.is_dir()); |
||||||
|
Self { path: base.to_path_buf()} |
||||||
|
} |
||||||
|
|
||||||
|
pub fn children(&self) -> WalkerIterator { |
||||||
|
WalkerIterator::new(&self.path) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
struct WalkerIterator { |
||||||
|
walking: ReadDir |
||||||
|
} |
||||||
|
|
||||||
|
impl WalkerIterator { |
||||||
|
fn new(path: &Path) -> Self { |
||||||
|
Self { |
||||||
|
walking: path.read_dir().unwrap() |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
impl Iterator for WalkerIterator { |
||||||
|
type Item = String; |
||||||
|
|
||||||
|
fn next(&mut self) -> Option<Self::Item> { |
||||||
|
let current = self.walking.next()?; |
||||||
|
Some(current.unwrap().file_name().to_string_lossy().to_string()) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
fn main() { |
||||||
|
let mut args = std::env::args(); |
||||||
|
let _ = args.next(); |
||||||
|
let base_path = args.next().unwrap(); |
||||||
|
|
||||||
|
let walker = Walker::new(&Path::new(&base_path).canonicalize().unwrap()); |
||||||
|
for child in walker.children() { |
||||||
|
println!("{child}"); |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue