Skip to content

Commit d29ce77

Browse files
authored
Document web module (#1041)
1 parent fbf98a1 commit d29ce77

File tree

7 files changed

+83
-12
lines changed

7 files changed

+83
-12
lines changed

rust-code-analysis-web/Cargo.toml

-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ keywords = ["metrics"]
88
description = "Run a web service to compute and export code metrics"
99
license = "MPL-2.0"
1010

11-
[[bin]]
12-
name = "rust-code-analysis-web"
13-
1411
[dependencies]
1512
actix-rt = "^2.6"
1613
actix-web = "^4.2"

rust-code-analysis-web/src/main.rs renamed to rust-code-analysis-web/src/bin/rust-code-analysis-web.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
1-
// After adding new fields for min and max in the json (server.rs line 630) this error arose:
2-
// error: recursion limit reached while expanding `json_internal!`
3-
// This solution was proposed as help by the compiler
4-
// for the full error details check here :https://github.com/mozilla/rust-code-analysis/pull/793#discussion_r817610530
5-
#![recursion_limit = "256"]
6-
mod web;
7-
81
use std::thread::available_parallelism;
92

103
use clap::Parser;
114

12-
use web::server;
5+
use rust_code_analysis_web::server::run;
136

147
#[derive(Parser, Debug)]
158
#[clap(
@@ -40,7 +33,7 @@ async fn main() {
4033
.get()
4134
});
4235

43-
if let Err(e) = server::run(&opts.host, opts.port, num_jobs).await {
36+
if let Err(e) = run(&opts.host, opts.port, num_jobs).await {
4437
eprintln!(
4538
"Cannot run the server at {}:{}: {}",
4639
opts.host, opts.port, e

rust-code-analysis-web/src/lib.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// After adding new fields for min and max in the json (server.rs line 630) this error arose:
2+
// error: recursion limit reached while expanding `json_internal!`
3+
// This solution was proposed as help by the compiler
4+
// for the full error details check here :https://github.com/mozilla/rust-code-analysis/pull/793#discussion_r817610530
5+
#![recursion_limit = "256"]
6+
pub mod web;
7+
pub use web::*;

rust-code-analysis-web/src/web/comment.rs

+14
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,42 @@ use serde::{Deserialize, Serialize};
22

33
use rust_code_analysis::{rm_comments, Callback, ParserTrait};
44

5+
/// Payload containing source code with comments to be removed.
56
#[derive(Debug, Deserialize, Serialize)]
67
pub struct WebCommentPayload {
8+
/// Payload identifier.
79
pub id: String,
10+
/// Source code filename.
811
pub file_name: String,
12+
/// Source code with comments to be removed.
913
pub code: String,
1014
}
1115

16+
/// Server response containing the source code without comments.
1217
#[derive(Debug, Serialize)]
1318
pub struct WebCommentResponse {
19+
/// Server response identifier.
1420
pub id: String,
21+
/// Source code without comments.
22+
///
23+
/// If `None`, an error occurred processing the request.
1524
pub code: Option<Vec<u8>>,
1625
}
1726

27+
/// Source code information.
1828
#[derive(Debug, Deserialize)]
1929
pub struct WebCommentInfo {
30+
/// Source code filename.
2031
pub file_name: String,
2132
}
2233

34+
/// Server request configuration.
2335
pub struct WebCommentCfg {
36+
/// Request identifier.
2437
pub id: String,
2538
}
2639

40+
/// Unit structure to implement the `Callback` trait.
2741
pub struct WebCommentCallback;
2842

2943
impl Callback for WebCommentCallback {

rust-code-analysis-web/src/web/function.rs

+12
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,40 @@ use serde_json::{self, Value};
33

44
use rust_code_analysis::{function, Callback, FunctionSpan, ParserTrait};
55

6+
/// Payload containing source code with function spans to be retrieved.
67
#[derive(Debug, Deserialize, Serialize)]
78
pub struct WebFunctionPayload {
9+
/// Payload identifier.
810
pub id: String,
11+
/// Source code filename.
912
pub file_name: String,
13+
/// Source code with function spans to be retrieved.
1014
pub code: String,
1115
}
1216

17+
/// Server response containing function spans for the requested source code.
1318
#[derive(Debug, Serialize)]
1419
pub struct WebFunctionResponse {
20+
/// Server response identifier.
1521
pub id: String,
22+
/// Function spans for the requested source code.
1623
pub spans: Vec<FunctionSpan>,
1724
}
1825

26+
/// Source code information.
1927
#[derive(Debug, Deserialize)]
2028
pub struct WebFunctionInfo {
29+
/// Source code filename.
2130
pub file_name: String,
2231
}
2332

33+
/// Server request configuration.
2434
pub struct WebFunctionCfg {
35+
/// Request identifier.
2536
pub id: String,
2637
}
2738

39+
/// Unit structure to implement the `Callback` trait.
2840
pub struct WebFunctionCallback;
2941

3042
impl Callback for WebFunctionCallback {

rust-code-analysis-web/src/web/metrics.rs

+24
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,58 @@ use std::path::PathBuf;
44

55
use rust_code_analysis::{metrics, Callback, FuncSpace, ParserTrait};
66

7+
/// Payload containing source code used to compute metrics.
78
#[derive(Debug, Deserialize, Serialize)]
89
pub struct WebMetricsPayload {
10+
/// Payload identifier.
911
pub id: String,
12+
/// Source code filename.
1013
pub file_name: String,
14+
/// Source code used to compute metrics.
1115
pub code: String,
16+
/// Flag to consider only unit space metrics.
1217
pub unit: bool,
1318
}
1419

20+
/// Server response containing metrics for every space present in
21+
/// the requested source code.
1522
#[derive(Debug, Serialize)]
1623
pub struct WebMetricsResponse {
24+
/// Server response identifier.
1725
pub id: String,
26+
/// Source code programming language.
1827
pub language: String,
28+
/// Metrics for every space contained in the requested source code.
29+
///
30+
/// If `None`, an error occurred processing the request.
1931
pub spaces: Option<FuncSpace>,
2032
}
2133

34+
/// Source code information.
2235
#[derive(Debug, Deserialize)]
2336
pub struct WebMetricsInfo {
37+
/// Source code filename.
2438
pub file_name: String,
39+
/// Unit space code.
40+
///
41+
///
42+
/// If `None`, the entire code is considered.
2543
pub unit: Option<String>,
2644
}
2745

46+
/// Server request configuration.
2847
pub struct WebMetricsCfg {
48+
/// Request identifier.
2949
pub id: String,
50+
/// Path to the source file.
3051
pub path: PathBuf,
52+
/// Flag to consider only unit space metrics.
3153
pub unit: bool,
54+
/// Source code programming language.
3255
pub language: String,
3356
}
3457

58+
/// Unit structure to implement the `Callback` trait.
3559
pub struct WebMetricsCallback;
3660

3761
impl Callback for WebMetricsCallback {

rust-code-analysis-web/src/web/server.rs

+24
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,30 @@ async fn ping() -> HttpResponse {
218218
HttpResponse::Ok().body(())
219219
}
220220

221+
/// Runs an HTTP Server which provides a series of services.
222+
///
223+
/// Each service corresponds to a functionality of the main library and can be
224+
/// accessed through a different route.
225+
///
226+
/// # Examples
227+
///
228+
/// ```no_run
229+
/// use rust_code_analysis_web::server::run;
230+
///
231+
/// #[actix_web::main]
232+
/// async fn main() {
233+
/// let host = "127.0.0.1";
234+
/// let port = 8080;
235+
/// let num_threads = 4;
236+
///
237+
/// // Runs a server on a determined host with a specific port and using a
238+
/// // certain number of threads.
239+
/// // If the server does not run correctly, an error will be shown.
240+
/// if let Err(e) = run(host, port, num_threads).await {
241+
/// eprintln!("Cannot run the server at {host}:{port}: {e}");
242+
/// }
243+
/// }
244+
/// ```
221245
pub async fn run(host: &str, port: u16, n_threads: usize) -> std::io::Result<()> {
222246
let max_size = 1024 * 1024 * 4;
223247

0 commit comments

Comments
 (0)