dir.rs 1.1 KB

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