REST API: Code Modification
Relevant source files
The following files were used as context for generating this wiki page:
The REST API: Code Modification feature set is provided by the rust-code-analysis-web component. This system allows users to programmatically alter or extract specific structural data from source code over HTTP, specifically focusing on comment removal and structural analysis such as retrieving function spans. It acts as a web-based interface for the core rust-code-analysis library, enabling remote code processing without requiring a local Rust environment.
Sources: rust-code-analysis-book/src/commands/rest.md, rust-code-analysis-web/README.md
System Architecture and Data Flow
The code modification system is built on the Actix-web framework. It receives source code via POST requests, guesses the programming language based on the provided file name and content, and executes specific "actions" through the core library.
Core Processing Flow
The server uses a centralized action dispatcher to bridge web requests with the underlying analysis engine. For code modification tasks like comment removal, the server often treats C++ files specifically using a Ccomment language variant to ensure accurate parsing.
The diagram above shows the logic within the server for determining how to process incoming code based on the requested modification type and format.
Sources: rust-code-analysis-web/src/web/server.rs:41-80
Comment Removal
The comment removal feature provides a "cleaner" version of the source code by stripping out inline and block comments while attempting to preserve the functional structure of the code.
Endpoint Specifications
The API supports both structured JSON communication and raw byte-stream processing for this feature.
| Feature | Method | Endpoint | Payload Requirement |
|---|---|---|---|
| JSON Comment Removal | POST | /comment | id, file_name, code |
| Plain Comment Removal | POST | /comment | file_name (Query Param), Raw Body |
Sources: rust-code-analysis-book/src/commands/rest.md, rust-code-analysis-web/src/web/server.rs:242-261
Implementation Logic
When a request is received, the server extracts the code buffer and invokes the WebCommentCallback. If the input is C++, the system explicitly maps it to LANG::Ccomment for the removal process.
The sequence above illustrates the interaction between the web server and the core library's comment_rm module.
Sources: rust-code-analysis-web/src/web/server.rs:41-68, src/lib.rs:114
Function Span Retrieval
This modification feature extracts the physical locations (start and end lines) of every function defined within the provided source code. This is essential for tools that need to slice code or perform targeted analysis on specific blocks.
Request/Response Structure
The function span retrieval is handled via the /function endpoint.
Sample Request Payload:
{
"id": "req-001",
"file_name": "main.py",
"code": "def hello():\n print('hi')"
}
Sample Response:
{
"id": "req-001",
"spans": [
{
"name": "hello",
"start_line": 1,
"end_line": 2
}
]
}
Sources: rust-code-analysis-book/src/commands/rest.md, rust-code-analysis-web/src/web/server.rs:563-585
Server Configuration
The rust-code-analysis-web server can be configured through command-line arguments to handle high-concurrency code modification tasks.
| Option | Argument | Default | Description |
|---|---|---|---|
| Host | --host | 127.0.0.1 | IP address to bind the server |
| Port | --port | 8080 | Port for the REST API |
| Jobs | -j | N/A | Number of parallel jobs/worker threads |
Sources: rust-code-analysis-book/src/commands/rest.md, rust-code-analysis-web/README.md
Conclusion
The REST API's code modification capabilities provide a robust, language-agnostic way to clean source code and identify its functional boundaries. By exposing the core library's comment_rm and function modules through Actix-web, the project enables integrated workflows where code can be pre-processed or segmented before further static analysis or storage.