1use std::path::Path;
2use std::sync::Arc;
3
4use crate::error::Diagnostic;
5use crate::graph::{CodeGraph, GraphBuilder, SccAnalysis};
6use crate::input;
7use crate::language::LangId;
8use crate::model::{FileExtraction, SnapshotId};
9
10#[derive(Debug, Clone)]
12pub struct SnapshotMeta {
13 pub id: SnapshotId,
14 pub datagraph_schema_version: u32,
15}
16
17pub struct GraphAnalysis {
19 pub graph: CodeGraph,
20 pub scc: SccAnalysis,
21 pub snapshot_id: SnapshotId,
22 pub extractions: Vec<Arc<crate::model::FileExtraction>>,
23 pub scope: crate::graph::resolver::FlattenedScopeCache,
25 pub references: Vec<crate::graph::resolver::ResolvedReference>,
27}
28
29pub fn build_analysis(
36 extractions: Vec<Arc<FileExtraction>>,
37 root: &Path,
38 snapshot_id: SnapshotId,
39) -> (GraphAnalysis, Vec<Diagnostic>) {
40 let mut diagnostics = Vec::new();
41 let parts =
42 GraphBuilder::from_extractions_detailed(&extractions, root, snapshot_id, &mut diagnostics);
43 diagnostics.sort_by(|a, b| a.sort_key().cmp(&b.sort_key()));
44
45 (
46 GraphAnalysis {
47 graph: parts.graph,
48 scc: parts.scc,
49 snapshot_id,
50 extractions,
51 scope: parts.scope,
52 references: parts.references,
53 },
54 diagnostics,
55 )
56}
57
58pub fn analyze_graph(
64 root: &Path,
65 snapshot_id: SnapshotId,
66 languages: Option<&[LangId]>,
67) -> anyhow::Result<(GraphAnalysis, Vec<Diagnostic>)> {
68 let files = input::discover_files(root, languages)?;
69 let extraction = crate::extractor::extract(&files);
70 let mut diagnostics: Vec<Diagnostic> = extraction
71 .files
72 .iter()
73 .flat_map(|f| f.diagnostics.iter().cloned())
74 .collect();
75
76 let arc_extractions: Vec<_> = extraction.files.into_iter().map(Arc::new).collect();
77
78 let (analysis, mut graph_diagnostics) = build_analysis(arc_extractions, root, snapshot_id);
79 diagnostics.append(&mut graph_diagnostics);
80 diagnostics.sort_by(|a, b| a.sort_key().cmp(&b.sort_key()));
81
82 Ok((analysis, diagnostics))
83}
84
85pub fn snapshot_meta(snapshot_id: SnapshotId) -> SnapshotMeta {
87 SnapshotMeta {
88 id: snapshot_id,
89 datagraph_schema_version: crate::output::graph::SCHEMA_VERSION,
90 }
91}