Browse Source

Trying something silly with iterators

master
Julio Biason 3 weeks ago
parent
commit
d75b466f6d
  1. 7
      yielddirs/Cargo.lock
  2. 8
      yielddirs/Cargo.toml
  3. 4
      yielddirs/README.md
  4. 51
      yielddirs/src/main.rs

7
yielddirs/Cargo.lock generated

@ -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"

8
yielddirs/Cargo.toml

@ -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]

4
yielddirs/README.md

@ -0,0 +1,4 @@
# YieldDirs
Trying to make an iterator over the filesystem, which spews out some specific
directories.

51
yielddirs/src/main.rs

@ -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…
Cancel
Save