UrlPattern

Struct UrlPattern 

Source
pub struct UrlPattern<R: RegExp = Regex> { /* private fields */ }
Expand description

A UrlPattern that can be matched against.

§Examples

use urlpattern::UrlPattern;
use urlpattern::UrlPatternInit;
use urlpattern::UrlPatternMatchInput;

// Create the UrlPattern to match against.
let init = UrlPatternInit {
  pathname: Some("/users/:id".to_owned()),
  ..Default::default()
};
let pattern = <UrlPattern>::parse(init, Default::default()).unwrap();

// Match the pattern against a URL.
let url = "https://example.com/users/123".parse().unwrap();
let result = pattern.exec(UrlPatternMatchInput::Url(url)).unwrap().unwrap();
assert_eq!(result.pathname.groups.get("id").unwrap().as_ref().unwrap(), "123");

Implementations§

Source§

impl<R: RegExp> UrlPattern<R>

Source

pub fn parse( init: UrlPatternInit, options: UrlPatternOptions, ) -> Result<Self, Error>

Parse a UrlPatternInit into a UrlPattern.

Source

pub fn protocol(&self) -> &str

The pattern used to match against the protocol of the URL.

Source

pub fn username(&self) -> &str

The pattern used to match against the username of the URL.

Source

pub fn password(&self) -> &str

The pattern used to match against the password of the URL.

Source

pub fn hostname(&self) -> &str

The pattern used to match against the hostname of the URL.

Source

pub fn port(&self) -> &str

The pattern used to match against the port of the URL.

Source

pub fn pathname(&self) -> &str

The pattern used to match against the pathname of the URL.

Source

pub fn search(&self) -> &str

The pattern used to match against the search string of the URL.

Source

pub fn hash(&self) -> &str

The pattern used to match against the hash fragment of the URL.

Source

pub fn has_regexp_groups(&self) -> bool

Returns whether the URLPattern contains one or more groups which uses regular expression matching.

Source

pub fn test(&self, input: UrlPatternMatchInput) -> Result<bool, Error>

Test if a given [UrlPatternInput] (with optional base url), matches the pattern.

Source

pub fn exec( &self, input: UrlPatternMatchInput, ) -> Result<Option<UrlPatternResult>, Error>

Execute the pattern against a [UrlPatternInput] (with optional base url), returning a UrlPatternResult if the pattern matches. If the pattern doesn’t match, returns None.

Trait Implementations§

Source§

impl<R: Debug + RegExp> Debug for UrlPattern<R>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<R> Freeze for UrlPattern<R>
where R: Freeze,

§

impl<R> RefUnwindSafe for UrlPattern<R>
where R: RefUnwindSafe,

§

impl<R> Send for UrlPattern<R>
where R: Send,

§

impl<R> Sync for UrlPattern<R>
where R: Sync,

§

impl<R> Unpin for UrlPattern<R>
where R: Unpin,

§

impl<R> UnwindSafe for UrlPattern<R>
where R: UnwindSafe,

Blanket Implementations§

§

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

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

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

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

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

§

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

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

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

§

fn into(self) -> U

Calls U::from(self).

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

§

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

§

type Error = Infallible

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

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

Performs the conversion.
§

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

§

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

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

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

Performs the conversion.