diff --git a/.github/workflows/Bindings.yml b/.github/workflows/Bindings.yml index 34916e9..c0263d6 100644 --- a/.github/workflows/Bindings.yml +++ b/.github/workflows/Bindings.yml @@ -37,4 +37,17 @@ jobs: - name: Run Tests working-directory: bindings/lz-str-wasm/binding-tests - run: npm test \ No newline at end of file + 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 \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index fa78a85..9880257 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -224,6 +224,12 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "indoc" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adab1eaa3408fb7f0c777a73e7465fd5656136fc93b670eb6df3c88c2c1344e3" + [[package]] name = "itertools" version = "0.10.5" @@ -260,6 +266,16 @@ version = "0.2.135" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "68783febc7782c6c5cb401fbda4de5a9898be1762314da0bb2c10ced61f18b0c" +[[package]] +name = "lock_api" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "435011366fe56583b16cf956f9df0095b405b82d76425bc8981c0e22e60ec4df" +dependencies = [ + "autocfg", + "scopeguard", +] + [[package]] name = "log" version = "0.4.17" @@ -277,6 +293,14 @@ dependencies = [ "rand", ] +[[package]] +name = "lz-str-py" +version = "0.0.0" +dependencies = [ + "lz-str", + "pyo3", +] + [[package]] name = "lz-str-wasm" version = "0.0.0" @@ -332,6 +356,29 @@ version = "6.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9ff7415e9ae3fff1225851df9e0d9e4e5479f947619774677a63572e55e80eff" +[[package]] +name = "parking_lot" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09a279cbf25cb0757810394fbc1e359949b59e348145c643a939a525692e6929" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-sys", +] + [[package]] name = "plotters" version = "0.3.4" @@ -375,6 +422,66 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "pyo3" +version = "0.17.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "201b6887e5576bf2f945fe65172c1fcbf3fcf285b23e4d71eb171d9736e38d32" +dependencies = [ + "cfg-if", + "indoc", + "libc", + "memoffset", + "parking_lot", + "pyo3-build-config", + "pyo3-ffi", + "pyo3-macros", + "unindent", +] + +[[package]] +name = "pyo3-build-config" +version = "0.17.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf0708c9ed01692635cbf056e286008e5a2927ab1a5e48cdd3aeb1ba5a6fef47" +dependencies = [ + "once_cell", + "target-lexicon", +] + +[[package]] +name = "pyo3-ffi" +version = "0.17.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90352dea4f486932b72ddf776264d293f85b79a1d214de1d023927b41461132d" +dependencies = [ + "libc", + "pyo3-build-config", +] + +[[package]] +name = "pyo3-macros" +version = "0.17.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7eb24b804a2d9e88bfcc480a5a6dd76f006c1e3edaf064e8250423336e2cd79d" +dependencies = [ + "proc-macro2", + "pyo3-macros-backend", + "quote", + "syn", +] + +[[package]] +name = "pyo3-macros-backend" +version = "0.17.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f22bb49f6a7348c253d7ac67a6875f2dc65f36c2ae64a82c381d528972bea6d6" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "quote" version = "1.0.21" @@ -438,6 +545,15 @@ dependencies = [ "num_cpus", ] +[[package]] +name = "redox_syscall" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a" +dependencies = [ + "bitflags", +] + [[package]] name = "regex" version = "1.6.0" @@ -505,6 +621,12 @@ dependencies = [ "serde", ] +[[package]] +name = "smallvec" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0" + [[package]] name = "syn" version = "1.0.102" @@ -516,6 +638,12 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "target-lexicon" +version = "0.12.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c02424087780c9b71cc96799eaeddff35af2bc513278cda5c99fc1f5d026d3c1" + [[package]] name = "textwrap" version = "0.15.1" @@ -538,6 +666,12 @@ version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6ceab39d59e4c9499d4e5a8ee0e2735b891bb7308ac83dfb4e80cad195c9f6f3" +[[package]] +name = "unindent" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "58ee9362deb4a96cef4d437d1ad49cffc9b9e92d202b6995674e928ce684f112" + [[package]] name = "walkdir" version = "2.3.2" @@ -649,3 +783,46 @@ name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows-sys" +version = "0.36.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea04155a16a59f9eab786fe12a4a450e75cdb175f9e0d80da1e17db09f55b8d2" +dependencies = [ + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_msvc" +version = "0.36.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9bb8c3fd39ade2d67e9874ac4f3db21f0d710bee00fe7cab16949ec184eeaa47" + +[[package]] +name = "windows_i686_gnu" +version = "0.36.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "180e6ccf01daf4c426b846dfc66db1fc518f074baa793aa7d9b9aaeffad6a3b6" + +[[package]] +name = "windows_i686_msvc" +version = "0.36.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2e7917148b2812d1eeafaeb22a97e4813dfa60a3f8f78ebe204bcc88f12f024" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.36.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4dcd171b8776c41b97521e5da127a2d86ad280114807d0b2ab1e462bc764d9e1" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.36.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c811ca4a8c853ef420abd8592ba53ddbbac90410fab6903b3e79972a631f7680" diff --git a/Cargo.toml b/Cargo.toml index 85d16a8..a5aa0d5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,4 +38,10 @@ harness = false [workspace] members = [ "bindings/*", -] \ No newline at end of file +] + +[profile.release] +opt-level = 3 +lto = "fat" +codegen-units = 1 +strip = "symbols" \ No newline at end of file diff --git a/bindings/lz-str-py/.gitignore b/bindings/lz-str-py/.gitignore new file mode 100644 index 0000000..b694934 --- /dev/null +++ b/bindings/lz-str-py/.gitignore @@ -0,0 +1 @@ +.venv \ No newline at end of file diff --git a/bindings/lz-str-py/Cargo.toml b/bindings/lz-str-py/Cargo.toml new file mode 100644 index 0000000..7c06139 --- /dev/null +++ b/bindings/lz-str-py/Cargo.toml @@ -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" ] } diff --git a/bindings/lz-str-py/pyproject.toml b/bindings/lz-str-py/pyproject.toml new file mode 100644 index 0000000..1331695 --- /dev/null +++ b/bindings/lz-str-py/pyproject.toml @@ -0,0 +1,7 @@ +[build-system] +requires = [ "maturin>=0.13,<0.14" ] +build-backend = "maturin" + +[project] +name = "lz_str_py" +requires-python = ">=3.7" \ No newline at end of file diff --git a/bindings/lz-str-py/src/lib.rs b/bindings/lz-str-py/src/lib.rs new file mode 100644 index 0000000..d8cb1fb --- /dev/null +++ b/bindings/lz-str-py/src/lib.rs @@ -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 { + let input = input.to_str()?; + Ok(lz_str::compress_to_base64(input)) +} + +#[pyfunction(name = "decompressFromBase64")] +pub fn decompress_from_base64(input: &PyString) -> PyResult { + 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(()) +}