Skip to main content

code_split_syn/
lib.rs

1mod crate_graph;
2mod ids;
3mod module_graph;
4
5use anyhow::{Context, Result};
6use cargo_metadata::MetadataCommand;
7use code_split_core::GraphBuilder;
8use std::path::Path;
9
10pub fn analyze(workspace: &Path, builder: &mut GraphBuilder) -> Result<()> {
11    analyze_with(workspace, builder, false)
12}
13
14/// Variant of [`analyze`] that passes `--no-deps` to `cargo metadata`.
15/// External crates are not enumerated and `metadata.resolve` is `None`,
16/// so the resulting graph contains only the workspace's local crates,
17/// their modules, files, and traits — no external crate nodes and no
18/// crate-level `Uses` edges into externals.
19///
20/// Use when third-party dependencies are unavailable (e.g. private git
21/// deps without credentials) or when the analysis intentionally
22/// focuses on the local code.
23pub fn analyze_local_only(workspace: &Path, builder: &mut GraphBuilder) -> Result<()> {
24    analyze_with(workspace, builder, true)
25}
26
27fn analyze_with(workspace: &Path, builder: &mut GraphBuilder, local_only: bool) -> Result<()> {
28    let manifest = workspace.join("Cargo.toml");
29    let mut cmd = MetadataCommand::new();
30    cmd.manifest_path(&manifest);
31    if local_only {
32        cmd.other_options(["--no-deps".to_string()]);
33    }
34    let metadata = cmd
35        .exec()
36        .with_context(|| format!("running cargo metadata for {}", manifest.display()))?;
37
38    crate_graph::contribute(&metadata, builder);
39    module_graph::contribute(&metadata, builder)?;
40    Ok(())
41}