Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic python binding #29

Merged
merged 3 commits into from
Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(())
}