use super::{
DocumentLoader, LoadPolicy, LoadSpec, PreparedQueryRequest, QueryError, QueryExecutionError,
QueryInput, QueryRequest, QueryViewResult, ResolvedContent, project_query_view,
};
use mant_protocol::{CatalogQuery, DocumentCatalog};
pub struct DocumentResolver {
loader: DocumentLoader,
}
impl DocumentResolver {
#[must_use]
pub fn from_system() -> Self {
Self {
loader: DocumentLoader::from_system(),
}
}
pub fn resolve(
&self,
request: &QueryRequest,
policy: LoadPolicy,
) -> Result<ResolvedContent, QueryError> {
PreparedQueryRequest::new(request, policy)?.resolve(self)
}
pub(super) fn resolve_validated(
&self,
request: &QueryRequest,
policy: LoadPolicy,
) -> Result<ResolvedContent, QueryError> {
self.loader
.load(load_spec(&request.input), policy)
.map_err(QueryError::Load)
}
pub fn execute(
&self,
request: &QueryRequest,
policy: LoadPolicy,
) -> Result<QueryViewResult, QueryExecutionError> {
PreparedQueryRequest::new(request, policy)
.map_err(QueryExecutionError::Query)?
.execute(self)
}
pub(super) fn execute_validated(
&self,
request: &QueryRequest,
policy: LoadPolicy,
) -> Result<QueryViewResult, QueryExecutionError> {
let content = self
.resolve_validated(request, policy)
.map_err(QueryExecutionError::Query)?;
project_query_view(content, &request.view)
}
pub fn discover(&self, query: &CatalogQuery) -> Result<DocumentCatalog, String> {
self.loader.discover(query)
}
pub fn discover_prepared(
&self,
query: &mant_loader::PreparedCatalogQuery<'_>,
) -> Result<DocumentCatalog, String> {
self.loader.discover_prepared(query)
}
pub(crate) fn loader(&self) -> &DocumentLoader {
&self.loader
}
}
pub(super) fn load_spec(input: &QueryInput) -> LoadSpec<'_> {
match input {
QueryInput::Document {
selector,
source,
manual_section,
} => LoadSpec::Document {
selector,
source: source.as_deref(),
manual_section: manual_section.as_deref(),
},
QueryInput::File { path, format } => LoadSpec::File {
path,
format: *format,
},
}
}
#[cfg(test)]
pub(super) fn query_with(
request: &QueryRequest,
policy: LoadPolicy,
load: impl FnOnce() -> Result<ResolvedContent, QueryError>,
) -> Result<ResolvedContent, QueryError> {
PreparedQueryRequest::new(request, policy)?;
load()
}