From 4b5d962c2c6fb417c15753fecc39b229c68f8209 Mon Sep 17 00:00:00 2001 From: Max Smirnov Date: Thu, 18 Aug 2022 17:23:09 +0300 Subject: [PATCH] Refactor count_repositories --- src/count.rs | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/src/count.rs b/src/count.rs index 4a74b27..d663ed8 100644 --- a/src/count.rs +++ b/src/count.rs @@ -1,5 +1,5 @@ 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] pub(crate) fn count_repositories

(repo_path: P) -> Result @@ -8,20 +8,17 @@ where { trace!("Counting repositories"); 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 { + 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)) - }) - .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()) + .filter_map(|entry| read_dir(entry.path()).ok()) }