Skip to content

Commit ccf472f

Browse files
committed
Refactored metrics_py
1 parent 9602bef commit ccf472f

2 files changed

Lines changed: 11 additions & 51 deletions

File tree

src/lib.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ use std::path::PathBuf;
22

33
use pyo3::prelude::*;
44

5-
use pyo3::types::PyDict;
65
use rust_code_analysis::{AstCallback, AstCfg, AstPayload, LANG, action, guess_language};
76

87
use rust_code_analysis::{Callback, ParserTrait, rm_comments};
98
mod metrics;
10-
use metrics::{MetricsPayload, MetricsResponse, metrics_rust};
9+
use metrics::{MetricsPayload, metrics_rust};
1110
/// Unit structure to implement the `Callback` trait.
1211
#[derive(Debug)]
1312
pub struct CommentRemoval;
@@ -45,28 +44,21 @@ fn comment_removal(file_name: String, code: String) -> PyResult<String> {
4544
#[pyfunction]
4645
fn metrics_py(file_name: String, code: String, unit: bool) -> PyResult<Py<PyAny>> {
4746
let payload = MetricsPayload {
48-
id: "1".to_string(),
4947
file_name,
5048
code,
5149
unit,
5250
};
5351
let response = metrics_rust(payload);
5452

5553
response
56-
.map_err(|e| PyErr::new::<pyo3::exceptions::PyValueError, _>(e.error)) // Unknown language
57-
.and_then(|response| match response.spaces {
58-
None => Err(PyErr::new::<pyo3::exceptions::PyValueError, _>(
59-
"Failed to compute metrics",
60-
)),
61-
Some(_) => Ok(response),
62-
})
6354
.and_then(|response| {
6455
Python::with_gil(|py| {
6556
pythonize::pythonize(py, &response)
66-
.map_err(|e| PyErr::new::<pyo3::exceptions::PyValueError, _>(e.to_string()))
57+
.map_err(|e| e.to_string())
6758
.map(|v| v.into())
6859
})
6960
})
61+
.map_err(|e| PyErr::new::<pyo3::exceptions::PyValueError, _>(e))
7062
}
7163

7264
/// A Python module implemented in Rust.

src/metrics.rs

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
use std::path::PathBuf;
22

33
use rust_code_analysis::{guess_language, metrics, Callback, FuncSpace, ParserTrait, action};
4-
use serde::Serialize;
54

65

76
/// Payload containing source code used to compute metrics.
87
#[derive(Debug)]
98
pub struct MetricsPayload {
10-
/// Payload identifier.
11-
pub id: String,
129
/// Source code filename.
1310
pub file_name: String,
1411
/// Source code used to compute metrics.
@@ -19,29 +16,15 @@ pub struct MetricsPayload {
1916

2017
/// Server response containing metrics for every space present in
2118
/// the requested source code.
22-
#[derive(Debug, Serialize)]
23-
pub struct MetricsResponse {
24-
/// Server response identifier.
25-
pub id: String,
26-
/// Source code programming language.
27-
pub language: String,
28-
/// Metrics for every space contained in the requested source code.
29-
///
30-
/// If `None`, an error occurred processing the request.
31-
pub spaces: Option<FuncSpace>,
32-
}
19+
pub type MetricsResponse = Result<FuncSpace, String>;
3320

3421
/// Server request configuration.
3522
#[derive(Debug)]
3623
pub struct MetricsCfg {
37-
/// Request identifier.
38-
pub id: String,
3924
/// Path to the source file.
4025
pub path: PathBuf,
4126
/// Flag to consider only unit space metrics.
4227
pub unit: bool,
43-
/// Source code programming language.
44-
pub language: String,
4528
}
4629

4730
/// Unit structure to implement the `Callback` trait.
@@ -53,7 +36,7 @@ impl Callback for MetricsCallback {
5336

5437
fn call<T: ParserTrait>(cfg: Self::Cfg, parser: &T) -> Self::Res {
5538
let spaces = metrics(parser, &cfg.path);
56-
let spaces = if cfg.unit {
39+
let spaces =if cfg.unit {
5740
if let Some(mut spaces) = spaces {
5841
spaces.spaces.clear();
5942
Some(spaces)
@@ -63,43 +46,28 @@ impl Callback for MetricsCallback {
6346
} else {
6447
spaces
6548
};
66-
67-
MetricsResponse {
68-
id: cfg.id,
69-
language: cfg.language,
70-
spaces,
71-
}
49+
spaces.map_or(Err("Failed to compute metrics".to_string()), Ok)
7250
}
7351
}
7452

75-
#[derive(Debug)]
76-
pub struct Error {
77-
pub id: String,
78-
pub error: &'static str,
79-
}
8053

81-
pub fn metrics_rust(payload: MetricsPayload) -> Result<MetricsResponse, Error> {
54+
pub fn metrics_rust(payload: MetricsPayload) -> MetricsResponse {
8255
let path = PathBuf::from(&payload.file_name);
8356
let buf = payload.code.into_bytes();
84-
let (language, name) = guess_language(&buf, &path);
57+
let (language, _name) = guess_language(&buf, &path);
8558
if let Some(language) = language {
8659
let cfg = MetricsCfg {
87-
id: payload.id,
8860
path,
8961
unit: payload.unit,
90-
language: name.to_string(),
9162
};
92-
Ok(action::<MetricsCallback>(
63+
action::<MetricsCallback>(
9364
&language,
9465
buf,
9566
&PathBuf::from(""),
9667
None,
9768
cfg,
98-
))
69+
)
9970
} else {
100-
Err(Error {
101-
id: payload.id,
102-
error: "The file extension doesn't correspond to a valid language",
103-
})
71+
Err("The file extension doesn't correspond to a valid language".to_string())
10472
}
10573
}

0 commit comments

Comments
 (0)