dir.rs 870 B

123456789101112131415161718192021222324252627282930313233
  1. use std::io::fs;
  2. use file::File;
  3. // The purpose of a Dir is to provide a cached list of the file paths
  4. // in the directory being searched for. This object is then passed to
  5. // the Files themselves, which can then check the status of their
  6. // surrounding files, such as whether it needs to be coloured
  7. // differently if a certain other file exists.
  8. pub struct Dir<'a> {
  9. contents: Vec<Path>,
  10. }
  11. impl<'a> Dir<'a> {
  12. pub fn readdir(path: Path) -> Dir<'a> {
  13. match fs::readdir(&path) {
  14. Ok(paths) => Dir {
  15. contents: paths,
  16. },
  17. Err(e) => fail!("readdir: {}", e),
  18. }
  19. }
  20. pub fn files(&'a self) -> Vec<File<'a>> {
  21. self.contents.iter().map(|path| File::from_path(path, self)).collect()
  22. }
  23. pub fn contains(&self, path: &Path) -> bool {
  24. self.contents.contains(path)
  25. }
  26. }