use super::{Document, IndexFile, ParseMode, Parsed, parse_source};
use crate::{RepoPath, RepoRoot, Result, Source, source};
pub trait Loader {
fn load(&mut self, path: &RepoPath) -> Result<Source>;
fn parse(&mut self, source: &Source, mode: ParseMode) -> Parsed {
parse_source(source, mode)
}
fn document(&mut self, path: &RepoPath) -> Result<Option<Document>> {
self.full_document(path).map(Some)
}
fn full_document(&mut self, path: &RepoPath) -> Result<Document> {
let source = self.load(path)?;
let parsed = self.parse(&source, ParseMode::Document);
Ok(Document::new(source, parsed))
}
fn additional(&mut self, path: &RepoPath) -> Result<Option<Source>> {
self.load(path).map(Some)
}
fn schema(&mut self, path: &RepoPath) -> Result<Option<Document>> {
self.full_document(path).map(Some)
}
fn index(&mut self, path: &RepoPath) -> Result<Option<IndexFile>> {
let source = self.load(path)?;
Ok(Some(IndexFile {
path: path.clone(),
source,
}))
}
}
#[derive(Debug, Clone)]
pub struct FsLoader<'a> {
root: &'a RepoRoot,
}
impl<'a> FsLoader<'a> {
pub fn new(root: &'a RepoRoot) -> Self {
FsLoader { root }
}
}
impl Loader for FsLoader<'_> {
fn load(&mut self, path: &RepoPath) -> Result<Source> {
source::load(self.root, path)
}
}