Metrics Output Formats
Relevant source files
The following files were used as context for generating this wiki page:
Introduction
The rust-code-analysis project provides a robust system for exporting static code metrics in various structured and human-readable formats. This functionality is primarily exposed through the rust-code-analysis-cli tool and the rust-code-analysis-web REST API, allowing developers to integrate code analysis results into CI/CD pipelines, documentation, or web services.
The system supports multiple serialization formats, including JSON, TOML, YAML, and CBOR, as well as a specialized terminal-based tree visualization. These formats capture complex hierarchical data generated during the analysis of Supported Languages and Supported Metrics.
Sources: README.md:12-25, rust-code-analysis-book/src/commands/metrics.md:13-23
Supported Serialization Formats
The CLI tool supports several standard formats for exporting metrics data. Users can specify the format using the -O or --output-format flag.
| Format | Description | CLI Parameter |
|---|---|---|
| JSON | JavaScript Object Notation. Supports pretty-printing with --pr. | json |
| TOML | Tom's Obvious, Minimal Language. Supports pretty-printing. | toml |
| YAML | YAML Ain't Markup Language. | yaml |
| CBOR | Concise Binary Object Representation for compact storage. | cbor |
Sources: rust-code-analysis-cli/README.md:38-40, rust-code-analysis-book/src/commands/metrics.md:13-33
Data Flow for Serialization
The following diagram illustrates how metrics data is processed from the raw source code to the final exported file.
The FuncSpace structure acts as the central data container before it is passed to specific serializers.
Sources: src/spaces.rs:107-128, rust-code-analysis-book/src/commands/rest.md:73-86
Terminal Tree Visualization
When using the CLI without a specific file output, the system uses the dump_root function to print a color-coded, hierarchical tree of the code's spaces and their associated metrics directly to the console.
Hierarchical Structure
The output follows the FuncSpace structure, where a Unit (typically the file) contains nested Class, Function, or Namespace spaces.
Visualization Logic
The dump_metrics module uses termcolor to differentiate components:
- Blue: Tree branch prefixes (
|-,\ -). - Yellow: Space kinds (e.g.,
function,class). - Cyan: Space names.
- Red: Line numbers (e.g.,
@12). - Green: Metric category headers.
- Magenta: Individual metric names.
- White: Metric values.
Sources: src/output/dump_metrics.rs:46-92, src/output/dump_metrics.rs:260-275
REST API JSON Schema
The rust-code-analysis-web tool provides metrics exclusively in JSON format. The response includes an id, the identified language, and a spaces object containing the hierarchical metrics.
Request Configuration
The API allows users to toggle the granularity of the output via the unit boolean field:
true: Compute only top-level (Unit) metrics.false: Detailed metrics across all functions, classes, and sub-units.
Sources: rust-code-analysis-book/src/commands/rest.md:73-95
Metrics Data Structures
The formats rely on the CodeMetrics and FuncSpace structs defined in src/spaces.rs. These structs derive serde::Serialize to facilitate the conversion to JSON, TOML, and other formats.
| Struct | Field | Description |
|---|---|---|
FuncSpace | name | The name of the function or space (e.g., "ConnectToUpdateServer"). |
FuncSpace | kind | Type of space: Function, Class, Struct, Trait, Impl, Unit, Namespace, or Interface. |
FuncSpace | metrics | A CodeMetrics object containing all computed values. |
CodeMetrics | loc | Lines of Code (SLOC, PLOC, LLOC, CLOC, BLANK). |
CodeMetrics | mi | Maintainability Index (Original, SEI, Visual Studio). |
CodeMetrics | nom | Number of Methods/Functions and Closures. |
Sources: src/spaces.rs:30-49, src/spaces.rs:69-92, src/spaces.rs:107-128
Conclusion
The metrics output system is designed for both human readability and machine interoperability. By offering a colorized terminal dump alongside standard structured formats like JSON and CBOR, the project ensures that code analysis data is accessible whether it is being reviewed by a developer in a terminal or processed by an automated quality gate.