#![warn(missing_docs)]
mod brotli;
mod error;
mod lz4;
mod negotiate;
mod sink;
mod snappy;
mod xz;
mod zlib;
mod zstd;
use std::borrow::Cow;
pub use brotli::{Brotli, Mode};
pub use error::Error;
pub use lz4::Lz4;
pub use sink::{Allocator, Sink};
pub use snappy::Snappy;
pub use xz::Xz;
pub use zlib::{Deflate, Gzip};
pub use zstd::Zstd;
#[cfg(doctest)]
#[doc = include_str!("../guide.md")]
pub struct GettingStarted;
pub trait Algorithm {
fn name(&self) -> &str;
fn content_encoding(&self) -> Cow<'_, str> {
let name = self.name();
match php_std::string::strtolower(name.as_bytes()) {
Cow::Borrowed(_) => Cow::Borrowed(name),
Cow::Owned(lower) => Cow::Owned(String::from_utf8_lossy(&lower).into_owned()),
}
}
fn compress(&self, data: &[u8]) -> Result<Vec<u8>, Error>;
fn decompress(&self, data: &[u8]) -> Result<Vec<u8>, Error>;
fn supported(&self) -> bool;
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Compression {
Brotli(Brotli),
Deflate(Deflate),
Gzip(Gzip),
Lz4(Lz4),
Snappy(Snappy),
Xz(Xz),
Zstd(Zstd),
}
impl Compression {
pub const NONE: &'static str = "none";
pub const IDENTITY: &'static str = "identity";
pub const BROTLI: &'static str = "brotli";
pub const DEFLATE: &'static str = "deflate";
pub const GZIP: &'static str = "gzip";
pub const LZ4: &'static str = "lz4";
pub const SNAPPY: &'static str = "snappy";
pub const XZ: &'static str = "xz";
pub const ZSTD: &'static str = "zstd";
pub fn from_name(name: &str) -> Option<Self> {
Self::from_name_bytes(name.as_bytes())
}
pub fn from_name_bytes(name: &[u8]) -> Option<Self> {
Some(match php_std::string::strtolower(name).as_ref() {
b"brotli" | b"br" => Compression::Brotli(Brotli::new()),
b"deflate" => Compression::Deflate(Deflate::new()),
b"gzip" => Compression::Gzip(Gzip::new()),
b"lz4" => Compression::Lz4(Lz4::new()),
b"snappy" => Compression::Snappy(Snappy::new()),
b"xz" => Compression::Xz(Xz::new()),
b"zstd" => Compression::Zstd(Zstd::new()),
_ => return None,
})
}
pub fn from_accept_encoding<S: AsRef<str>>(accept: &str, supported: &[S]) -> Option<Self> {
if supported.is_empty() {
Self::from_accept_encoding_with(accept, Self::is_default_supported)
} else {
Self::from_accept_encoding_with(accept, |name| supported.iter().any(|s| s.as_ref() == name))
}
}
pub fn from_accept_encoding_with(accept: &str, supported: impl Fn(&str) -> bool) -> Option<Self> {
negotiate::choose(accept.as_bytes(), |name| std::str::from_utf8(name).is_ok_and(&supported))
}
pub fn from_accept_encoding_bytes(accept: &[u8], supported: impl Fn(&[u8]) -> bool) -> Option<Self> {
negotiate::choose(accept, supported)
}
pub fn is_default_supported(name: &str) -> bool {
match name {
Self::ZSTD => Zstd::is_supported(),
Self::BROTLI => Brotli::is_supported(),
Self::GZIP => Gzip::is_supported(),
Self::DEFLATE => Deflate::is_supported(),
Self::NONE | Self::IDENTITY => true,
_ => false,
}
}
fn algorithm(&self) -> &dyn Algorithm {
match self {
Compression::Brotli(a) => a,
Compression::Deflate(a) => a,
Compression::Gzip(a) => a,
Compression::Lz4(a) => a,
Compression::Snappy(a) => a,
Compression::Xz(a) => a,
Compression::Zstd(a) => a,
}
}
}
impl Algorithm for Compression {
fn name(&self) -> &str {
self.algorithm().name()
}
fn content_encoding(&self) -> Cow<'_, str> {
self.algorithm().content_encoding()
}
fn compress(&self, data: &[u8]) -> Result<Vec<u8>, Error> {
match self {
Compression::Brotli(a) => a.compress(data),
Compression::Deflate(a) => a.compress(data),
Compression::Gzip(a) => a.compress(data),
Compression::Lz4(a) => a.compress(data),
Compression::Snappy(a) => a.compress(data),
Compression::Xz(a) => a.compress(data),
Compression::Zstd(a) => a.compress(data),
}
}
fn decompress(&self, data: &[u8]) -> Result<Vec<u8>, Error> {
match self {
Compression::Brotli(a) => a.decompress(data),
Compression::Deflate(a) => a.decompress(data),
Compression::Gzip(a) => a.decompress(data),
Compression::Lz4(a) => a.decompress(data),
Compression::Snappy(a) => a.decompress(data),
Compression::Xz(a) => a.decompress(data),
Compression::Zstd(a) => a.decompress(data),
}
}
fn supported(&self) -> bool {
self.algorithm().supported()
}
}