harper 0.0.2

A library to implement SSGs.
Documentation
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
use std::{fs, fmt};
use std::ops::Deref;
use std::sync::Arc;
use std::path::Path;
use std::collections::VecDeque;

use rustc_hash::FxHashMap;

use crate::error::Result;

#[derive(Debug)]
pub struct FsTree {
    entries: Vec<Entry>,
    map: FxHashMap<Arc<Path>, EntryId>,
}

#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct EntryId(usize);

#[derive(Clone)]
pub struct OwnedEntry {
    pub tree: Arc<FsTree>,
    pub id: EntryId,
}

#[derive(Debug)]
pub struct Entry {
    pub id: EntryId,
    pub path: Arc<Path>,
    pub metadata: fs::Metadata,
    pub file_name: String,
    pub file_type: fs::FileType,
    pub parent: Option<EntryId>,
    pub children: Vec<EntryId>,
    pub depth: usize,
}

#[derive(Default, Debug)]
struct FsMetadata(Option<fs::Metadata>);

impl FsTree {
    fn new() -> Self {
        Self {
            map: FxHashMap::default(),
            entries: vec![],
        }
    }

    #[inline(always)]
    pub fn build<P: AsRef<Path>>(root: P) -> Result<Self> {
        Self::build_with(root.as_ref(), |_, _| Ok(()))
    }

    #[inline]
    pub fn build_with<P, F>(root: P, mut callback: F) -> Result<Self>
        where P: AsRef<Path>,
              F: FnMut(&Self, EntryId) -> Result<()>,
    {
        use jwalk::WalkDirGeneric;

        let root = root.as_ref();
        let walker = WalkDirGeneric::<FsMetadata>::new(root)
            .follow_links(true)
            .process_read_dir(|_, _, _, entries| {
                entries.iter_mut()
                    .filter_map(|e| e.as_mut().ok())
                    .for_each(|e| e.client_state = FsMetadata(e.metadata().ok()))
            });

        let mut tree: FsTree = FsTree::new();
        for f in walker.into_iter().filter_map(|e| e.ok()).filter(|e| e.client_state.0.is_some()) {
            let id = tree.insert(f);
            callback(&mut tree, id)?;
        }

        if tree.len() == 0 {
            return err! {
                "file system tree discovery yielded zero files",
                "search root" => root.display(),
            }
        }

        Ok(tree)
    }

    pub fn len(&self) -> usize {
        self.entries.len()
    }

    pub fn root(&self) -> &Entry {
        &self[self.root_id()]
    }

    pub fn root_id(&self) -> EntryId {
        EntryId(0)
    }

    #[inline]
    pub fn get<R, P>(&self, root: R, path: P) -> Option<&Entry>
        where R: Into<Option<EntryId>>, P: AsRef<Path>
    {
        self.get_id(root.into(), path.as_ref()).map(|id| &self[id])
    }

    pub fn get_file_id<R, P>(&self, root: R, path: P) -> Option<EntryId>
        where R: Into<Option<EntryId>>, P: AsRef<Path>
    {
        let id = self.get_id(root.into(), path.as_ref())?;
        self[id].file_type.is_file().then_some(id)
    }

    pub fn get_id<R, P>(&self, root: R, path: P) -> Option<EntryId>
        where R: Into<Option<EntryId>>, P: AsRef<Path>
    {
        let root = root.into().unwrap_or(self.root_id());
        let full_path = self[root].path.join(path.as_ref());
        self.map.get(&*full_path).cloned()
    }

    pub fn ancestors_of(&self, mut entry: EntryId) -> impl Iterator<Item = EntryId> + '_ {
        std::iter::from_fn(move || {
            let parent = self[entry].parent?;
            entry = parent;
            Some(parent)
        })
    }

    pub fn iter(&self) -> impl Iterator<Item = &Entry> {
        (0..self.entries.len()).map(|i| &self[EntryId(i)])
    }

    pub fn iter_breadth_first(&self, root: EntryId) -> Bfs<'_> {
        Bfs {
            tree: self,
            root: Some(root),
            stack: VecDeque::new(),
            progress: 0
        }
    }

    pub fn iter_depth_first(&self, root: EntryId) -> Dfs<'_> {
        Dfs {
            tree: self,
            stack: { let mut q = VecDeque::new(); q.push_back(root); q },
        }
    }

    pub fn depth_first_search<F>(&self, root: EntryId, mut progress: F)
        where F: FnMut(&Entry) -> bool
    {
        fn _dfs<F: FnMut(&Entry) -> bool>(tree: &FsTree, root: EntryId, progress: &mut F) {
            let entry = &tree[root];
            if progress(entry) {
                for &child in &entry.children {
                    _dfs(tree, child, progress)
                }
            }
        }

        _dfs(self, root, &mut progress)
    }

    pub fn search<P: AsRef<Path>>(&self, path: P) -> Option<EntryId> {
        let root_path = &self[self.root_id()].path;
        for id in self.iter_breadth_first(self.root_id()) {
            let entry = &self[id];
            let suffix = entry.path.strip_prefix(&root_path).unwrap();
            if path.as_ref() == suffix {
                return Some(id);
            }
        }

        None
    }

    #[inline]
    pub fn search_entry<P: AsRef<Path>>(&self, path: P) -> Option<&Entry> {
        self.search(path.as_ref()).map(|id| &self[id])
    }

    /// Returns `true` `iff` `to` is or is a descendent of `from`.
    pub fn path_exists(&self, from: EntryId, mut to: EntryId) -> bool {
        // simple case: path determines if it descends
        if self[to].path.starts_with(&self[from].path) {
            return true;
        }

        // harder case: path isn't reliable (ie symlinks)
        loop {
            if from == to {
                return true;
            }

            if self[from].depth >= self[to].depth {
                return false;
            }

            match self[to].parent {
                Some(id) => to = id,
                None => return false
            }
        }
    }

    fn insert(&mut self, entry: jwalk::DirEntry<FsMetadata>) -> EntryId {
        let entry = Entry {
            id: EntryId(self.entries.len()),
            path: Arc::from(entry.path().into_boxed_path()),
            metadata: entry.client_state.0.unwrap(),
            file_type: entry.file_type,
            file_name: entry.file_name.to_string_lossy().into_owned(),
            parent: self.map.get(&entry.parent_path).cloned(),
            children: vec![],
            depth: entry.depth,
        };

        self.map.insert(entry.path.clone(), entry.id);
        if let Some(parent) = entry.parent {
            self.entries[parent.0].children.push(entry.id);
        }

		let id = entry.id;
        self.entries.push(entry);
		id
    }
}

impl OwnedEntry {
    pub fn new(tree: Arc<FsTree>, id: EntryId) -> Self {
        OwnedEntry { tree, id  }
    }

    pub fn entry(&self) -> &Entry {
        &self.tree[self.id]
    }
}

impl Deref for OwnedEntry {
    type Target = Entry;

    fn deref(&self) -> &Self::Target {
        self.entry()
    }
}

impl fmt::Debug for OwnedEntry {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.entry().fmt(f)
    }
}

impl PartialEq for OwnedEntry {
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id
    }
}

impl Eq for OwnedEntry { }

impl Entry {
    /// File name without the extension.
    pub fn file_stem(&self) -> &str {
        match self.file_name.rsplit_once('.') {
            Some((left, _)) => left,
            None => &self.file_name,
        }
    }

    /// The complete extension, if any.
    pub fn file_ext(&self) -> Option<&str> {
        self.file_name.rsplit_once('.').map(|(_, right)| right)
    }

    /// Path relative to the root tree of `self`.
    pub fn relative_path(&self) -> &Path {
        let mut components = self.path.components();
        for _ in 0..(self.path.components().count() - self.depth) {
            components.next();
        }

        components.as_path()
    }

    /// Path relative to `other`. `self` must be super-path of `other`.
    pub fn path_relative_to(&self, other: &Entry) -> Option<&Path> {
        if !self.path.starts_with(&other.path) {
            return None;
        }

        let n = self.depth - other.depth;
        let mut components = self.path.components();
        for _ in 0..(self.path.components().count() - n) {
            components.next();
        }

        Some(components.as_path())
    }
}

// This implementation is more memory efficient than the usual since it doesn't
// store all of the potential visits in the stack, instead only storing visits
// that can yield further children. This bounds the length of `stack`. The cost
// is a more complex implementation.
pub struct Bfs<'a> {
    tree: &'a FsTree,
    root: Option<EntryId>,
    stack: VecDeque<EntryId>,
    progress: usize,
}

impl Iterator for Bfs<'_> {
    type Item = EntryId;

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            if let Some(root) = self.root.take() {
                self.stack.push_front(root);
                return Some(root);
            }

            if self.stack.is_empty() {
                return None;
            }

            if let Some(&parent) = self.stack.front() {
                let children = &self.tree[parent].children;
                if self.progress < children.len() {
                    let node = children[self.progress];
                    if !self.tree[node].children.is_empty() {
                        self.stack.push_back(node);
                    }

                    self.progress += 1;
                    return Some(node)
                } else {
                    self.stack.pop_front();
                    self.progress = 0;
                }
            }
        }
    }
}

impl<'a> Bfs<'a> {
    #[inline]
    pub fn entries(self) -> impl Iterator<Item = &'a Entry> {
        let tree = self.tree;
        self.into_iter().map(move |id| &tree[id])
    }

    #[inline]
    pub fn files(self) -> impl Iterator<Item = &'a Entry> {
        let tree = self.tree;
        self.into_iter().map(move |id| &tree[id]).filter(|e| e.metadata.is_file())
    }
}

pub struct Dfs<'a> {
    tree: &'a FsTree,
    stack: VecDeque<EntryId>,
}

impl<'a> Dfs<'a> {
    #[inline]
    pub fn entries(self) -> impl Iterator<Item = &'a Entry> {
        let tree = self.tree;
        self.into_iter().map(move |id| &tree[id])
    }

    #[inline]
    pub fn files(self) -> impl Iterator<Item = &'a Entry> {
        let tree = self.tree;
        self.into_iter().map(move |id| &tree[id]).filter(|e| e.metadata.is_file())
    }
}

impl Iterator for Dfs<'_> {
    type Item = EntryId;

    fn next(&mut self) -> Option<Self::Item> {
        let node = self.stack.pop_front()?;
        for &child in &self.tree[node].children {
            self.stack.push_front(child);
        }

        Some(node)
    }
}

impl jwalk::ClientState for FsMetadata {
    type ReadDirState = ();
    type DirEntryState = Self;
}

impl std::ops::Index<EntryId> for FsTree {
    type Output = Entry;

    fn index(&self, index: EntryId) -> &Self::Output {
        &self.entries[index.0]
    }
}

impl std::ops::IndexMut<EntryId> for FsTree {
    fn index_mut(&mut self, index: EntryId) -> &mut Self::Output {
        &mut self.entries[index.0]
    }
}

impl fmt::Debug for EntryId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(f)
    }
}