duvet_core/
dir.rs

1// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4use crate::{glob::Glob, path::Path};
5use futures::Stream;
6use std::sync::Arc;
7
8pub mod walk;
9
10#[derive(Clone)]
11pub struct Directory {
12    pub(crate) path: Path,
13    pub(crate) contents: Arc<[Path]>,
14}
15
16impl Directory {
17    pub fn iter(&self) -> impl Iterator<Item = &Path> {
18        self.contents.iter()
19    }
20
21    pub fn walk(&self) -> impl Stream<Item = Path> {
22        walk::dir(self.path.clone())
23    }
24
25    pub fn glob(&self, include: Glob, ignore: Glob) -> impl Stream<Item = Path> {
26        walk::glob(self.path.clone(), include, ignore)
27    }
28}
29
30impl IntoIterator for Directory {
31    type Item = Path;
32    type IntoIter = DirIter;
33
34    fn into_iter(self) -> Self::IntoIter {
35        DirIter {
36            contents: self.contents,
37            index: 0,
38        }
39    }
40}
41
42pub struct DirIter {
43    contents: Arc<[Path]>,
44    index: usize,
45}
46
47impl Iterator for DirIter {
48    type Item = Path;
49
50    fn next(&mut self) -> Option<Self::Item> {
51        let index = self.index;
52        self.index += 1;
53        self.contents.get(index).cloned()
54    }
55}