Skip to main content

Compression

Enum Compression 

Source
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')); // NULL

Variants§

§

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

Source

pub const NONE: &'static str = "none"

No compression. PHP: Compression::NONE.

Source

pub const IDENTITY: &'static str = "identity"

No compression, the HTTP token. PHP: Compression::IDENTITY (deprecated there).

Source

pub const BROTLI: &'static str = "brotli"

PHP: Compression::BROTLI.

Source

pub const DEFLATE: &'static str = "deflate"

PHP: Compression::DEFLATE.

Source

pub const GZIP: &'static str = "gzip"

PHP: Compression::GZIP.

Source

pub const LZ4: &'static str = "lz4"

PHP: Compression::LZ4.

Source

pub const SNAPPY: &'static str = "snappy"

PHP: Compression::SNAPPY.

Source

pub const XZ: &'static str = "xz"

PHP: Compression::XZ.

Source

pub const ZSTD: &'static str = "zstd"

PHP: Compression::ZSTD.

Source

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).

Source

pub fn from_name_bytes(name: &[u8]) -> Option<Self>

Compression::from_name for a name in bytes, as PHP’s strings are.

Source

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')); // NULL

PHP: Compression::fromAcceptEncoding($acceptEncoding, $supported) with a list of names.

Source

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).

Source

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).

Source

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

Source§

fn name(&self) -> &str

The algorithm’s name, such as gzip. Read more
Source§

fn content_encoding(&self) -> Cow<'_, str>

The token of the algorithm in Content-Encoding and Accept-Encoding headers: by default the name in lower case (ASCII only); br for Brotli. Read more
Source§

fn compress(&self, data: &[u8]) -> Result<Vec<u8>, Error>

Compresses data. Read more
Source§

fn decompress(&self, data: &[u8]) -> Result<Vec<u8>, Error>

Decompresses data. Fails with Error::Codec when data is not a valid stream of the algorithm. Read more
Source§

fn supported(&self) -> bool

Whether this build can run the algorithm. Read more
Source§

impl Clone for Compression

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Compression

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Compression

Source§

impl PartialEq for Compression

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Compression

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.