dir.rs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. use std::io::{self, Result as IOResult};
  2. use std::fs;
  3. use std::path::{Path, PathBuf};
  4. use std::slice::Iter as SliceIter;
  5. use feature::Git;
  6. use file::{File, fields};
  7. /// A **Dir** provides a cached list of the file paths in a directory that's
  8. /// being listed.
  9. ///
  10. /// This object gets passed to the Files themselves, in order for them to
  11. /// check the existence of surrounding files, then highlight themselves
  12. /// accordingly. (See `File#get_source_files`)
  13. pub struct Dir {
  14. /// A vector of the files that have been read from this directory.
  15. contents: Vec<PathBuf>,
  16. /// The path that was read.
  17. pub path: PathBuf,
  18. /// Holds a `Git` object if scanning for Git repositories is switched on,
  19. /// and this directory happens to contain one.
  20. git: Option<Git>,
  21. }
  22. impl Dir {
  23. /// Create a new Dir object filled with all the files in the directory
  24. /// pointed to by the given path. Fails if the directory can't be read, or
  25. /// isn't actually a directory, or if there's an IO error that occurs
  26. /// while scanning.
  27. pub fn read_dir(path: &Path, git: bool) -> IOResult<Dir> {
  28. let reader = try!(fs::read_dir(path));
  29. let contents = try!(reader.map(|e| e.map(|e| e.path())).collect());
  30. Ok(Dir {
  31. contents: contents,
  32. path: path.to_path_buf(),
  33. git: if git { Git::scan(path).ok() } else { None },
  34. })
  35. }
  36. /// Produce an iterator of IO results of trying to read all the files in
  37. /// this directory.
  38. pub fn files<'dir>(&'dir self) -> Files<'dir> {
  39. Files {
  40. inner: self.contents.iter(),
  41. dir: &self,
  42. }
  43. }
  44. /// Whether this directory contains a file with the given path.
  45. pub fn contains(&self, path: &Path) -> bool {
  46. self.contents.iter().any(|ref p| p.as_path() == path)
  47. }
  48. /// Append a path onto the path specified by this directory.
  49. pub fn join(&self, child: &Path) -> PathBuf {
  50. self.path.join(child)
  51. }
  52. /// Return whether there's a Git repository on or above this directory.
  53. pub fn has_git_repo(&self) -> bool {
  54. self.git.is_some()
  55. }
  56. /// Get a string describing the Git status of the given file.
  57. pub fn git_status(&self, path: &Path, prefix_lookup: bool) -> fields::Git {
  58. match (&self.git, prefix_lookup) {
  59. (&Some(ref git), false) => git.status(path),
  60. (&Some(ref git), true) => git.dir_status(path),
  61. (&None, _) => fields::Git::empty()
  62. }
  63. }
  64. }
  65. /// Iterator over reading the contents of a directory as `File` objects.
  66. pub struct Files<'dir> {
  67. inner: SliceIter<'dir, PathBuf>,
  68. dir: &'dir Dir,
  69. }
  70. impl<'dir> Iterator for Files<'dir> {
  71. type Item = Result<File<'dir>, (PathBuf, io::Error)>;
  72. fn next(&mut self) -> Option<Self::Item> {
  73. self.inner.next().map(|path| File::from_path(path, Some(self.dir)).map_err(|t| (path.clone(), t)))
  74. }
  75. }