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
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
use std::cell::RefCell;
use std::convert::AsRef;
use std::path::{Path,PathBuf};

use crate::fs_tree::FsTree;

///
/// Configure and build an `FsTree` iterator.
///
#[derive(Default)]
pub struct FsTreeBuilder
{
    path: PathBuf,
    ignore_files: Option<Vec<PathBuf>>,
    ignore_paths: Option<Vec<PathBuf>>,
    max_depth: Option<usize>,
    min_depth: usize
}

impl FsTreeBuilder
{
    ///
    /// Create a new `FsTreeBuilder` with a root directory of `path`.
    ///
    pub fn new<P>(path: P) -> FsTreeBuilder
        where P: AsRef<Path>
    {
        let path = path.as_ref().to_path_buf();
        FsTreeBuilder { path, ..Default::default() }
    }

    ///
    /// Ignore specified files in the iterator (chainable).
    ///
    pub fn ignore_files<P>(mut self, paths: &[P]) -> Self
        where P: AsRef<Path>
    {
        Self::_set_ignore_files(&mut self, paths);
        self
    }

    ///
    /// Ignore specified files in the iterator.
    ///
    pub fn set_ignore_files<P>(&mut self, paths: &[P])
        where P: AsRef<Path>
    {
        Self::_set_ignore_files(self, paths);
    }

    ///
    /// Ignore specified directory trees in the iterator (chainable).
    ///
    pub fn ignore_paths<P>(mut self, paths: &[P]) -> Self
        where P: AsRef<Path>
    {
        Self::_set_ignore_paths(&mut self, paths);
        self
    }

    ///
    /// Ignore specified directory trees in the iterator.
    ///
    pub fn set_ignore_paths<P>(&mut self, paths: &[P])
        where P: AsRef<Path>
    {
        Self::_set_ignore_paths(self, paths);
    }

    ///
    /// Set a maximum directory depth (chainable).
    ///
    pub fn max_depth(mut self, value: usize) -> Self {
        self.max_depth = Some(value);
        self
    }

    ///
    /// Set a maximum directory depth.
    ///
    pub fn set_max_depth(&mut self, value: usize) {
        self.max_depth = Some(value);
    }

    ///
    /// Set a minimum directory depth (chainable).
    ///
    pub fn min_depth(mut self, value: usize) -> Self {
        self.min_depth = value;
        self
    }

    ///
    /// Set a minimimum directory depth.
    ///
    pub fn set_min_depth(&mut self, value: usize) {
        self.min_depth = value;
    }

    ///
    /// Create the `FsTree` iterator.
    ///
    pub fn build(self) -> FsTree {
        FsTree {
            top: self.path,
            stack: RefCell::new(Vec::new()),
            ignore_files: self.ignore_files,
            ignore_paths: self.ignore_paths,
            max_depth: self.max_depth,
            min_depth: self.min_depth,
            ..Default::default()
        }
    }

    fn _set_ignore_files<P>(&mut self, paths: &[P])
        where P: AsRef<Path>
    {
        let ignore_files = Self::_path_buf_collect(paths);
        self.ignore_files = Some(ignore_files);
    }

    fn _set_ignore_paths<P>(&mut self, paths: &[P])
        where P: AsRef<Path>
    {
        let ignore_paths = Self::_path_buf_collect(paths);
        self.ignore_paths = Some(ignore_paths);
    }

    fn _path_buf_collect<P>(paths: &[P]) -> Vec<PathBuf>
        where P: AsRef<Path>
    {
        paths.iter().map(|p| {
            p.as_ref().to_path_buf()
        }).collect()
    }
}