Refactor count_repositories
This commit is contained in:
parent
c93c83f004
commit
4b5d962c2c
27
src/count.rs
27
src/count.rs
@ -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())
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user