Transation between rust extraction type and Python dictionary
(py: Python, extraction: Extraction)
| 38 | |
| 39 | // Transation between rust extraction type and Python dictionary |
| 40 | fn pythonize(py: Python, extraction: Extraction) -> PyResult<Py<PyAny>> { |
| 41 | let refs = PyList::empty(py); |
| 42 | |
| 43 | for r in extraction.refs.iter() { |
| 44 | let pyref = PyDict::new(py); |
| 45 | pyref.set_item("name", &r.name)?; |
| 46 | if r.package.is_some() { |
| 47 | pyref.set_item("package", &r.package)?; |
| 48 | } |
| 49 | match &r.version { |
| 50 | Some(StringRV(s)) => pyref.set_item("version", s), |
| 51 | Some(IntRV(i)) => pyref.set_item("version", i), |
| 52 | Some(DoubleRV(d)) => pyref.set_item("version", d), |
| 53 | _ => PyResult::Ok(()), |
| 54 | }?; |
| 55 | |
| 56 | refs.append(pyref)?; |
| 57 | } |
| 58 | |
| 59 | let sources = PySet::new(py, &extraction.sources[..])?; |
| 60 | let py_configs: Vec<(String, Py<PyAny>)> = extraction |
| 61 | .configs |
| 62 | .into_iter() |
| 63 | .map(|(k, v)| (k, convert_config(py, v))) |
| 64 | .collect(); |
| 65 | let configs = PyList::new(py, &py_configs[..])?; |
| 66 | |
| 67 | let dict = PyDict::new(py); |
| 68 | dict.set_item("refs", refs)?; |
| 69 | dict.set_item("sources", sources)?; |
| 70 | dict.set_item("configs", configs)?; |
| 71 | Ok(dict.into_pyobject(py).unwrap().into()) |
| 72 | } |
| 73 | |
| 74 | // Naively converts a Result to a PyResult by using the string |
| 75 | // representation of the Rust exception as the message for ExtractionError. |
no test coverage detected