Skip to main content

code_atlas/
lib.rs

1//! Repository analysis, treemap layout, and architectural rendering for Code Atlas.
2//!
3//! Most users should install and use the `code-atlas` command-line application.
4//! The library API is public for integrations, but remains experimental during
5//! the `0.x` release series.
6
7pub mod analyze;
8mod export;
9pub mod git;
10mod html;
11pub mod layout;
12pub mod model;
13pub mod render;
14pub mod render_wgpu;
15mod scip;
16
17use std::{path::Path, time::Instant};
18
19use anyhow::Result;
20
21use crate::{
22    analyze::analyze_calls,
23    git::scan_repository,
24    layout::{LayoutOptions, layout_repository},
25    model::{Atlas, Metric},
26    render::{RenderOptions, RenderStats, render_output},
27};
28
29#[derive(Debug, Clone)]
30pub struct BuildOptions {
31    pub metric: Metric,
32    pub analyze_calls: bool,
33    pub include_tests: bool,
34    pub include_hidden: bool,
35    pub excluded_paths: Vec<String>,
36}
37
38impl Default for BuildOptions {
39    fn default() -> Self {
40        Self {
41            metric: Metric::Loc,
42            analyze_calls: true,
43            include_tests: false,
44            include_hidden: false,
45            excluded_paths: Vec::new(),
46        }
47    }
48}
49
50pub fn build_atlas(repo: &Path, options: &BuildOptions) -> Result<Atlas> {
51    let scan_started = Instant::now();
52    let mut atlas = scan_repository(
53        repo,
54        options.metric,
55        options.include_tests,
56        options.include_hidden,
57        &options.excluded_paths,
58    )?;
59    atlas.timings.scan_ms = elapsed_ms(scan_started);
60    if options.analyze_calls {
61        let analyze_started = Instant::now();
62        analyze_calls(&mut atlas)?;
63        atlas.timings.analyze_ms = elapsed_ms(analyze_started);
64    }
65    Ok(atlas)
66}
67
68fn elapsed_ms(started: Instant) -> u64 {
69    started.elapsed().as_millis().min(u128::from(u64::MAX)) as u64
70}
71
72pub fn render_atlas(
73    atlas: &mut Atlas,
74    output: &Path,
75    layout_options: &LayoutOptions,
76    render_options: &RenderOptions,
77) -> Result<RenderStats> {
78    layout_repository(atlas, layout_options)?;
79    render_output(atlas, output, layout_options, render_options)
80}