use std::path::{Component, Path, PathBuf};
pub fn resource_relpath(path: &Path) -> PathBuf {
let mut dest = PathBuf::new();
for component in path.components() {
match component {
Component::Prefix(_) => {}
Component::RootDir => dest.push("_root_"),
Component::CurDir => {}
Component::ParentDir => dest.push("_up_"),
Component::Normal(string) => dest.push(string)
}
}
dest
}
pub fn external_binaries(external_binaries: &[String], target_triple: &str) -> Vec<String> {
let mut paths = Vec::new();
for curr_path in external_binaries {
paths.push(format!("{}-{}{}", curr_path, target_triple, if target_triple.contains("windows") { ".exe" } else { "" }));
}
paths
}
pub struct ResourcePaths<'a> {
pattern_iter: std::slice::Iter<'a, String>,
glob_iter: Option<glob::Paths>,
walk_iter: Option<walkdir::IntoIter>,
allow_walk: bool,
current_pattern: Option<String>,
current_pattern_is_valid: bool
}
impl<'a> ResourcePaths<'a> {
pub fn new(patterns: &'a [String], allow_walk: bool) -> ResourcePaths<'a> {
ResourcePaths {
pattern_iter: patterns.iter(),
glob_iter: None,
walk_iter: None,
allow_walk,
current_pattern: None,
current_pattern_is_valid: false
}
}
}
impl<'a> Iterator for ResourcePaths<'a> {
type Item = crate::Result<PathBuf>;
fn next(&mut self) -> Option<crate::Result<PathBuf>> {
loop {
if let Some(ref mut walk_entries) = self.walk_iter {
if let Some(entry) = walk_entries.next() {
let entry = match entry {
Ok(entry) => entry,
Err(error) => return Some(Err(crate::Error::from(error)))
};
let path = entry.path();
if path.is_dir() {
continue;
}
self.current_pattern_is_valid = true;
return Some(Ok(path.to_path_buf()));
}
}
self.walk_iter = None;
if let Some(ref mut glob_paths) = self.glob_iter {
if let Some(glob_result) = glob_paths.next() {
let path = match glob_result {
Ok(path) => path,
Err(error) => return Some(Err(error.into()))
};
if path.is_dir() {
if self.allow_walk {
let walk = walkdir::WalkDir::new(path);
self.walk_iter = Some(walk.into_iter());
continue;
} else {
return Some(Err(crate::Error::NotAllowedToWalkDir(path)));
}
}
self.current_pattern_is_valid = true;
return Some(Ok(path));
} else if let Some(current_path) = &self.current_pattern {
if !self.current_pattern_is_valid {
self.glob_iter = None;
return Some(Err(crate::Error::GlobPathNotFound(current_path.clone())));
}
}
}
self.glob_iter = None;
if let Some(pattern) = self.pattern_iter.next() {
self.current_pattern = Some(pattern.to_string());
self.current_pattern_is_valid = false;
let glob = match glob::glob(pattern) {
Ok(glob) => glob,
Err(error) => return Some(Err(error.into()))
};
self.glob_iter = Some(glob);
continue;
}
return None;
}
}
}