Project Overview
Relevant source files
The following files were used as context for generating this wiki page:
rust-code-analysis is a comprehensive Rust library and toolsuite designed to analyze and extract structural and maintainability information from source code across multiple programming languages. It leverages the Tree Sitter incremental parsing library to generate Abstract Syntax Trees (AST) for precise static analysis.
Sources: README.md:10-15, src/lib.rs:1-5
Core Architecture
The project is organized as a workspace comprising a core library and specialized interfaces for CLI and Web environments.
Figure 1: High-level architectural relationship between the core library, CLI, and Web components.
Components
- Core Library (
rust-code-analysis): The engine responsible for parsing code via Tree Sitter, traversing ASTs, and calculating various static metrics. - CLI Tool (
rust-code-analysis-cli): A command-line interface for batch processing files, dumping ASTs, and exporting metrics in formats like JSON, TOML, YAML, or CBOR. - Web Service (
rust-code-analysis-web): A REST API server (built on Actix-Web) that provides remote analysis capabilities including comment removal and metrics computation.
Sources: README.md:18-28, rust-code-analysis-cli/src/main.rs:253-264, rust-code-analysis-book/src/commands/README.md:27-30
Static Metrics Engine
The system computes "static metrics"—quantitative measures derived without executing the code—to evaluate code quality and maintainability.
Supported Metrics Categories
| Metric Category | Description |
|---|---|
| Complexity | CC (Cyclomatic Complexity) and Cognitive Complexity. |
| Size/LOC | SLOC (Source), PLOC (Physical), LLOC (Logical), BLANK (Blank lines), CLOC (Comments). |
| Object Oriented | WMC (Weighted Methods per Class), NPM (Public Methods), NPA (Public Attributes). |
| Maintainability | MI (Maintainability Index), Halstead Suite (Effort, Time, Bugs). |
| Structural | NOM (Number of Methods), NARGS (Number of Arguments), NEXITS (Exit points). |
Sources: rust-code-analysis-book/src/metrics.md:3-31, src/lib.rs:15-38
Workflow & Data Flow
The analysis process follows a linear transformation from raw source code to structured metrics.
Figure 2: Data flow from raw source input to final metric output.
- Language Detection: Uses file extensions or Emacs modes to determine the correct Tree Sitter grammar.
- Parsing: The library invokes the specific language parser to build an AST.
- Traversal: Internal utilities like
traverse_childrenandget_parentnavigate the tree. - Computation: Metric-specific traits (e.g.,
Loc,Cyclomatic,Halstead) process nodes to accumulate stats.
Sources: rust-code-analysis-cli/src/main.rs:77-85, src/lib.rs:80-81, rust-code-analysis-book/src/commands/README.md:17-25
Supported Languages
The project supports a wide array of languages by integrating their respective Tree Sitter grammars:
- C/C++: Including Firefox-specific
mozcpp. - Java / Kotlin: Comprehensive support for JVM languages.
- JavaScript / TypeScript: Includes
tsxand Firefox-internalmozjs. - Python / Rust / Go / C#: Standard system and scripting languages.
- Web: HTML and CSS.
Sources: src/lib.rs:15-28, Cargo.toml:26-36
Developer Interfaces
Command Line Interface (CLI)
The CLI supports several operational modes:
--metrics: Standard metrics computation.--dump: Prints the AST structure to stdout.--comments: Removes comments from files (often used for minification/pre-processing).--find: Locates specific node types within the AST.
Sources: rust-code-analysis-cli/README.md:19-41
REST API Endpoints
The web server exposes the following primary endpoints:
GET /ping: Server health check.POST /comments: Receives code, returns it without comments.POST /functions: Returns spans (start/end lines) for all functions.POST /metrics: Returns a full JSON object of computed metrics.
Sources: rust-code-analysis-book/src/commands/rest.md:21-70
Building and Environment
The project requires the latest version of Rust. It uses a workspace-based build system.
- Build Library:
cargo build - Build CLI:
cargo build -p rust-code-analysis-cli - Build Web:
cargo build -p rust-code-analysis-web - Testing:
cargo test --workspace --all-features
Sources: rust-code-analysis-book/src/developers/README.md:12-32
Summary
Project Overview serves as the foundational framework for rust-code-analysis, enabling developers to perform multi-language code audits through a unified metrics engine. By abstracting the complexities of language-specific grammars into a common AST-based processing pipeline, it provides consistent maintainability data across diverse codebases.