From 5381cc027d52b7279cc72e3b1645aea8027650be Mon Sep 17 00:00:00 2001 From: "Christopher R. Wood" Date: Thu, 29 Aug 2024 15:56:51 -0400 Subject: [PATCH] Use newer declarative syntax for pyo3 (sub)modules --- src/lib.rs | 110 ++++++++++++++++++++++++++--------------------------- 1 file changed, 53 insertions(+), 57 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index f6ad84c..308d161 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,68 +11,64 @@ pub use crate::util::base32; pub use crate::util::hashutil; pub use crate::util::netstring; -#[pyfunction] -#[pyo3(name = "b2a")] -fn base32_b2a(py: Python, b: &[u8]) -> PyObject { - let result = base32::b2a(b); - PyBytes::new_bound(py, &result).into() -} +#[pymodule] +mod lafs { + use super::*; + + #[pymodule] + mod util { + use super::*; + + #[pymodule] + mod base32 { + use super::*; + use crate::util::base32; + + #[pyfunction] + fn b2a(py: Python, b: &[u8]) -> PyObject { + let result = base32::b2a(b); + PyBytes::new_bound(py, &result).into() + } + } -#[pyfunction] -#[pyo3(name = "tagged_hash")] -#[pyo3(signature = (tag, val, truncate_to = 32))] -fn hashutil_tagged_hash(py: Python, tag: &[u8], val: &[u8], truncate_to: usize) -> PyObject { - let result = hashutil::tagged_hash(tag, val, truncate_to); - PyBytes::new_bound(py, &result).into() -} + #[pymodule] + mod hashutil { + use super::*; + use crate::util::hashutil; -#[pyfunction] -#[pyo3(name = "ssk_writekey_hash")] -fn hashutil_ssk_writekey_hash(py: Python, privkey: &[u8]) -> PyObject { - let result = hashutil::ssk_writekey_hash(privkey); - PyBytes::new_bound(py, &result).into() -} + #[pyfunction] + #[pyo3(signature = (tag, val, truncate_to = 32))] + fn tagged_hash(py: Python, tag: &[u8], val: &[u8], truncate_to: usize) -> PyObject { + let result = hashutil::tagged_hash(tag, val, truncate_to); + PyBytes::new_bound(py, &result).into() + } -#[pyfunction] -#[pyo3(name = "ssk_pubkey_fingerprint_hash")] -fn hashutil_ssk_pubkey_fingerprint_hash(py: Python, pubkey: &[u8]) -> PyObject { - let result = hashutil::ssk_pubkey_fingerprint_hash(pubkey); - PyBytes::new_bound(py, &result).into() -} + #[pyfunction] + fn ssk_writekey_hash(py: Python, privkey: &[u8]) -> PyObject { + let result = hashutil::ssk_writekey_hash(privkey); + PyBytes::new_bound(py, &result).into() + } -#[pyfunction] -#[pyo3(name = "netstring")] -fn netstring_netstring(py: Python, s: &[u8]) -> PyObject { - let result = netstring::netstring(s); - PyBytes::new_bound(py, &result).into() -} + #[pyfunction] + fn ssk_pubkey_fingerprint_hash(py: Python, pubkey: &[u8]) -> PyObject { + let result = hashutil::ssk_pubkey_fingerprint_hash(pubkey); + PyBytes::new_bound(py, &result).into() + } + } -#[pymodule] -fn lafs(m: &Bound<'_, PyModule>) -> PyResult<()> { - let base32_module = PyModule::new_bound(m.py(), "base32")?; - base32_module.add_function(wrap_pyfunction!(base32_b2a, &base32_module)?)?; - - let hashutil_module = PyModule::new_bound(m.py(), "hashutil")?; - hashutil_module.add_function(wrap_pyfunction!(hashutil_tagged_hash, &hashutil_module)?)?; - hashutil_module.add_function(wrap_pyfunction!( - hashutil_ssk_writekey_hash, - &hashutil_module - )?)?; - hashutil_module.add_function(wrap_pyfunction!( - hashutil_ssk_pubkey_fingerprint_hash, - &hashutil_module - )?)?; - - let netstring_module = PyModule::new_bound(m.py(), "netstring")?; - netstring_module.add_function(wrap_pyfunction!(netstring_netstring, &base32_module)?)?; - - let util_module = PyModule::new_bound(m.py(), "util")?; - util_module.add_submodule(&base32_module)?; - util_module.add_submodule(&hashutil_module)?; - util_module.add_submodule(&netstring_module)?; - m.add_submodule(&util_module)?; - - Ok(()) + #[pymodule] + mod netstring { + use super::*; + use crate::util::netstring; + + #[pyfunction] + #[pyo3(name = "netstring")] + fn py_netstring(py: Python, s: &[u8]) -> PyObject { + let result = netstring::netstring(s); + PyBytes::new_bound(py, &result).into() + } + } + } } pub fn derive_lafs_mutable(private_key_pem: &str, format: &str) -> String {