#[provable_contracts_macros::contract("pmat-core.yaml", equation = "path_exists")]
pub async fn analyze_rust_file(path: &Path) -> Result<FileContext, TemplateError> {
analyze_rust_file_with_cache(path, None).await
}
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "path_exists")]
pub async fn analyze_rust_file_with_cache(
path: &Path,
cache_manager: Option<Arc<SessionCacheManager>>,
) -> Result<FileContext, TemplateError> {
if let Some(cache) = cache_manager {
cache
.get_or_compute_ast(path, || async {
let content = tokio::fs::read_to_string(path)
.await
.map_err(|e| anyhow::anyhow!("Failed to read file: {e}"))?;
let syntax = syn::parse_file(&content)
.map_err(|e| anyhow::anyhow!("Failed to parse Rust file: {e}"))?;
let mut visitor = RustVisitor::new(content);
visitor.visit_file(&syntax);
Ok(FileContext {
path: path.display().to_string(),
language: "rust".to_string(),
items: visitor.items,
complexity_metrics: None,
})
})
.await
.map(|arc| (*arc).clone())
.map_err(|e| TemplateError::InvalidUtf8(e.to_string()))
} else {
let content = tokio::fs::read_to_string(path)
.await
.map_err(TemplateError::Io)?;
let syntax =
syn::parse_file(&content).map_err(|e| TemplateError::InvalidUtf8(e.to_string()))?;
let mut visitor = RustVisitor::new(content);
visitor.visit_file(&syntax);
Ok(FileContext {
path: path.display().to_string(),
language: "rust".to_string(),
items: visitor.items,
complexity_metrics: None,
})
}
}
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "path_exists")]
pub async fn analyze_rust_file_with_persistent_cache(
path: &Path,
cache_manager: Option<Arc<PersistentCacheManager>>,
) -> Result<FileContext, TemplateError> {
if let Some(cache) = cache_manager {
cache
.get_or_compute_ast(path, || async {
let content = tokio::fs::read_to_string(path)
.await
.map_err(|e| anyhow::anyhow!("Failed to read file: {e}"))?;
let syntax = syn::parse_file(&content)
.map_err(|e| anyhow::anyhow!("Failed to parse Rust file: {e}"))?;
let mut visitor = RustVisitor::new(content);
visitor.visit_file(&syntax);
Ok(FileContext {
path: path.display().to_string(),
language: "rust".to_string(),
items: visitor.items,
complexity_metrics: None,
})
})
.await
.map(|arc| (*arc).clone())
.map_err(|e| TemplateError::InvalidUtf8(e.to_string()))
} else {
let content = tokio::fs::read_to_string(path)
.await
.map_err(TemplateError::Io)?;
let syntax =
syn::parse_file(&content).map_err(|e| TemplateError::InvalidUtf8(e.to_string()))?;
let mut visitor = RustVisitor::new(content);
visitor.visit_file(&syntax);
Ok(FileContext {
path: path.display().to_string(),
language: "rust".to_string(),
items: visitor.items,
complexity_metrics: None,
})
}
}