Created
December 6, 2023 08:55
-
-
Save DefectingCat/1d508005da858e36389073730974a292 to your computer and use it in GitHub Desktop.
Return a closure fn
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Find parents of all files | |
/// | |
/// ## Arguments | |
/// | |
/// `local_path`: local files path from params | |
/// | |
/// ## Return | |
/// | |
/// `Fn`: used for `fold()`'s callback | |
pub fn fold_parents(local_path: &String) -> impl Fn(Vec<PathBuf>, &PathBuf) -> Vec<PathBuf> { | |
let local_path = PathBuf::from(local_path); | |
move |mut prev: Vec<_>, cur: &PathBuf| -> Vec<PathBuf> { | |
let skip_count = local_path | |
.parent() | |
.unwrap_or(&PathBuf::new()) | |
.components() | |
.count(); | |
let skip_count = if local_path.is_dir() && local_path.components().count() == 1 { | |
1 | |
} else { | |
skip_count | |
}; | |
let parent = cur | |
.parent() | |
.map(|parent| parent.components().skip(skip_count)) | |
.filter(|p| p.clone().count() > 0) | |
.map(|p| p.collect::<PathBuf>()); | |
if let Some(p) = parent { | |
if prev.contains(&p) { | |
return prev; | |
} | |
let components = p.components().collect::<Vec<_>>(); | |
for index in 1..=components.len() { | |
let path = &components[..index] | |
.iter() | |
.fold(PathBuf::new(), |mut child, cur| { | |
child.push(PathBuf::from(cur)); | |
child | |
}); | |
prev.push(path.clone()); | |
} | |
} | |
prev | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment