-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathde.rs
More file actions
29 lines (22 loc) · 862 Bytes
/
Copy pathde.rs
File metadata and controls
29 lines (22 loc) · 862 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
use pyo3::prelude::*;
use pyo3::types::*;
use crate::cid::extract_cid;
fn hash_to_pydict<'py>(py: Python<'py>, cid: &::cid::Cid) -> PyResult<Bound<'py, PyDict>> {
let hash = cid.hash();
let dict_obj = PyDict::new(py);
dict_obj.set_item("code", hash.code())?;
dict_obj.set_item("size", hash.size())?;
dict_obj.set_item("digest", PyBytes::new(py, hash.digest()))?;
Ok(dict_obj)
}
fn to_pydict<'py>(py: Python<'py>, cid: &::cid::Cid) -> PyResult<Bound<'py, PyDict>> {
let dict_obj = PyDict::new(py);
dict_obj.set_item("version", cid.version() as u64)?;
dict_obj.set_item("codec", cid.codec())?;
dict_obj.set_item("hash", hash_to_pydict(py, cid)?)?;
Ok(dict_obj)
}
#[pyfunction]
pub fn decode_cid<'py>(py: Python<'py>, data: &Bound<PyAny>) -> PyResult<Bound<'py, PyDict>> {
to_pydict(py, &extract_cid(data)?)
}