Use the optional argument trick

This commit is contained in:
Benjamin Sago 2017-06-29 13:17:26 +01:00
parent 31148eda7b
commit 0aa33595d7
2 changed files with 14 additions and 13 deletions

View File

@ -114,10 +114,10 @@ impl<'dir> Files<'dir> {
fn next_visible_file(&mut self) -> Option<Result<File<'dir>, (PathBuf, io::Error)>> {
loop {
if let Some(path) = self.inner.next() {
let filen = File::filename(path);
if !self.dotfiles && filen.starts_with(".") { continue }
let filename = File::filename(path);
if !self.dotfiles && filename.starts_with(".") { continue }
return Some(File::new(path.clone(), Some(self.dir), Some(filen))
return Some(File::new(path.clone(), self.dir, filename)
.map_err(|e| (path.clone(), e)))
}
else {
@ -148,12 +148,12 @@ impl<'dir> Iterator for Files<'dir> {
fn next(&mut self) -> Option<Self::Item> {
if let Dots::DotNext = self.dots {
self.dots = Dots::DotDotNext;
Some(File::new(self.dir.path.to_path_buf(), Some(self.dir), Some(String::from(".")))
Some(File::new(self.dir.path.to_path_buf(), self.dir, String::from("."))
.map_err(|e| (Path::new(".").to_path_buf(), e)))
}
else if let Dots::DotDotNext = self.dots {
self.dots = Dots::FilesNext;
Some(File::new(self.parent(), Some(self.dir), Some(String::from("..")))
Some(File::new(self.parent(), self.dir, String::from(".."))
.map_err(|e| (self.parent(), e)))
}
else {

View File

@ -57,15 +57,16 @@ pub struct File<'dir> {
}
impl<'dir> File<'dir> {
pub fn new(path: PathBuf, parent_dir: Option<&'dir Dir>, mut filename: Option<String>) -> IOResult<File<'dir>> {
if filename.is_none() {
filename = Some(File::filename(&path));
}
pub fn new<PD, FN>(path: PathBuf, parent_dir: PD, filename: FN) -> IOResult<File<'dir>>
where PD: Into<Option<&'dir Dir>>,
FN: Into<Option<String>>
{
let parent_dir = parent_dir.into();
let metadata = fs::symlink_metadata(&path)?;
let name = filename.into().unwrap_or_else(|| File::filename(&path));
let ext = File::ext(&path);
let metadata = fs::symlink_metadata(&path)?;
let ext = File::ext(&path);
Ok(File { path, parent_dir, metadata, ext, name: filename.unwrap() })
Ok(File { path, parent_dir, metadata, ext, name })
}
/// A files name is derived from its string. This needs to handle directories