obsidian_export/
walker.rs

1use std::fmt;
2use std::path::{Path, PathBuf};
3
4use ignore::{DirEntry, Walk, WalkBuilder};
5use snafu::ResultExt;
6
7use crate::{ExportError, WalkDirSnafu};
8
9type Result<T, E = ExportError> = std::result::Result<T, E>;
10type FilterFn = dyn Fn(&DirEntry) -> bool + Send + Sync + 'static;
11
12/// `WalkOptions` specifies how an Obsidian vault directory is scanned for eligible files to export.
13#[derive(Clone)]
14#[allow(clippy::exhaustive_structs)]
15pub struct WalkOptions<'a> {
16    /// The filename for ignore files, following the
17    /// [gitignore](https://git-scm.com/docs/gitignore) syntax.
18    ///
19    /// By default `.export-ignore` is used.
20    pub ignore_filename: &'a str,
21    /// Whether to ignore hidden files.
22    ///
23    /// This is enabled by default.
24    pub ignore_hidden: bool,
25    /// Whether to honor git's ignore rules (`.gitignore` files, `.git/config/exclude`, etc) if
26    /// the target is within a git repository.
27    ///
28    /// This is enabled by default.
29    pub honor_gitignore: bool,
30    /// An optional custom filter function which is called for each directory entry to determine if
31    /// it should be included or not.
32    ///
33    /// This is passed to [`ignore::WalkBuilder::filter_entry`].
34    pub filter_fn: Option<&'static FilterFn>,
35}
36
37impl<'a> fmt::Debug for WalkOptions<'a> {
38    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39        let filter_fn_fmt = match self.filter_fn {
40            Some(_) => "<function set>",
41            None => "<not set>",
42        };
43        f.debug_struct("WalkOptions")
44            .field("ignore_filename", &self.ignore_filename)
45            .field("ignore_hidden", &self.ignore_hidden)
46            .field("honor_gitignore", &self.honor_gitignore)
47            .field("filter_fn", &filter_fn_fmt)
48            .finish()
49    }
50}
51
52impl<'a> WalkOptions<'a> {
53    /// Create a new set of options using default values.
54    #[must_use]
55    pub fn new() -> Self {
56        WalkOptions {
57            ignore_filename: ".export-ignore",
58            ignore_hidden: true,
59            honor_gitignore: true,
60            filter_fn: None,
61        }
62    }
63
64    fn build_walker(self, path: &Path) -> Walk {
65        let mut walker = WalkBuilder::new(path);
66        walker
67            .standard_filters(false)
68            .parents(true)
69            .hidden(self.ignore_hidden)
70            .add_custom_ignore_filename(self.ignore_filename)
71            .require_git(true)
72            .git_ignore(self.honor_gitignore)
73            .git_global(self.honor_gitignore)
74            .git_exclude(self.honor_gitignore);
75
76        if let Some(filter) = self.filter_fn {
77            walker.filter_entry(filter);
78        }
79        walker.build()
80    }
81}
82
83impl<'a> Default for WalkOptions<'a> {
84    fn default() -> Self {
85        Self::new()
86    }
87}
88
89/// `vault_contents` returns all of the files in an Obsidian vault located at `path` which would be
90/// exported when using the given [`WalkOptions`].
91pub fn vault_contents(root: &Path, opts: WalkOptions<'_>) -> Result<Vec<PathBuf>> {
92    let mut contents = Vec::new();
93    let walker = opts.build_walker(root);
94    for entry in walker {
95        let entry = entry.context(WalkDirSnafu { path: root })?;
96        let path = entry.path();
97        let metadata = entry.metadata().context(WalkDirSnafu { path })?;
98
99        if metadata.is_dir() {
100            continue;
101        }
102        contents.push(path.to_path_buf());
103    }
104    Ok(contents)
105}