Skip to main content

object_storage_proxy/parsers/
keystore.rs

1use std::collections::HashMap;
2
3use pyo3::{exceptions, prelude::*};
4
5/// Convert a Python list of `{"access_key": …, "secret_key": …}` dicts into a
6/// Rust `HashMap<access_key, secret_key>`.
7///
8/// # Errors
9///
10/// Returns a [`pyo3::PyErr`] if `hmac_list` cannot be extracted as
11/// `Vec<HashMap<String, String>>` or if any entry is missing `access_key` or
12/// `secret_key`.
13pub fn parse_hmac_list(py: Python, hmac_list: &PyObject) -> PyResult<HashMap<String, String>> {
14    // let list: Vec<HashMap<String, String>> = hmac_list.try_into().unwrap();
15    let list: Vec<HashMap<String, String>> = hmac_list.extract(py).expect("dict mismatch");
16    let mut map = HashMap::new();
17
18    for item in list {
19        let access_key: String = item
20            .get("access_key")
21            .ok_or_else(|| exceptions::PyKeyError::new_err("access_key not found"))?
22            .to_string();
23        let secret_key: String = item
24            .get("secret_key")
25            .ok_or_else(|| exceptions::PyKeyError::new_err("secret_key not found"))?
26            .to_string();
27        map.insert(access_key, secret_key);
28    }
29    Ok(map)
30}
31
32// #[cfg(test)]
33// mod tests {
34//     use super::*;
35//     use pyo3::types::{IntoPyDict, PyList};
36//     use pyo3::Python;
37
38//     #[test]
39//     fn test_parse_hmac_list_success() {
40//         Python::with_gil(|py| {
41//             let dict1 = [("access_key", "key1"), ("secret_key", "secret1")].into_py_dict(py);
42//             let dict2 = [("access_key", "key2"), ("secret_key", "secret2")].into_py_dict(py);
43//             let dict1 = dict1.expect("Failed to create dict1");
44//             let dict2 = dict2.expect("Failed to create dict2");
45//             let py_list: PyObject = PyList::new(py, &[dict1, dict2]).unwrap().into();
46
47//             let result = parse_hmac_list(py, &py_list).expect("Parsing should succeed");
48//             assert_eq!(result.get("key1").unwrap(), "secret1");
49//             assert_eq!(result.get("key2").unwrap(), "secret2");
50//         });
51//     }
52
53//     // #[test]
54//     // fn test_parse_hmac_list_missing_key() {
55//     //     Python::with_gil(|py| {
56//     //         let dict1 = [("access_key", "key1")].into_py_dict(py); // missing secret_key
57//     //         let dict1 = dict1.unwrap();
58//     //         let py_list = PyList::new(py, &[&dict1]);
59
60//     //         let result = parse_hmac_list(py, &py_list);
61//     //         assert!(result.is_err(), "Should error if secret_key is missing");
62//     //     });
63//     // }
64// }