Trait UriMatchReplace
pub trait UriMatchReplace {
// Required method
fn match_replace_uri<'a>(
&self,
uri: Cow<'a, Uri>,
) -> Result<Cow<'a, Uri>, UriMatchError<'a>>;
}Expand description
A trait for types that can match and optionally replace (rewrite)
Uri values.
§Blanket implementations
This trait is implemented for common iterable types:
[R; N]for any array ofUriMatchReplaces of usizeN;&[R]for any slice ofUriMatchReplaces;Vec<R>for anyVec] ofUriMatchReplaces;
It is also implemeneted for tuples size 1 to 12,
allowing you to combine multiple UriMatchReplace types.
In case you wish a fallthrough behaviour for any supported
slice type or tuple you can also wrap it with UriMatchReplaceFallthrough
which will ensure that Uri’s go through all rules,
preserving the last found match (if any).
For UriMatchReplaceRule it is best to use UriMatchReplaceRuleset
in case you wish to use multiple rules, as it is more optimal
than the blanket slice implementation.
For match-replace rules specifically for a scheme or domain-like
condition it is better to use UriMatchReplaceScheme and
UriMatchReplaceDomain respectively over UriMatchReplaceRule
as it also is more optimal and in some ways more powerful as well.
§Edge cases
- A rule that matches but produces an invalid URI will be skipped.
- When multiple rules could match, only the first one in iteration order is applied.
- Query parameters are part of the match only when the rule’s pattern or
formatter includes
?(escaped with\\) or ends on*
§Contract
A UriMatchReplace is expected to preserve the input Uri as-is
when returning UriMatchError::NoMatch. This error variant
is also to be returned for errors unless the original Uri
is no longer present in which case a UriMatchError::Unexpected
error can be returned.
§Examples
Apply a single rule:
let rule = UriMatchReplaceRule::try_new("http://*", "https://$1").unwrap();
let uri = Uri::from_static("http://example.com/x");
let out = rule.match_replace_uri(Cow::Owned(uri)).unwrap();
assert_eq!(out.to_string(), "https://example.com/x");Apply several rules in order, multiple rules even if applicable:
let rules = UriMatchReplaceFallthrough((
UriMatchReplaceScheme::http_to_https(),
UriMatchReplaceRule::try_new("https://*/docs/*", "https://$1/knowledge/$2").unwrap(),
));
let uri = Uri::from_static("http://example.com/foo/docs/bar");
let out = rules.match_replace_uri(Cow::Owned(uri)).unwrap();
assert_eq!(out.to_string(), "https://example.com/foo/knowledge/bar");Required Methods§
fn match_replace_uri<'a>(
&self,
uri: Cow<'a, Uri>,
) -> Result<Cow<'a, Uri>, UriMatchError<'a>>
fn match_replace_uri<'a>( &self, uri: Cow<'a, Uri>, ) -> Result<Cow<'a, Uri>, UriMatchError<'a>>
Tries to match uri against the rule’s pattern and, on success,
returns the same Uri or a new reformatted Uri.
When the input does not match, or the resulting bytes do not parse as a
valid Uri, None is returned.
Trait Implementations§
§impl UriMatchReplace for Box<dyn UriMatchReplace>
impl UriMatchReplace for Box<dyn UriMatchReplace>
§fn match_replace_uri<'a>(
&self,
uri: Cow<'a, Uri>,
) -> Result<Cow<'a, Uri>, UriMatchError<'a>>
fn match_replace_uri<'a>( &self, uri: Cow<'a, Uri>, ) -> Result<Cow<'a, Uri>, UriMatchError<'a>>
uri against the rule’s pattern and, on success,
returns the same Uri or a new reformatted Uri. Read more