Skip to content

Commit

Permalink
Add basic python binding (#29)
Browse files Browse the repository at this point in the history
* Add basic python binding

* Add CI for binding

* Change cd before using maturin
  • Loading branch information
adumbidiot authored Oct 17, 2022
1 parent 37cc0e5 commit 735e012
Show file tree
Hide file tree
Showing 7 changed files with 249 additions and 2 deletions.
15 changes: 14 additions & 1 deletion .github/workflows/Bindings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,17 @@ jobs:

- name: Run Tests
working-directory: bindings/lz-str-wasm/binding-tests
run: npm test
run: npm test

Python:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Install Maturin
run: pip install maturin

- name: Build Wheel
working-directory: bindings/lz-str-py
run: maturin build --release
177 changes: 177 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,10 @@ harness = false
[workspace]
members = [
"bindings/*",
]
]

[profile.release]
opt-level = 3
lto = "fat"
codegen-units = 1
strip = "symbols"
1 change: 1 addition & 0 deletions bindings/lz-str-py/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.venv
12 changes: 12 additions & 0 deletions bindings/lz-str-py/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "lz-str-py"
version = "0.0.0"
edition = "2021"

[lib]
name = "lz_str_py"
crate-type = [ "cdylib" ]

[dependencies]
lz-str = { path = "../.." }
pyo3 = { version = "0.17.2", features = [ "extension-module", "abi3", "abi3-py37" ] }
7 changes: 7 additions & 0 deletions bindings/lz-str-py/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[build-system]
requires = [ "maturin>=0.13,<0.14" ]
build-backend = "maturin"

[project]
name = "lz_str_py"
requires-python = ">=3.7"
31 changes: 31 additions & 0 deletions bindings/lz-str-py/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;
use pyo3::types::PyString;

#[pyfunction(name = "compressToBase64")]
pub fn compress_to_base64(input: &PyString) -> PyResult<String> {
let input = input.to_str()?;
Ok(lz_str::compress_to_base64(input))
}

#[pyfunction(name = "decompressFromBase64")]
pub fn decompress_from_base64(input: &PyString) -> PyResult<String> {
let input = input.to_str()?;
match lz_str::decompress_from_base64(input) {
Some(result) => {
// TODO: Make string from invalid unicode
match String::from_utf16(&result) {
Ok(value) => Ok(value),
Err(_e) => Err(PyValueError::new_err("invalid unicode output")),
}
}
None => Err(PyValueError::new_err("invalid input")),
}
}

#[pymodule]
fn lz_str_py(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(compress_to_base64, m)?)?;
m.add_function(wrap_pyfunction!(decompress_from_base64, m)?)?;
Ok(())
}

0 comments on commit 735e012

Please sign in to comment.