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>
impl<R: RegExp> UrlPattern<R>
Sourcepub fn parse(
init: UrlPatternInit,
options: UrlPatternOptions,
) -> Result<Self, Error>
pub fn parse( init: UrlPatternInit, options: UrlPatternOptions, ) -> Result<Self, Error>
Parse a UrlPatternInit into a UrlPattern.
Sourcepub fn has_regexp_groups(&self) -> bool
pub fn has_regexp_groups(&self) -> bool
Returns whether the URLPattern contains one or more groups which uses regular expression matching.
Sourcepub fn test(&self, input: UrlPatternMatchInput) -> Result<bool, Error>
pub fn test(&self, input: UrlPatternMatchInput) -> Result<bool, Error>
Test if a given [UrlPatternInput] (with optional base url), matches the pattern.
Sourcepub fn exec(
&self,
input: UrlPatternMatchInput,
) -> Result<Option<UrlPatternResult>, Error>
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.