use crate::cache::{CacheKey, NormalizedPath, Sha256Hash}; use crate::types::{RootId, Span}; use std::future::Future; use std::pin::Pin; use thiserror::Error; /// A caller-owned request issued at a type-1 include boundary. #[derive(Clone, Debug, Eq, PartialEq)] pub struct ResolveRequest { /// Full identity of the model containing the include. pub source: CacheKey, /// Strictly validated target path from the type-1 line. pub path: NormalizedPath, /// Root context selected by the caller's resolution policy. pub root: RootId, /// Active include depth before descending into the target. pub depth: u64, /// Source location of the include, when available. pub span: Option, } /// A resolver failure that is distinct from a successfully resolved but unavailable target. #[derive(Clone, Debug, Eq, PartialEq, Error)] pub enum ResolveError { #[error("materialization not found: {path} in {root:?}")] NotFound { path: NormalizedPath, root: RootId }, #[error("ambiguous materialization: {path} in {root:?}")] Ambiguous { path: NormalizedPath, root: RootId }, #[error("resolver rejected {path}: {message}")] Rejected { path: NormalizedPath, message: String, }, #[error("resolver backend failed for {path}: {message}")] Backend { path: NormalizedPath, message: String, }, } /// Owned bytes and the complete cache identity selected for one include. #[derive(Clone, Debug, Eq, PartialEq)] pub struct Materialization { pub path: NormalizedPath, pub root: RootId, pub content_hash: Sha256Hash, pub bytes: Vec, pub available: bool, pub provenance: Option, } impl Materialization { pub fn new( path: NormalizedPath, root: RootId, bytes: Vec, available: bool, provenance: Option, ) -> Self { let content_hash = CacheKey::new(path.clone(), root, &bytes).content_hash; Self { path, root, content_hash, bytes, available, provenance, } } pub fn unavailable(path: NormalizedPath, root: RootId, provenance: Option) -> Self { Self::new(path, root, Vec::new(), false, provenance) } pub fn with_hash( path: NormalizedPath, root: RootId, bytes: Vec, content_hash: Sha256Hash, available: bool, provenance: Option, ) -> Self { Self { path, root, content_hash, bytes, available, provenance, } } pub fn key(&self) -> CacheKey { CacheKey { canonical_path: self.path.clone(), resolved_root: self.root, content_hash: self.content_hash.clone(), } } pub fn has_valid_hash(&self) -> bool { CacheKey::new(self.path.clone(), self.root, &self.bytes).content_hash == self.content_hash } } /// Shared asynchronous resolution contract owned by the semantic core. /// /// Implementations may use a filesystem, SQLite, an HTTP client, or an in-memory /// table. The core never assumes how the bytes are obtained. pub trait Resolver { fn resolve( &self, request: ResolveRequest, ) -> Pin> + '_>>; } /// Ready resolver for callers that already have owned materializations. #[derive(Clone, Debug, Default)] pub struct ReadyResolver { entries: Vec, } impl ReadyResolver { pub fn new(entries: impl IntoIterator) -> Result { let mut resolver = Self::default(); for entry in entries { if resolver .entries .iter() .any(|existing| existing.path == entry.path && existing.root == entry.root) { return Err(ResolveError::Ambiguous { path: entry.path, root: entry.root, }); } resolver.entries.push(entry); } Ok(resolver) } pub fn entries(&self) -> &[Materialization] { &self.entries } } impl Resolver for ReadyResolver { fn resolve( &self, request: ResolveRequest, ) -> Pin> + '_>> { Box::pin(async move { self.entries .iter() .find(|entry| entry.path == request.path && entry.root == request.root) .cloned() .ok_or(ResolveError::NotFound { path: request.path, root: request.root, }) }) } }