-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add basic python binding * Add CI for binding * Change cd before using maturin
- Loading branch information
1 parent
37cc0e5
commit 735e012
Showing
7 changed files
with
249 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.venv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" ] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |