From 6d4e82b0b7811883f4ef3ef591838b76dc501175 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Fri, 1 Mar 2024 20:44:14 +0100 Subject: [PATCH] python bintols --- members/libpt-py/src/bintols/mod.rs | 56 +++++++++++++++++++++++++++++ members/libpt-py/src/lib.rs | 5 +++ 2 files changed, 61 insertions(+) create mode 100644 members/libpt-py/src/bintols/mod.rs diff --git a/members/libpt-py/src/bintols/mod.rs b/members/libpt-py/src/bintols/mod.rs new file mode 100644 index 0000000..ea0403f --- /dev/null +++ b/members/libpt-py/src/bintols/mod.rs @@ -0,0 +1,56 @@ +use pyo3::prelude::*; + +use libpt::bintols as origin; + +mod display { + use pyo3::prelude::*; + + use libpt::bintols::display as origin; + + #[pyfunction] + pub fn bytes_to_bin(data: &[u8]) -> String { + origin::bytes_to_bin(data) + } + + #[pyfunction] + pub fn byte_bit_display(data: usize) -> String { + origin::byte_bit_display(data) + } + + #[pyfunction] + pub fn humanbytes(total: u128) -> String { + origin::humanbytes(total) + } + + /// implement a python module in Rust + pub fn submodule(py: Python, parent: &PyModule) -> PyResult<()> { + let module = PyModule::new(py, "display")?; + + module.add_function(wrap_pyfunction!(bytes_to_bin, module)?)?; + module.add_function(wrap_pyfunction!(byte_bit_display, module)?)?; + module.add_function(wrap_pyfunction!(humanbytes, module)?)?; + + parent.add_submodule(module)?; + Ok(()) + } +} + +/// implement a python module in Rust +pub fn submodule(py: Python, parent: &PyModule) -> PyResult<()> { + let module = PyModule::new(py, "bintols")?; + + // binary constants + module.add("KIBI", origin::KIBI)?; + module.add("MEBI", origin::MEBI)?; + module.add("GIBI", origin::GIBI)?; + module.add("TEBI", origin::TEBI)?; + module.add("PEBI", origin::PEBI)?; + module.add("EXBI", origin::EXBI)?; + module.add("ZEBI", origin::ZEBI)?; + module.add("YOBI", origin::YOBI)?; + + display::submodule(py, module)?; + + parent.add_submodule(module)?; + Ok(()) +} diff --git a/members/libpt-py/src/lib.rs b/members/libpt-py/src/lib.rs index c595ba1..d66858e 100644 --- a/members/libpt-py/src/lib.rs +++ b/members/libpt-py/src/lib.rs @@ -4,6 +4,8 @@ mod core; #[cfg(feature = "log")] mod log; +#[cfg(feature = "bintols")] +mod bintols; use pyo3::prelude::*; @@ -20,6 +22,9 @@ fn libpt_py(py: Python, m: &PyModule) -> PyResult<()> { m.add_function(wrap_pyfunction!(version, m)?)?; #[cfg(feature = "core")] core::submodule(py, m)?; + #[cfg(feature = "log")] log::submodule(py, m)?; + #[cfg(feature = "bintols")] + bintols::submodule(py, m)?; Ok(()) }