-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2b509df
commit 617606f
Showing
8 changed files
with
89 additions
and
51 deletions.
There are no files selected for viewing
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,7 @@ | ||
# `ryo3-flate2` | ||
|
||
Wrapper around the `flate2` crate for Python. | ||
|
||
REF: | ||
- [crates.io](https://crates.io/crates/flate2) | ||
- [docs.rs](https://docs.rs/flate2) |
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,53 @@ | ||
use flate2::read::GzDecoder; | ||
use flate2::write::GzEncoder; | ||
use flate2::Compression; | ||
use pyo3::prelude::*; | ||
use pyo3::types::PyBytes; | ||
use ryo3_macros::py_value_error; | ||
use std::io::{Read, Write}; | ||
|
||
#[pyfunction] | ||
#[pyo3(signature = (data, quality=None))] | ||
pub fn gzip_encode(py: Python<'_>, data: &[u8], quality: Option<u32>) -> PyResult<PyObject> { | ||
let quality = if let Some(param) = quality { | ||
if param > 9 { | ||
return Err(py_value_error!( | ||
"Quality must be between 0 and 9 - got: {param:?}" | ||
)); | ||
} | ||
Compression::new(param) | ||
} else { | ||
Compression::default() | ||
}; | ||
let mut gzip_encoder = GzEncoder::new(Vec::new(), quality); | ||
gzip_encoder.write_all(data).map_err(|e| { | ||
PyErr::new::<pyo3::exceptions::PyValueError, _>(format!("gzip-encode-error: {e:?}")) | ||
})?; | ||
let encoded = gzip_encoder.finish().map_err(|e| { | ||
PyErr::new::<pyo3::exceptions::PyValueError, _>(format!("gzip-encode-error: {e:?}")) | ||
})?; | ||
Ok(PyBytes::new(py, &encoded).into()) | ||
} | ||
|
||
#[pyfunction] | ||
pub fn gzip_decode(py: Python<'_>, data: &[u8]) -> PyResult<PyObject> { | ||
let mut decompressed = Vec::new(); | ||
GzDecoder::new(data) | ||
.read_to_end(&mut decompressed) | ||
.map_err(|e| { | ||
PyErr::new::<pyo3::exceptions::PyValueError, _>(format!("gzip-decode-error: {e:?}")) | ||
})?; | ||
Ok(PyBytes::new(py, &decompressed).into()) | ||
} | ||
|
||
// aliases... | ||
#[pyfunction] | ||
#[pyo3(signature = (data, quality=None))] | ||
pub fn gzip(py: Python<'_>, data: &[u8], quality: Option<u32>) -> PyResult<PyObject> { | ||
gzip_encode(py, data, quality) | ||
} | ||
|
||
#[pyfunction] | ||
pub fn gunzip(py: Python<'_>, data: &[u8]) -> PyResult<PyObject> { | ||
gzip_decode(py, data) | ||
} |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
mod not_implemented; | ||
mod py_errs; |
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 @@ | ||
/// Return a `PyValueError` with formatted string | ||
#[macro_export] | ||
macro_rules! py_value_error { | ||
($($arg:tt)*) => { | ||
::pyo3::PyErr::new::<pyo3::exceptions::PyValueError, _>( format!($($arg)*) ) | ||
}; | ||
} |
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