REST API: Metrics Computation
Relevant source files
The following files were used as context for generating this wiki page:
Introduction
The rust-code-analysis-web tool provides a web server that enables source code analysis through a RESTful API. This system allows users to compute a wide variety of static code metrics over HTTP, facilitating remote analysis without requiring a local installation of the CLI or library. The server is built using the Actix-web framework and supports concurrent request processing.
The core purpose of the metrics computation endpoint is to receive source code and a filename, identify the programming language using Tree Sitter-based incremental parsing, and return quantitative measures such as Cyclomatic Complexity, Lines of Code (LoC), and Halstead metrics in a structured JSON format.
Sources: rust-code-analysis-book/src/commands/rest.md:3-8, rust-code-analysis-web/README.md:3-10, rust-code-analysis-web/Cargo.toml:13-14
System Architecture
The REST API architecture follows a standard client-server model where requests are handled by specific route handlers. The rust-code-analysis-web crate acts as a wrapper around the core rust-code-analysis library, translating HTTP payloads into internal analysis calls.
Server Components
The server is initialized with configurable host, port, and worker thread settings. It employs guards to ensure that incoming requests have the correct content-type headers, supporting both application/json and application/octet-stream for different interaction styles.
Sources: rust-code-analysis-web/src/web/server.rs:242-290, rust-code-analysis-web/README.md:20-30
Request Data Flow
When a metrics computation request is received, the system follows a specific processing pipeline:
The diagram shows the branching logic based on content headers and the subsequent language identification and metric execution.
Sources: rust-code-analysis-web/src/web/server.rs:107-160
API Endpoints and Payloads
The primary endpoint for metrics is /metrics. It supports two main modes of operation: JSON-based structured requests and plain-text/binary uploads via query parameters.
JSON Metrics Endpoint
URL: POST /metrics
Header: Content-Type: application/json
| Field | Type | Description |
|---|---|---|
id | String | A unique identifier for the specific request. |
file_name | String | The name of the file (used for language detection). |
code | String | The raw source code to be analyzed. |
unit | Boolean | true for top-level metrics only; false for detailed metrics per unit. |
Sources: rust-code-analysis-book/src/commands/rest.md:95-108, rust-code-analysis-web/src/web/server.rs:107-111
Plain/Binary Metrics Endpoint
URL: POST /metrics
Header: Content-Type: application/octet-stream
Parameters: Requires file_name and optionally unit as query strings.
Sources: rust-code-analysis-web/src/web/server.rs:134-140
Computed Metrics Overview
The API returns a comprehensive suite of static metrics. These are calculated based on the AST (Abstract Syntax Tree) generated by Tree Sitter.
| Metric Code | Full Name | Description |
|---|---|---|
| CC | Cyclomatic Complexity | Measures the control flow complexity. |
| SLOC/PLOC | Source/Physical Lines of Code | Counts raw lines and instruction lines. |
| HALSTEAD | Halstead Suite | Estimates effort, difficulty, and implementation time. |
| MI | Maintainability Index | Provides an overall software maintainability score. |
| NOM | Number of Methods | Counts functions and closures. |
| COGNITIVE | Cognitive Complexity | Measures how difficult the code is to understand. |
Sources: rust-code-analysis-book/src/metrics.md:3-23, rust-code-analysis-web/src/web/server.rs:600-650
Internal Logic and Implementation
Language Detection
The server uses guess_language to identify the programming language from the provided buffer and file extension. If a language cannot be identified, the API returns a 404 Not Found error with a specific message: "The file extension doesn't correspond to a valid language".
Sources: rust-code-analysis-web/src/web/server.rs:16-17, rust-code-analysis-web/src/web/server.rs:112-115
Processing Actions
The system uses an action pattern with a WebMetricsCallback. This generic execution interface ensures that the library metrics are computed consistently regardless of the input source (CLI or Web).
// Snippet of metrics_json handler
let cfg = WebMetricsCfg {
id: payload.id,
path,
unit: payload.unit,
language: name.to_string(),
};
HttpResponse::Ok().json(action::<WebMetricsCallback>(
&language,
buf,
&PathBuf::from(""),
None,
cfg,
))
Sources: rust-code-analysis-web/src/web/server.rs:116-128
Conclusion
REST API Metrics Computation within rust-code-analysis-web serves as a high-performance gateway to the underlying library's static analysis capabilities. By supporting various content types and offering detailed control over the granularity of metrics (via the unit parameter), it provides a flexible interface for integrating code quality analysis into CI/CD pipelines and web-based developer tools.
Sources: rust-code-analysis-web/README.md:1-15, rust-code-analysis-book/src/commands/rest.md:120-135