obsidian_export/
walker.rs1use 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#[derive(Clone)]
14#[allow(clippy::exhaustive_structs)]
15pub struct WalkOptions<'a> {
16 pub ignore_filename: &'a str,
21 pub ignore_hidden: bool,
25 pub honor_gitignore: bool,
30 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 #[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
89pub 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}