mod visitor;
use std::path::Path;
use std::sync::mpsc::Sender;
use log::debug;
use crate::error::VaultError;
use crate::index::NoteIndex;
use crate::nfs::{self, VaultPath};
use crate::{NotesValidation, SearchResult};
use visitor::NoteListVisitorBuilder;
pub(crate) struct VaultSync<'a> {
index: &'a NoteIndex,
workspace_path: &'a Path,
}
impl<'a> VaultSync<'a> {
pub(crate) fn new(index: &'a NoteIndex, workspace_path: &'a Path) -> Self {
Self {
index,
workspace_path,
}
}
pub(crate) async fn run(
&self,
path: &VaultPath,
recursive: bool,
validation: NotesValidation,
sender: Option<Sender<SearchResult>>,
) -> Result<(), VaultError> {
debug!("Syncing subtree at {}", path);
let cached_notes = self.index.get_notes(path, recursive).await?;
let builder =
NoteListVisitorBuilder::new(self.workspace_path, validation, cached_notes, sender);
let walker = nfs::get_file_walker(self.workspace_path, path, recursive);
let builder = run_walker_blocking(walker, builder).await?;
self.index.apply(builder.into_diff()).await?;
Ok(())
}
}
async fn run_walker_blocking(
walker: ignore::WalkParallel,
builder: NoteListVisitorBuilder,
) -> Result<NoteListVisitorBuilder, VaultError> {
tokio::task::spawn_blocking(move || {
let mut builder = builder;
walker.visit(&mut builder);
builder
})
.await
.map_err(|e| VaultError::TaskJoin(format!("vault walker: {}", e)))
}