impl UnifiedRustAnalyzer {
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "path_exists")]
pub fn new(file_path: PathBuf) -> Self {
Self {
file_path,
#[cfg(test)]
parse_count: AtomicUsize::new(0),
}
}
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "path_exists")]
pub fn file_path(&self) -> &Path {
&self.file_path
}
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub async fn analyze(&self) -> Result<UnifiedAnalysis, AnalysisError> {
#[cfg(test)]
{
self.parse_count.fetch_add(1, Ordering::SeqCst);
}
let content = tokio::fs::read_to_string(&self.file_path)
.await
.map_err(AnalysisError::Io)?;
let syntax_tree =
syn::parse_file(&content).map_err(|e| AnalysisError::Parse(e.to_string()))?;
let ast_items = self.extract_ast_items(&syntax_tree);
let file_metrics = self.extract_complexity_metrics(&syntax_tree);
Ok(UnifiedAnalysis {
ast_items,
file_metrics,
parsed_at: std::time::Instant::now(),
})
}
#[cfg(test)]
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn parse_count(&self) -> usize {
self.parse_count.load(Ordering::SeqCst)
}
fn extract_ast_items(&self, syntax_tree: &syn::File) -> Vec<AstItem> {
let visitor = EnhancedAstVisitor::new(&self.file_path);
visitor.extract_items(syntax_tree)
}
}