From edacde08986f17aad9eb7f3caba5de9a3500d0bf Mon Sep 17 00:00:00 2001 From: Benno Zeeman Date: Wed, 5 Feb 2025 16:54:08 +0100 Subject: [PATCH] refactor: more docs and fixes for Python --- autonomi/src/python.rs | 54 +++++++++++++++--------------------------- 1 file changed, 19 insertions(+), 35 deletions(-) diff --git a/autonomi/src/python.rs b/autonomi/src/python.rs index 4663bae587..5e6e18045e 100644 --- a/autonomi/src/python.rs +++ b/autonomi/src/python.rs @@ -1,6 +1,5 @@ use crate::client::{ chunk::DataMapChunk, - files::{archive_private::PrivateArchiveAccess, archive_public::ArchiveAddr}, payment::PaymentOption, vault::{UserData, VaultSecretKey}, Client, @@ -298,15 +297,17 @@ pub struct PyPointerAddress { #[pymethods] impl PyPointerAddress { - /// Creates a new pointer address from a hex string. - #[new] - pub fn new(hex_str: String) -> PyResult { - let bytes = hex::decode(&hex_str) - .map_err(|e| PyValueError::new_err(format!("`hex_str` is invalid: {e:?}")))?; - let xorname = XorName::from_content(&bytes); + /// Initialise pointer address from hex string. + #[staticmethod] + pub fn from_hex(hex: String) -> PyResult { + let bytes = hex::decode(hex) + .map_err(|e| PyValueError::new_err(format!("`hex` not a valid hex string: {e}")))?; + let bytes: [u8; 32] = bytes + .try_into() + .map_err(|_| PyValueError::new_err("`hex` invalid: must be 32 bytes"))?; Ok(Self { - inner: PointerAddress::new(xorname), + inner: PointerAddress::new(XorName(bytes)), }) } @@ -370,11 +371,17 @@ pub struct PyPointerTarget { #[pymethods] impl PyPointerTarget { - /// Creates a new pointer target from a xorname byte array. - #[new] - fn new(xorname: &[u8]) -> PyResult { + /// Initialize a pointer target from a chunk address hex string. + #[staticmethod] + fn from_hex(hex: &str) -> PyResult { + let bytes = hex::decode(hex) + .map_err(|e| PyValueError::new_err(format!("`hex` not a valid hex string: {e}")))?; + let bytes: [u8; 32] = bytes + .try_into() + .map_err(|_| PyValueError::new_err("`hex` invalid: must be 32 bytes"))?; + Ok(Self { - inner: PointerTarget::ChunkAddress(ChunkAddress::new(XorName::from_content(xorname))), + inner: PointerTarget::ChunkAddress(ChunkAddress::new(XorName(bytes))), }) } @@ -392,14 +399,6 @@ impl PyPointerTarget { } } - /// Creates a pointer target from a xorname byte array. - #[staticmethod] - fn from_xorname(xorname: &[u8]) -> PyResult { - Ok(Self { - inner: PointerTarget::ChunkAddress(ChunkAddress::new(XorName::from_content(xorname))), - }) - } - /// Creates a pointer target from a chunk address. #[staticmethod] fn from_chunk_address(addr: &PyChunkAddress) -> Self { @@ -673,21 +672,6 @@ impl PyUserData { } } - fn add_file_archive(&mut self, archive: &str) -> Option { - let name = XorName::from_content(archive.as_bytes()); - let archive_addr = ArchiveAddr::from_content(&name); - self.inner.add_file_archive(archive_addr) - } - - fn add_private_file_archive(&mut self, archive: &str) -> Option { - let name = XorName::from_content(archive.as_bytes()); - let private_access = match PrivateArchiveAccess::from_hex(&name.to_string()) { - Ok(access) => access, - Err(_e) => return None, - }; - self.inner.add_private_file_archive(private_access) - } - /// Returns a list of public file archives as (address, name) pairs. fn file_archives(&self) -> Vec<(String, String)> { self.inner