Skip to main content

mant_engine/query/
adapter.rs

1//! Application adapters validate complete requests before loading or querying.
2use super::{
3    DocumentLoader, LoadPolicy, LoadSpec, PreparedQueryRequest, QueryError, QueryExecutionError,
4    QueryInput, QueryRequest, QueryViewResult, ResolvedContent, project_query_view,
5};
6use mant_protocol::{CatalogQuery, DocumentCatalog};
7
8/// Application composition over an explicit local document snapshot.
9pub struct DocumentResolver {
10    loader: DocumentLoader,
11}
12impl DocumentResolver {
13    /// Capture source discovery for related application operations.
14    #[must_use]
15    pub fn from_system() -> Self {
16        Self {
17            loader: DocumentLoader::from_system(),
18        }
19    }
20    /// Validate the entire request before resolving its source.
21    ///
22    /// # Errors
23    /// Returns loading or query-view validation errors.
24    pub fn resolve(
25        &self,
26        request: &QueryRequest,
27        policy: LoadPolicy,
28    ) -> Result<ResolvedContent, QueryError> {
29        PreparedQueryRequest::new(request, policy)?.resolve(self)
30    }
31    pub(super) fn resolve_validated(
32        &self,
33        request: &QueryRequest,
34        policy: LoadPolicy,
35    ) -> Result<ResolvedContent, QueryError> {
36        self.loader
37            .load(load_spec(&request.input), policy)
38            .map_err(QueryError::Load)
39    }
40    /// Load and materialize the view encoded by a complete request.
41    ///
42    /// # Errors
43    /// Returns loading, validation, projection or search failure.
44    pub fn execute(
45        &self,
46        request: &QueryRequest,
47        policy: LoadPolicy,
48    ) -> Result<QueryViewResult, QueryExecutionError> {
49        PreparedQueryRequest::new(request, policy)
50            .map_err(QueryExecutionError::Query)?
51            .execute(self)
52    }
53    pub(super) fn execute_validated(
54        &self,
55        request: &QueryRequest,
56        policy: LoadPolicy,
57    ) -> Result<QueryViewResult, QueryExecutionError> {
58        let content = self
59            .resolve_validated(request, policy)
60            .map_err(QueryExecutionError::Query)?;
61        project_query_view(content, &request.view)
62    }
63    /// Discover candidates from the same local snapshot.
64    ///
65    /// # Errors
66    /// Returns source configuration or catalog filter failures.
67    pub fn discover(&self, query: &CatalogQuery) -> Result<DocumentCatalog, String> {
68        self.loader.discover(query)
69    }
70
71    /// Discover with a query prepared before this environment was captured.
72    ///
73    /// # Errors
74    /// Returns source configuration or catalog acquisition failures.
75    pub fn discover_prepared(
76        &self,
77        query: &mant_loader::PreparedCatalogQuery<'_>,
78    ) -> Result<DocumentCatalog, String> {
79        self.loader.discover_prepared(query)
80    }
81    pub(crate) fn loader(&self) -> &DocumentLoader {
82        &self.loader
83    }
84}
85
86pub(super) fn load_spec(input: &QueryInput) -> LoadSpec<'_> {
87    match input {
88        QueryInput::Document {
89            selector,
90            source,
91            manual_section,
92        } => LoadSpec::Document {
93            selector,
94            source: source.as_deref(),
95            manual_section: manual_section.as_deref(),
96        },
97        QueryInput::File { path, format } => LoadSpec::File {
98            path,
99            format: *format,
100        },
101    }
102}
103
104#[cfg(test)]
105pub(super) fn query_with(
106    request: &QueryRequest,
107    policy: LoadPolicy,
108    load: impl FnOnce() -> Result<ResolvedContent, QueryError>,
109) -> Result<ResolvedContent, QueryError> {
110    PreparedQueryRequest::new(request, policy)?;
111    load()
112}