Skip to main content

Negotiator

Struct Negotiator 

Source
pub struct Negotiator { /* private fields */ }
Expand description

The configurable form of the best_* functions.

§Examples

use parse_net::negotiate::{LanguageMatching, MalformedPolicy, Negotiator, Outcome};

let n = Negotiator::new().language_matching(LanguageMatching::BasicFilter);
assert_eq!(n.language(Some(b"en"), &["fr", "en-GB"]).index(), Some(1));
let strict = Negotiator::new().on_malformed(MalformedPolicy::Refuse);
assert!(matches!(strict.media(Some(b"text/html;q=2"), &["text/html"]), Outcome::Refused(_)));

Implementations§

Source§

impl Negotiator

Source

pub const fn new() -> Self

Default limits, RFC 4647 lookup, malformed headers treated as absent.

Source

pub const fn limits(self, l: Limits) -> Self

Sets the limits the request headers are parsed with.

Source

pub const fn language_matching(self, m: LanguageMatching) -> Self

Sets how language ranges are matched.

Source

pub const fn on_malformed(self, p: MalformedPolicy) -> Self

Sets what happens with a header that does not parse.

Source

pub fn media(&self, accept: Option<&[u8]>, offers: &[&str]) -> Outcome

Selects the best of offers (media types such as "text/html") for Accept.

An offer’s weight is the q of the most specific matching range (RFC 9110 section 12.5.1); q=0 means not acceptable; ties go to the earlier offer. An absent header makes every offer acceptable, so the first wins. An offer that is not a valid media type is never chosen.

Source

pub fn media_lines<'a, I>(&self, lines: I, offers: &[&str]) -> Outcome
where I: IntoIterator<Item = &'a [u8]>, I::IntoIter: Clone,

As media, for several Accept field lines read as one list.

Source

pub fn language(&self, value: Option<&[u8]>, offers: &[&str]) -> Outcome

Selects the best of offers (language tags such as "fr-CA") for Accept-Language.

With LanguageMatching::Lookup (the default), a range matches an offer equal to it or reached by removing subtags from the range’s end, and the exact match has precedence over a shortened one; with LanguageMatching::BasicFilter, a range matches an offer equal to it or starting with it and -, the longest range having precedence. * matches every offer at the lowest precedence. The highest weight wins; between equal weights the closer match (as lookup returns the longest truncation), then the earlier offer. Tags compare ASCII case-insensitively.

Source

pub fn encoding(&self, value: Option<&[u8]>, offers: &[&str]) -> Outcome

Selects the best of offers (content codings such as "br") for Accept-Encoding (RFC 9110 section 12.5.3).

identity is acceptable unless identity;q=0, or *;q=0 without an explicit identity; an empty field value means only identity is acceptable; x-gzip equals gzip and x-compress equals compress.

Source

pub fn charset(&self, value: Option<&[u8]>, offers: &[&str]) -> Outcome

Selects the best of offers (charsets such as "utf-8") for Accept-Charset: as encoding, without the identity rule.

Trait Implementations§

Source§

impl Clone for Negotiator

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 Copy for Negotiator

Source§

impl Debug for Negotiator

Source§

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

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

impl Default for Negotiator

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Eq for Negotiator

Source§

impl Hash for Negotiator

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Negotiator

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 Negotiator

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