Getting Started
Relevant source files
The following files were used as context for generating this wiki page:
rust-code-analysis is a multi-language library designed to analyze and extract structural and maintainability information from source code. It leverages Tree Sitter for incremental parsing and provides tools for computing static metrics, identifying code spans, and removing comments across various programming languages.
Sources: README.md:15-20, rust-code-analysis-book/src/README.md:3-7
The project is architected as a core Rust library with two primary interfaces: a Command Line Interface (rust-code-analysis-cli) for direct interaction and a Web server (rust-code-analysis-web) providing a REST API for remote analysis.
Sources: README.md:23-31, rust-code-analysis-book/src/commands/rest.md:3-5
Installation and Prerequisites
To develop or build the project, you must have the latest version of the Rust toolchain installed. The project is managed using cargo, the Rust package manager.
Sources: rust-code-analysis-book/src/developers/README.md:8-11
Cloning the Repository
The repository can be cloned via HTTPS or SSH:
# HTTPS
git clone -j8 https://github.com/mozilla/rust-code-analysis.git
# SSH
git clone -j8 git@github.com:mozilla/rust-code-analysis.git
Sources: rust-code-analysis-book/src/developers/README.md:15-27
Building the Project
The project supports building individual components or the entire workspace using standard cargo commands.
| Component | Build Command | Description |
|---|---|---|
| Core Library | cargo build | The primary analysis engine. |
| CLI Tool | cargo build -p rust-code-analysis-cli | Command-line utility for local analysis. |
| Web Server | cargo build -p rust-code-analysis-web | REST API server for remote analysis. |
| Workspace | cargo build --workspace | Builds all components in the repository. |
Sources: rust-code-analysis-book/src/developers/README.md:31-48, README.md:43-60
Using the CLI Tool
The rust-code-analysis-cli is the primary way to interact with the library locally. It supports various operations from AST dumping to metric computation.
CLI Workflow Diagram
The following diagram illustrates the typical data flow when using the CLI tool to analyze a file.
Common Commands
To view all available parameters and help:
cargo run -p rust-code-analysis-cli -- --help
Sources: rust-code-analysis-book/src/developers/README.md:104-106
To analyze metrics and export to JSON:
rust-code-analysis-cli --metrics --output-format json --output . --paths path/to/file.rs
Sources: rust-code-analysis-cli/README.md:57-59
Using the Web Service
rust-code-analysis-web provides a RESTful interface for the library.
Starting the Server
By default, the server runs on 127.0.0.1:8080.
rust-code-analysis-web --host 127.0.0.1 --port 9090
Sources: rust-code-analysis-book/src/commands/rest.md:12-14
Available Endpoints
| Endpoint | Method | Description |
|---|---|---|
/ping | GET | Health check to verify if the server is running. |
/comments | POST | Removes comments from provided code. |
/functions | POST | Retrieves function spans (start/end lines). |
/metrics | POST | Computes metrics for the provided source code. |
Sources: rust-code-analysis-book/src/commands/rest.md:20-80
REST API Interaction Flow
The web service processes requests by receiving code, parsing it via the core library, and returning a JSON payload.
Developer Guidelines
Contributors should adhere to the following workflow to ensure code quality:
- Testing: Always verify changes with
cargo test --workspace --all-features --verbose. Snapshot tests are managed viainsta. - Formatting: Install and run
cargo fmtto maintain style consistency. - Linting: Use
cargo clippyto catch common mistakes and fix all warnings before submitting pull requests. - Documentation: Generate internal API docs using
cargo doc --open --no-deps.
Sources: rust-code-analysis-book/src/developers/README.md:52-94, README.md:62-72
Summary
Getting started with rust-code-analysis involves setting up the Rust environment, cloning the workspace, and choosing between the CLI or Web interfaces depending on use-case requirements. The project provides a robust framework for static analysis across multiple languages, supported by standardized developer tools for testing and formatting.