Skip to content

Commit 34a6078

Browse files
committed
Refactor PyConfig get methods for improved readability and performance
1 parent 428839d commit 34a6078

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

src/config.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,17 @@ impl PyConfig {
5050

5151
/// Get a configuration option
5252
pub fn get<'py>(&self, key: &str, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
53-
let value = {
53+
let value: Option<Option<String>> = {
5454
let options = self.config.read();
5555
options
5656
.entries()
57-
.iter()
58-
.find(|entry| entry.key == key)
59-
.map(|entry| entry.value.clone())
57+
.into_iter()
58+
.find_map(|entry| (entry.key == key).then_some(entry.value.clone()))
6059
};
6160

62-
if let Some(value) = value {
63-
Ok(value.into_pyobject(py)?)
64-
} else {
65-
Ok(None::<String>.into_pyobject(py)?)
61+
match value {
62+
Some(value) => Ok(value.into_pyobject(py)?),
63+
None => Ok(None::<String>.into_pyobject(py)?),
6664
}
6765
}
6866

@@ -76,13 +74,13 @@ impl PyConfig {
7674

7775
/// Get all configuration options
7876
pub fn get_all(&self, py: Python) -> PyResult<PyObject> {
79-
let entries = {
77+
let entries: Vec<(String, Option<String>)> = {
8078
let options = self.config.read();
8179
options
8280
.entries()
8381
.into_iter()
84-
.map(|entry| (entry.key.to_string(), entry.value.clone()))
85-
.collect::<Vec<_>>()
82+
.map(|entry| (entry.key.clone(), entry.value.clone()))
83+
.collect()
8684
};
8785

8886
let dict = PyDict::new(py);

0 commit comments

Comments
 (0)