dir.rs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. use std::io::{fs, IoResult};
  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. pub contents: Vec<Path>,
  10. pub path: Path,
  11. }
  12. impl<'a> Dir<'a> {
  13. pub fn readdir(path: Path) -> IoResult<Dir<'a>> {
  14. fs::readdir(&path).map(|paths| Dir {
  15. contents: paths,
  16. path: path.clone(),
  17. })
  18. }
  19. pub fn files(&'a self) -> Vec<File<'a>> {
  20. let mut files = vec![];
  21. for path in self.contents.iter() {
  22. match File::from_path(path, self) {
  23. Ok(file) => {
  24. files.push(file);
  25. }
  26. Err(e) => {
  27. println!("{}: {}", path.display(), e);
  28. }
  29. }
  30. }
  31. files
  32. }
  33. pub fn contains(&self, path: &Path) -> bool {
  34. self.contents.contains(path)
  35. }
  36. }