Refactor count_repositories

This commit is contained in:
Max Smirnov 2022-08-18 17:23:09 +03:00
parent c93c83f004
commit 4b5d962c2c
No known key found for this signature in database
GPG Key ID: 2C3271EA73A3F336

View File

@ -1,5 +1,5 @@
use crate::error::Result; use crate::error::Result;
use std::{fs::read_dir, path::Path, result::Result as StdResult}; use std::{fs::{read_dir, ReadDir}, path::Path, result::Result as StdResult, iter::once};
#[instrument] #[instrument]
pub(crate) fn count_repositories<P>(repo_path: P) -> Result<usize> pub(crate) fn count_repositories<P>(repo_path: P) -> Result<usize>
@ -8,20 +8,17 @@ where
{ {
trace!("Counting repositories"); trace!("Counting repositories");
std::fs::create_dir_all(&repo_path)?; std::fs::create_dir_all(&repo_path)?;
Ok(read_dir(repo_path)? 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_map(StdResult::ok)
.filter(|entry| entry.file_type().map(|ft| ft.is_dir()).unwrap_or(false)) .filter(|entry| entry.file_type().map(|ft| ft.is_dir()).unwrap_or(false))
.map(|entry| read_dir(entry.path())) .filter_map(|entry| read_dir(entry.path()).ok())
.filter_map(StdResult::ok)
.flat_map(|dir| {
dir.filter_map(StdResult::ok)
.filter(|entry| entry.file_type().map(|ft| ft.is_dir()).unwrap_or(false))
})
.map(|entry| read_dir(entry.path()))
.filter_map(StdResult::ok)
.flat_map(|dir| {
dir.filter_map(StdResult::ok)
.filter(|entry| entry.file_type().map(|ft| ft.is_dir()).unwrap_or(false))
})
.count())
} }