pub enum Compression {
Brotli(Brotli),
Deflate(Deflate),
Gzip(Gzip),
Lz4(Lz4),
Snappy(Snappy),
Xz(Xz),
Zstd(Zstd),
}Expand description
A built-in algorithm, as chosen by name or from an Accept-Encoding
header.
use utopia_compression::{Algorithm, Compression, Zstd};
assert_eq!(Compression::from_name("ZSTD"), Some(Compression::Zstd(Zstd::new())));
assert_eq!(Compression::from_name("br").unwrap().name(), "brotli");
assert_eq!(Compression::from_name("identity"), None);use Utopia\Compression\Compression;
var_dump(Compression::fromName('ZSTD') instanceof Utopia\Compression\Algorithms\Zstd); // bool(true)
echo Compression::fromName('br')->getName(), "\n"; // brotli
var_dump(Compression::fromName('identity')); // NULLVariants§
Brotli(Brotli)
Brotli (br).
Deflate(Deflate)
Raw deflate (deflate).
Gzip(Gzip)
Gzip (gzip).
Lz4(Lz4)
LZ4 (lz4).
Snappy(Snappy)
Snappy (snappy).
Xz(Xz)
XZ (xz).
Zstd(Zstd)
Zstandard (zstd).
Implementations§
Source§impl Compression
impl Compression
Sourcepub const IDENTITY: &'static str = "identity"
pub const IDENTITY: &'static str = "identity"
No compression, the HTTP token. PHP: Compression::IDENTITY (deprecated there).
Sourcepub fn from_name(name: &str) -> Option<Self>
pub fn from_name(name: &str) -> Option<Self>
The algorithm named name (ASCII case-insensitive; br is Brotli),
at its default level. None for none, identity and unknown names.
PHP: Compression::fromName($name).
Sourcepub fn from_name_bytes(name: &[u8]) -> Option<Self>
pub fn from_name_bytes(name: &[u8]) -> Option<Self>
Compression::from_name for a name in bytes, as PHP’s strings are.
Sourcepub fn from_accept_encoding<S: AsRef<str>>(
accept: &str,
supported: &[S],
) -> Option<Self>
pub fn from_accept_encoding<S: AsRef<str>>( accept: &str, supported: &[S], ) -> Option<Self>
The algorithm to answer an Accept-Encoding header with: of the
encodings the header lists that are in supported, the one with the
highest q (the first listed on a tie), or None when there is
none, or when it is none or identity (no compression).
An empty supported means Compression::is_default_supported:
zstd, brotli, gzip and deflate when this build supports them,
plus none and identity. Names in supported are canonical (brotli,
not br).
use utopia_compression::{Algorithm, Compression};
let chosen = Compression::from_accept_encoding("gzip;q=0.5, br;q=0.5", &["gzip", "brotli"]);
assert_eq!(chosen.unwrap().name(), "gzip");
let chosen = Compression::from_accept_encoding("snappy;q=0.6, br;q=0.4", &["snappy"]);
assert_eq!(chosen.unwrap().name(), "snappy");
assert_eq!(Compression::from_accept_encoding("identity, gzip", &[] as &[&str]), None);use Utopia\Compression\Compression;
echo Compression::fromAcceptEncoding('gzip;q=0.5, br;q=0.5', ['gzip', 'brotli'])->getName(), "\n"; // gzip
echo Compression::fromAcceptEncoding('snappy;q=0.6, br;q=0.4', ['snappy'])->getName(), "\n"; // snappy
var_dump(Compression::fromAcceptEncoding('identity, gzip')); // NULLPHP: Compression::fromAcceptEncoding($acceptEncoding, $supported) with
a list of names.
Sourcepub fn from_accept_encoding_with(
accept: &str,
supported: impl Fn(&str) -> bool,
) -> Option<Self>
pub fn from_accept_encoding_with( accept: &str, supported: impl Fn(&str) -> bool, ) -> Option<Self>
Compression::from_accept_encoding with the supported encodings
given by a predicate on their canonical name.
PHP: Compression::fromAcceptEncoding($acceptEncoding, $supported)
with a name => bool map (fn(name) => $supported[$name] ?? false).
Sourcepub fn from_accept_encoding_bytes(
accept: &[u8],
supported: impl Fn(&[u8]) -> bool,
) -> Option<Self>
pub fn from_accept_encoding_bytes( accept: &[u8], supported: impl Fn(&[u8]) -> bool, ) -> Option<Self>
Compression::from_accept_encoding_with for a header and names in bytes, as PHP’s
strings are: a name that is not UTF-8 can still be supported, and chosen (it names no
algorithm, so the answer is then None).
Sourcepub fn is_default_supported(name: &str) -> bool
pub fn is_default_supported(name: &str) -> bool
Whether name is offered when no supported list is given: zstd,
brotli, gzip and deflate when this build supports them, and
none and identity.
PHP: the default $supported of fromAcceptEncoding().
Trait Implementations§
Source§impl Algorithm for Compression
impl Algorithm for Compression
Source§fn content_encoding(&self) -> Cow<'_, str>
fn content_encoding(&self) -> Cow<'_, str>
Content-Encoding and
Accept-Encoding headers: by default the name in lower case (ASCII
only); br for Brotli. Read moreSource§fn decompress(&self, data: &[u8]) -> Result<Vec<u8>, Error>
fn decompress(&self, data: &[u8]) -> Result<Vec<u8>, Error>
data. Fails with Error::Codec when data is not a
valid stream of the algorithm. Read moreSource§impl Clone for Compression
impl Clone for Compression
Source§impl Debug for Compression
impl Debug for Compression
impl Eq for Compression
Source§impl PartialEq for Compression
impl PartialEq for Compression
impl StructuralPartialEq for Compression
Auto Trait Implementations§
impl Forget for Compression
impl Freeze for Compression
impl Move for Compression
impl RefUnwindSafe for Compression
impl Send for Compression
impl Sync for Compression
impl Unpin for Compression
impl UnsafeUnpin for Compression
impl UnwindSafe for Compression
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.