dir.rs 1.0 KB
Newer Older
B
Ben S 已提交
1
use std::io::{fs, IoResult};
B
Ben S 已提交
2 3 4 5 6 7 8 9 10
use file::File;

// The purpose of a Dir is to provide a cached list of the file paths
// in the directory being searched for. This object is then passed to
// the Files themselves, which can then check the status of their
// surrounding files, such as whether it needs to be coloured
// differently if a certain other file exists.

pub struct Dir<'a> {
B
Ben S 已提交
11 12
    pub contents: Vec<Path>,
    pub path: Path,
B
Ben S 已提交
13 14 15
}

impl<'a> Dir<'a> {
B
Ben S 已提交
16 17 18
    pub fn readdir(path: Path) -> IoResult<Dir<'a>> {
        fs::readdir(&path).map(|paths| Dir {
            contents: paths,
B
Ben S 已提交
19
            path: path.clone(),
B
Ben S 已提交
20
        })
B
Ben S 已提交
21 22 23
    }

    pub fn files(&'a self) -> Vec<File<'a>> {
B
Ben S 已提交
24 25 26 27
        let mut files = vec![];
        
        for path in self.contents.iter() {
            match File::from_path(path, self) {
B
Ben S 已提交
28 29
                Ok(file) => files.push(file),
                Err(e)   => println!("{}: {}", path.display(), e),
B
Ben S 已提交
30 31 32 33
            }
        }
        
        files
B
Ben S 已提交
34 35 36 37 38 39 40 41
    }

    pub fn contains(&self, path: &Path) -> bool {
        self.contents.contains(path)
    }
}