hoc/src/count.rs

25 lines
739 B
Rust
Raw Normal View History

use crate::error::Result;
2022-08-18 16:23:09 +02:00
use std::{fs::{read_dir, ReadDir}, path::Path, result::Result as StdResult, iter::once};
#[instrument]
pub(crate) fn count_repositories<P>(repo_path: P) -> Result<usize>
where
P: AsRef<Path> + std::fmt::Debug,
{
trace!("Counting repositories");
2020-05-15 13:34:48 +02:00
std::fs::create_dir_all(&repo_path)?;
2022-08-18 16:23:09 +02:00
Ok(once(read_dir(repo_path)?)
.flat_map(sub_directories)
.flat_map(sub_directories)
.flat_map(sub_directories)
.count()
)
}
fn sub_directories(dir: ReadDir) -> impl Iterator<Item = ReadDir> {
dir
.filter_map(StdResult::ok)
.filter(|entry| entry.file_type().map(|ft| ft.is_dir()).unwrap_or(false))
2022-08-18 16:23:09 +02:00
.filter_map(|entry| read_dir(entry.path()).ok())
}