2020-11-28 15:14:50 +03:00
|
|
|
use crate::{ExportError, WalkDirError};
|
2020-12-14 01:15:13 +03:00
|
|
|
use ignore::{DirEntry, Walk, WalkBuilder};
|
2020-11-28 15:14:50 +03:00
|
|
|
use snafu::ResultExt;
|
2020-12-14 01:15:13 +03:00
|
|
|
use std::fmt;
|
2020-11-28 15:14:50 +03:00
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
|
|
|
|
type Result<T, E = ExportError> = std::result::Result<T, E>;
|
2020-12-14 01:15:13 +03:00
|
|
|
type FilterFn = dyn Fn(&DirEntry) -> bool + Send + Sync + 'static;
|
2020-11-28 15:14:50 +03:00
|
|
|
|
2020-12-14 01:15:13 +03:00
|
|
|
#[derive(Clone)]
|
2020-12-23 02:12:16 +03:00
|
|
|
/// WalkOptions specifies how an Obsidian vault directory is scanned for eligible files to export.
|
2020-11-28 15:14:50 +03:00
|
|
|
pub struct WalkOptions<'a> {
|
2020-12-23 02:12:16 +03:00
|
|
|
/// The filename for ignore files, following the
|
|
|
|
/// [gitignore](https://git-scm.com/docs/gitignore) syntax.
|
|
|
|
///
|
|
|
|
/// By default `.export-ignore` is used.
|
2020-12-11 16:52:59 +03:00
|
|
|
pub ignore_filename: &'a str,
|
2020-12-23 02:12:16 +03:00
|
|
|
/// Whether to ignore hidden files.
|
|
|
|
///
|
|
|
|
/// This is enabled by default.
|
2020-12-11 16:52:59 +03:00
|
|
|
pub ignore_hidden: bool,
|
2020-12-23 02:12:16 +03:00
|
|
|
/// Whether to honor git's ignore rules (`.gitignore` files, `.git/config/exclude`, etc) if
|
|
|
|
/// the target is within a git repository.
|
|
|
|
///
|
|
|
|
/// This is enabled by default.
|
2020-12-11 16:52:59 +03:00
|
|
|
pub honor_gitignore: bool,
|
2020-12-23 02:12:16 +03:00
|
|
|
/// An optional custom filter function which is called for each directory entry to determine if
|
|
|
|
/// it should be included or not.
|
|
|
|
///
|
|
|
|
/// This is passed to [`ignore::WalkBuilder::filter_entry`].
|
2020-12-14 01:15:13 +03:00
|
|
|
pub filter_fn: Option<Box<&'static FilterFn>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> fmt::Debug for WalkOptions<'a> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
f.debug_struct("WalkOptions")
|
|
|
|
.field("ignore_filename", &self.ignore_filename)
|
|
|
|
.field("ignore_hidden", &self.ignore_hidden)
|
|
|
|
.field("honor_gitignore", &self.honor_gitignore)
|
|
|
|
.finish()
|
|
|
|
}
|
2020-11-28 15:14:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> WalkOptions<'a> {
|
2020-12-23 02:12:16 +03:00
|
|
|
/// Create a new set of options using default values.
|
2020-11-28 15:14:50 +03:00
|
|
|
pub fn new() -> WalkOptions<'a> {
|
|
|
|
WalkOptions {
|
|
|
|
ignore_filename: ".export-ignore",
|
|
|
|
ignore_hidden: true,
|
|
|
|
honor_gitignore: true,
|
2020-12-14 01:15:13 +03:00
|
|
|
filter_fn: None,
|
2020-11-28 15:14:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn build_walker(self, path: &Path) -> Walk {
|
2020-12-14 01:15:13 +03:00
|
|
|
let mut walker = WalkBuilder::new(path);
|
|
|
|
walker
|
2020-11-28 15:14:50 +03:00
|
|
|
.standard_filters(false)
|
|
|
|
.parents(true)
|
|
|
|
.hidden(self.ignore_hidden)
|
|
|
|
.add_custom_ignore_filename(self.ignore_filename)
|
|
|
|
.require_git(true)
|
|
|
|
.git_ignore(self.honor_gitignore)
|
|
|
|
.git_global(self.honor_gitignore)
|
2020-12-14 01:15:13 +03:00
|
|
|
.git_exclude(self.honor_gitignore);
|
|
|
|
|
|
|
|
if let Some(filter) = self.filter_fn {
|
|
|
|
walker.filter_entry(filter);
|
|
|
|
}
|
|
|
|
walker.build()
|
2020-11-28 15:14:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Default for WalkOptions<'a> {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-23 02:12:16 +03:00
|
|
|
/// `vault_contents` returns all of the files in an Obsidian vault located at `path` which would be
|
|
|
|
/// exported when using the given [WalkOptions].
|
2020-11-28 15:14:50 +03:00
|
|
|
pub fn vault_contents(path: &Path, opts: WalkOptions) -> Result<Vec<PathBuf>> {
|
|
|
|
let mut contents = Vec::new();
|
|
|
|
let walker = opts.build_walker(path);
|
|
|
|
for entry in walker {
|
|
|
|
let entry = entry.context(WalkDirError { path })?;
|
|
|
|
let path = entry.path();
|
|
|
|
let metadata = entry.metadata().context(WalkDirError { path })?;
|
|
|
|
|
|
|
|
if metadata.is_dir() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
contents.push(path.to_path_buf());
|
|
|
|
}
|
|
|
|
Ok(contents)
|
|
|
|
}
|