Create Python version; fix Rust code sample on README
This commit is contained in:
parent
6b2b8cf552
commit
7940fae01b
8 changed files with 135 additions and 7 deletions
2
python/.gitignore
vendored
Normal file
2
python/.gitignore
vendored
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
/Cargo.lock
|
||||
/target
|
||||
23
python/Cargo.toml
Normal file
23
python/Cargo.toml
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
[package]
|
||||
name = "hyperbuild-python"
|
||||
publish = false
|
||||
version = "0.0.19"
|
||||
authors = ["Wilson Lin <code@wilsonl.in>"]
|
||||
edition = "2018"
|
||||
|
||||
[lib]
|
||||
name = "hyperbuild_python_lib"
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
hyperbuild = "0.0.19"
|
||||
|
||||
[dependencies.pyo3]
|
||||
version = "0.9.0-alpha.1"
|
||||
features = ["extension-module"]
|
||||
|
||||
[target.x86_64-apple-darwin]
|
||||
rustflags = [
|
||||
"-C", "link-arg=-undefined",
|
||||
"-C", "link-arg=dynamic_lookup",
|
||||
]
|
||||
21
python/src/lib.rs
Normal file
21
python/src/lib.rs
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
use hyperbuild::hyperbuild as hyperbuild_native;
|
||||
use pyo3::prelude::*;
|
||||
use pyo3::exceptions::SyntaxError;
|
||||
use pyo3::wrap_pyfunction;
|
||||
use std::str::from_utf8_unchecked;
|
||||
|
||||
#[pyfunction]
|
||||
fn minify(code: String) -> PyResult<String> {
|
||||
let mut code = code.into_bytes();
|
||||
match hyperbuild_native(&mut code) {
|
||||
Ok(out_len) => Ok(unsafe { from_utf8_unchecked(&code[0..out_len]).to_string() }),
|
||||
Err((err, pos)) => Err(SyntaxError::py_err(format!("{} [Character {}]", err.message(), pos))),
|
||||
}
|
||||
}
|
||||
|
||||
#[pymodule]
|
||||
fn hyperbuild(py: Python, m: &PyModule) -> PyResult<()> {
|
||||
m.add_wrapped(wrap_pyfunction!(minify))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in a new issue