Skip to content

Commit 9602bef

Browse files
committed
Still works
1 parent af2d2d5 commit 9602bef

1 file changed

Lines changed: 21 additions & 22 deletions

File tree

src/lib.rs

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ use pyo3::prelude::*;
55
use pyo3::types::PyDict;
66
use rust_code_analysis::{AstCallback, AstCfg, AstPayload, LANG, action, guess_language};
77

8-
98
use rust_code_analysis::{Callback, ParserTrait, rm_comments};
109
mod metrics;
11-
use metrics::{metrics_rust, MetricsPayload, MetricsResponse};
10+
use metrics::{MetricsPayload, MetricsResponse, metrics_rust};
1211
/// Unit structure to implement the `Callback` trait.
1312
#[derive(Debug)]
1413
pub struct CommentRemoval;
@@ -22,7 +21,6 @@ impl Callback for CommentRemoval {
2221
}
2322
}
2423

25-
2624
#[pyfunction]
2725
fn comment_removal(file_name: String, code: String) -> PyResult<String> {
2826
let path = PathBuf::from(file_name);
@@ -34,15 +32,13 @@ fn comment_removal(file_name: String, code: String) -> PyResult<String> {
3432
} else {
3533
language
3634
};
37-
action::<CommentRemoval>(
38-
&language,
39-
buf,
40-
&PathBuf::from(""),
41-
None,
42-
(),
43-
).ok_or_else(|| PyErr::new::<pyo3::exceptions::PyValueError, _>("Failed to remove comments"))
35+
action::<CommentRemoval>(&language, buf, &PathBuf::from(""), None, ()).ok_or_else(|| {
36+
PyErr::new::<pyo3::exceptions::PyValueError, _>("Failed to remove comments")
37+
})
4438
} else {
45-
Err(PyErr::new::<pyo3::exceptions::PyValueError, _>("Invalid language"))
39+
Err(PyErr::new::<pyo3::exceptions::PyValueError, _>(
40+
"Invalid language",
41+
))
4642
}
4743
}
4844

@@ -55,19 +51,22 @@ fn metrics_py(file_name: String, code: String, unit: bool) -> PyResult<Py<PyAny>
5551
unit,
5652
};
5753
let response = metrics_rust(payload);
58-
Python::with_gil(|py| {
59-
response.map_err(|e| PyErr::new::<pyo3::exceptions::PyValueError, _>(e.error))
54+
55+
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+
})
6063
.and_then(|response| {
61-
match response.spaces {
62-
None => Err(PyErr::new::<pyo3::exceptions::PyValueError, _>("Failed to compute metrics")),
63-
Some(_) => Ok(response)
64-
}
65-
}).and_then(|response| {
66-
pythonize::pythonize(py, &response)
67-
.map_err(|e| PyErr::new::<pyo3::exceptions::PyValueError, _>(e.to_string()))
68-
.map(|v| v.into())
64+
Python::with_gil(|py| {
65+
pythonize::pythonize(py, &response)
66+
.map_err(|e| PyErr::new::<pyo3::exceptions::PyValueError, _>(e.to_string()))
67+
.map(|v| v.into())
68+
})
6969
})
70-
})
7170
}
7271

7372
/// A Python module implemented in Rust.

0 commit comments

Comments
 (0)