Skip to content

Commit

Permalink
fix(python): add ChunkInfo import and fix type conversions
Browse files Browse the repository at this point in the history
This commit resolves build errors in the Python bindings:
- Add missing ChunkInfo import from crate root
- Fix type conversion in DataMap.infos() method
- Properly convert between ChunkInfo and Python tuple formats
- Maintain proper XorName conversions for hashes

These changes ensure correct type handling between Rust and Python while
maintaining the expected data structures on both sides.
  • Loading branch information
dirvine committed Nov 28, 2024
1 parent 8ba841f commit 73ea17e
Showing 1 changed file with 29 additions and 11 deletions.
40 changes: 29 additions & 11 deletions src/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,19 @@
/// return (Path("chunks") / hash_hex).read_bytes()
/// decrypt_from_storage(data_map, "output.dat", get_chunk)
/// ```
use pyo3::prelude::*;
use pyo3::types::{PyBytes, PyType};
use std::path::PathBuf;

use crate::{
decrypt as rust_decrypt, decrypt_from_storage as rust_decrypt_from_storage,
encrypt as rust_encrypt, encrypt_from_file as rust_encrypt_from_file,
shrink_data_map as rust_shrink_data_map,
streaming_decrypt_from_storage as rust_streaming_decrypt_from_storage,
streaming_encrypt_from_file as rust_streaming_encrypt_from_file,
verify_chunk as rust_verify_chunk,
decrypt_full_set as rust_decrypt_full_set, decrypt_from_storage as rust_decrypt_from_storage,
streaming_decrypt_from_storage as rust_streaming_decrypt_from_storage,
shrink_data_map as rust_shrink_data_map,
ChunkInfo, DataMap as RustDataMap, EncryptedChunk as RustEncryptedChunk, Error, Result,
};
use bytes::Bytes;
use pyo3::prelude::*;
use pyo3::types::{PyBytes, PyType};
use std::path::PathBuf;
use xor_name::XorName;

// Define Python types at module level
Expand All @@ -71,15 +71,33 @@ pub struct PyXorName {
impl PyDataMap {
#[new]
fn new(chunk_infos: Vec<(usize, Vec<u8>, Vec<u8>, usize)>) -> Self {
let infos = chunk_infos
.into_iter()
.map(|(index, dst_hash, src_hash, src_size)| ChunkInfo {
index,
dst_hash: XorName::from_content(&dst_hash),
src_hash: XorName::from_content(&src_hash),
src_size,
})
.collect();
Self {
inner: RustDataMap::new(chunk_infos),
inner: RustDataMap::new(infos),
}
}

#[staticmethod]
fn with_child(chunk_infos: Vec<(usize, Vec<u8>, Vec<u8>, usize)>, child: usize) -> Self {
let infos = chunk_infos
.into_iter()
.map(|(index, dst_hash, src_hash, src_size)| ChunkInfo {
index,
dst_hash: XorName::from_content(&dst_hash),
src_hash: XorName::from_content(&src_hash),
src_size,
})
.collect();
Self {
inner: RustDataMap::with_child(chunk_infos, child),
inner: RustDataMap::with_child(infos, child),
}
}

Expand Down Expand Up @@ -258,7 +276,7 @@ fn encrypt_from_file(input_path: String, output_dir: String) -> PyResult<(PyData
/// ```
fn decrypt(data_map: &PyDataMap, chunks: Vec<PyEncryptedChunk>) -> PyResult<Py<PyBytes>> {
let chunks: Vec<RustEncryptedChunk> = chunks.into_iter().map(|c| c.inner).collect();
let result = rust_decrypt(&data_map.inner, &chunks)
let result = rust_decrypt_full_set(&data_map.inner, &chunks)
.map_err(|e| PyErr::new::<pyo3::exceptions::PyValueError, _>(e.to_string()))?;

Python::with_gil(|py| Ok(PyBytes::new(py, &result).into()))
Expand Down Expand Up @@ -517,7 +535,7 @@ fn shrink_data_map(
}

#[pymodule]
fn _self_encryption(py: Python<'_>, m: &PyModule) -> PyResult<()> {
fn _self_encryption(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
m.add_class::<PyDataMap>()?;
m.add_class::<PyEncryptedChunk>()?;
m.add_class::<PyXorName>()?;
Expand Down

0 comments on commit 73ea17e

Please sign in to comment.