1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use std::{fs::DirEntry, io, path::PathBuf};

pub mod index;
pub mod listing;

/// A directory; responds with the generated directory listing.
#[derive(Debug)]
pub struct Directory {
  /// Base directory
  pub base: PathBuf,
  /// Path of subdirectory to generate listing for
  pub path: PathBuf,
}

impl Directory {
  /// Create a new directory
  pub fn new(base: PathBuf, path: PathBuf) -> Directory {
    Directory { base, path }
  }

  /// Is this entry visible from this directory?
  pub fn is_visible(&self, entry: &io::Result<DirEntry>) -> bool {
    if let Ok(ref entry) = *entry {
      if let Some(name) = entry.file_name().to_str() {
        if name.starts_with('.') {
          return false;
        }
      }
      if let Ok(ref md) = entry.metadata() {
        let ft = md.file_type();
        return ft.is_dir() || ft.is_file() || ft.is_symlink();
      }
    }
    false
  }
}