Skip to main content

code_split_syn/
lib.rs

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