use std::path::PathBuf;
use swdir::{ScanOptions, scan_dir_with_options};
use crate::entry::LoadedEntry;
use crate::error::ScanIssue;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ScanRequest {
pub path: PathBuf,
pub generation: u32,
pub depth: u32,
}
#[derive(Debug, Clone, PartialEq)]
pub struct LoadPayload {
pub path: PathBuf,
pub generation: u32,
pub depth: u32,
pub result: Result<Vec<LoadedEntry>, ScanIssue>,
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct LoadedOutcome {
pub accepted: bool,
pub prefetch_requests: Vec<ScanRequest>,
}
impl LoadedOutcome {
pub(crate) fn discarded() -> Self {
Self::default()
}
pub(crate) fn accepted() -> Self {
Self {
accepted: true,
prefetch_requests: Vec::new(),
}
}
}
pub fn run(request: &ScanRequest) -> LoadPayload {
let result = scan_dir_with_options(&request.path, &ScanOptions::default())
.map(|entries| entries.iter().map(LoadedEntry::from).collect())
.map_err(ScanIssue::from);
LoadPayload {
path: request.path.clone(),
generation: request.generation,
depth: request.depth,
result,
}
}