ContentEncoding

Struct ContentEncoding 

Source
pub struct ContentEncoding(/* private fields */);
Expand description

A wrapper type for content encoding that represents the compression or encoding scheme used in an HTTP message body. This is used in HTTP’s Content-Encoding header.

Implementations§

Source§

impl ContentEncoding

Source

pub fn new(encoding: Encoding) -> Self

Create a new ContentEncoding with the specified encoding

Examples found in repository?
examples/encode_decode.rs (line 132)
117fn content_encoding_examples() {
118    println!("\n\n2. Content-Encoding Examples");
119    println!("============================");
120
121    // Example 1: Creating Content-Encoding
122    println!("\n2a. Creating Content-Encoding headers:");
123    let encodings_to_test = vec![
124        Encoding::Gzip,
125        Encoding::Deflate,
126        Encoding::Br,
127        Encoding::Zstd,
128        Encoding::Custom("lz4".to_string()),
129    ];
130
131    for encoding in encodings_to_test {
132        let content_encoding = ContentEncoding::new(encoding);
133        println!("   Created: {:?}", content_encoding);
134
135        // Encode to header value
136        let mut values = Vec::new();
137        content_encoding.encode(&mut values);
138
139        if let Some(header_value) = values.first() {
140            if let Ok(as_str) = header_value.to_str() {
141                println!("     Header value: {}", as_str);
142            }
143        }
144    }
145
146    // Example 2: Decoding Content-Encoding headers
147    println!("\n2b. Decoding Content-Encoding headers:");
148    let test_headers = vec!["gzip", "deflate", "br", "zstd", "custom-encoding"];
149
150    for header_str in test_headers {
151        println!("   Decoding: \"{}\"", header_str);
152
153        let header_values = vec![HeaderValue::from_str(header_str).unwrap()];
154        match ContentEncoding::decode(&mut header_values.iter()) {
155            Ok(decoded) => {
156                println!("     Success: {:?}", decoded);
157                println!("     Encoding: {:?}", decoded.encoding());
158            }
159            Err(e) => println!("     Error: {:?}", e),
160        }
161    }
162}
More examples
Hide additional examples
examples/basic_usage.rs (line 96)
90fn content_encoding_examples() {
91    println!("\n\n2. Content-Encoding Header Examples");
92    println!("===================================");
93
94    // Example 2a: Creating and encoding Content-Encoding headers
95    println!("\n2a. Creating and encoding Content-Encoding:");
96    let content_encoding = ContentEncoding::new(Encoding::Gzip);
97    println!("   Created ContentEncoding: {:?}", content_encoding);
98
99    // Use with HeaderMap
100    let mut headers = HeaderMap::new();
101    headers.typed_insert(content_encoding);
102
103    if let Some(header_value) = headers.get(http::header::CONTENT_ENCODING) {
104        println!(
105            "   Header value: {}",
106            header_value.to_str().unwrap_or("invalid")
107        );
108    }
109
110    // Example 2b: Decoding Content-Encoding headers
111    println!("\n2b. Decoding Content-Encoding headers:");
112    let test_values = vec!["gzip", "deflate", "br", "zstd"];
113
114    for encoding_str in test_values {
115        println!("   Testing: {}", encoding_str);
116        let header_values = vec![HeaderValue::from_str(encoding_str).unwrap()];
117
118        match ContentEncoding::decode(&mut header_values.iter()) {
119            Ok(decoded) => println!("     Decoded: {:?}", decoded),
120            Err(e) => println!("     Error: {:?}", e),
121        }
122    }
123
124    // Example 2c: Multiple identical values (valid)
125    println!("\n2c. Multiple identical Content-Encoding values:");
126    let identical_values = vec![
127        HeaderValue::from_str("gzip").unwrap(),
128        HeaderValue::from_str("gzip").unwrap(),
129    ];
130
131    match ContentEncoding::decode(&mut identical_values.iter()) {
132        Ok(decoded) => println!("   Multiple identical values decoded: {:?}", decoded),
133        Err(e) => println!("   Error: {:?}", e),
134    }
135
136    // Example 2d: Conflicting values (should error)
137    println!("\n2d. Conflicting Content-Encoding values (should error):");
138    let conflicting_values = vec![
139        HeaderValue::from_str("gzip").unwrap(),
140        HeaderValue::from_str("deflate").unwrap(),
141    ];
142
143    match ContentEncoding::decode(&mut conflicting_values.iter()) {
144        Ok(decoded) => println!("   Unexpectedly decoded: {:?}", decoded),
145        Err(_) => println!("   Correctly rejected conflicting values"),
146    }
147}
Source

pub fn encoding(&self) -> &Encoding

Get the encoding value

Examples found in repository?
examples/encode_decode.rs (line 157)
117fn content_encoding_examples() {
118    println!("\n\n2. Content-Encoding Examples");
119    println!("============================");
120
121    // Example 1: Creating Content-Encoding
122    println!("\n2a. Creating Content-Encoding headers:");
123    let encodings_to_test = vec![
124        Encoding::Gzip,
125        Encoding::Deflate,
126        Encoding::Br,
127        Encoding::Zstd,
128        Encoding::Custom("lz4".to_string()),
129    ];
130
131    for encoding in encodings_to_test {
132        let content_encoding = ContentEncoding::new(encoding);
133        println!("   Created: {:?}", content_encoding);
134
135        // Encode to header value
136        let mut values = Vec::new();
137        content_encoding.encode(&mut values);
138
139        if let Some(header_value) = values.first() {
140            if let Ok(as_str) = header_value.to_str() {
141                println!("     Header value: {}", as_str);
142            }
143        }
144    }
145
146    // Example 2: Decoding Content-Encoding headers
147    println!("\n2b. Decoding Content-Encoding headers:");
148    let test_headers = vec!["gzip", "deflate", "br", "zstd", "custom-encoding"];
149
150    for header_str in test_headers {
151        println!("   Decoding: \"{}\"", header_str);
152
153        let header_values = vec![HeaderValue::from_str(header_str).unwrap()];
154        match ContentEncoding::decode(&mut header_values.iter()) {
155            Ok(decoded) => {
156                println!("     Success: {:?}", decoded);
157                println!("     Encoding: {:?}", decoded.encoding());
158            }
159            Err(e) => println!("     Error: {:?}", e),
160        }
161    }
162}

Trait Implementations§

Source§

impl Clone for ContentEncoding

Source§

fn clone(&self) -> ContentEncoding

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for ContentEncoding

Source§

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

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

impl Header for ContentEncoding

Available on crate feature http_crates only.
Source§

fn name() -> &'static HeaderName

The name of this header.
Source§

fn decode<'i, I>(values: &mut I) -> Result<Self, Error>
where Self: Sized, I: Iterator<Item = &'i HeaderValue>,

Decode this type from an iterator of HeaderValues.
Source§

fn encode<E: Extend<HeaderValue>>(&self, values: &mut E)

Encode this type to a HeaderMap. Read more
Source§

impl Ord for ContentEncoding

Source§

fn cmp(&self, other: &ContentEncoding) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for ContentEncoding

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for ContentEncoding

Source§

fn partial_cmp(&self, other: &ContentEncoding) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Eq for ContentEncoding

Source§

impl StructuralPartialEq for ContentEncoding

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<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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.