UriMatchReplace

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:

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

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>

§

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

Implementations on Foreign Types§

§

impl UriMatchReplace for Arc<dyn UriMatchReplace>

§

fn match_replace_uri<'a>( &self, uri: Cow<'a, Uri>, ) -> Result<Cow<'a, Uri>, UriMatchError<'a>>

§

impl<R> UriMatchReplace for &[R]
where R: UriMatchReplace,

§

fn match_replace_uri<'a>( &self, uri: Cow<'a, Uri>, ) -> Result<Cow<'a, Uri>, UriMatchError<'a>>

§

impl<R> UriMatchReplace for &R
where R: UriMatchReplace,

§

fn match_replace_uri<'a>( &self, uri: Cow<'a, Uri>, ) -> Result<Cow<'a, Uri>, UriMatchError<'a>>

§

impl<R> UriMatchReplace for Arc<R>
where R: UriMatchReplace,

§

fn match_replace_uri<'a>( &self, uri: Cow<'a, Uri>, ) -> Result<Cow<'a, Uri>, UriMatchError<'a>>

§

impl<R, const N: usize> UriMatchReplace for [R; N]
where R: UriMatchReplace,

§

fn match_replace_uri<'a>( &self, uri: Cow<'a, Uri>, ) -> Result<Cow<'a, Uri>, UriMatchError<'a>>

§

impl<T1> UriMatchReplace for (T1,)
where T1: UriMatchReplace,

§

fn match_replace_uri<'a>( &self, uri: Cow<'a, Uri>, ) -> Result<Cow<'a, Uri>, UriMatchError<'a>>

§

impl<T1, T2> UriMatchReplace for (T1, T2)

§

fn match_replace_uri<'a>( &self, uri: Cow<'a, Uri>, ) -> Result<Cow<'a, Uri>, UriMatchError<'a>>

§

impl<T1, T2, T3> UriMatchReplace for (T1, T2, T3)

§

fn match_replace_uri<'a>( &self, uri: Cow<'a, Uri>, ) -> Result<Cow<'a, Uri>, UriMatchError<'a>>

§

impl<T1, T2, T3, T4> UriMatchReplace for (T1, T2, T3, T4)

§

fn match_replace_uri<'a>( &self, uri: Cow<'a, Uri>, ) -> Result<Cow<'a, Uri>, UriMatchError<'a>>

§

impl<T1, T2, T3, T4, T5> UriMatchReplace for (T1, T2, T3, T4, T5)

§

fn match_replace_uri<'a>( &self, uri: Cow<'a, Uri>, ) -> Result<Cow<'a, Uri>, UriMatchError<'a>>

§

impl<T1, T2, T3, T4, T5, T6> UriMatchReplace for (T1, T2, T3, T4, T5, T6)

§

fn match_replace_uri<'a>( &self, uri: Cow<'a, Uri>, ) -> Result<Cow<'a, Uri>, UriMatchError<'a>>

§

impl<T1, T2, T3, T4, T5, T6, T7> UriMatchReplace for (T1, T2, T3, T4, T5, T6, T7)

§

fn match_replace_uri<'a>( &self, uri: Cow<'a, Uri>, ) -> Result<Cow<'a, Uri>, UriMatchError<'a>>

§

impl<T1, T2, T3, T4, T5, T6, T7, T8> UriMatchReplace for (T1, T2, T3, T4, T5, T6, T7, T8)

§

fn match_replace_uri<'a>( &self, uri: Cow<'a, Uri>, ) -> Result<Cow<'a, Uri>, UriMatchError<'a>>

§

impl<T1, T2, T3, T4, T5, T6, T7, T8, T9> UriMatchReplace for (T1, T2, T3, T4, T5, T6, T7, T8, T9)

§

fn match_replace_uri<'a>( &self, uri: Cow<'a, Uri>, ) -> Result<Cow<'a, Uri>, UriMatchError<'a>>

§

impl<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> UriMatchReplace for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)

§

fn match_replace_uri<'a>( &self, uri: Cow<'a, Uri>, ) -> Result<Cow<'a, Uri>, UriMatchError<'a>>

§

impl<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> UriMatchReplace for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)

§

fn match_replace_uri<'a>( &self, uri: Cow<'a, Uri>, ) -> Result<Cow<'a, Uri>, UriMatchError<'a>>

§

impl<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12> UriMatchReplace for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12)

§

fn match_replace_uri<'a>( &self, uri: Cow<'a, Uri>, ) -> Result<Cow<'a, Uri>, UriMatchError<'a>>

Implementors§

§

impl UriMatchReplace for Box<dyn UriMatchReplace>

§

impl UriMatchReplace for UriMatchReplaceDomain

§

impl UriMatchReplace for UriMatchReplaceRule

§

impl UriMatchReplace for UriMatchReplaceRuleset

§

impl UriMatchReplace for UriMatchReplaceScheme

§

impl UriMatchReplace for UriMatchReplaceNever

§

impl<A, B> UriMatchReplace for Either<A, B>

§

impl<A, B, C> UriMatchReplace for Either3<A, B, C>

§

impl<A, B, C, D> UriMatchReplace for Either4<A, B, C, D>

§

impl<A, B, C, D, E> UriMatchReplace for Either5<A, B, C, D, E>

§

impl<A, B, C, D, E, F> UriMatchReplace for Either6<A, B, C, D, E, F>

§

impl<A, B, C, D, E, F, G> UriMatchReplace for Either7<A, B, C, D, E, F, G>

§

impl<A, B, C, D, E, F, G, H> UriMatchReplace for Either8<A, B, C, D, E, F, G, H>

§

impl<A, B, C, D, E, F, G, H, I> UriMatchReplace for Either9<A, B, C, D, E, F, G, H, I>

§

impl<R> UriMatchReplace for Option<R>
where R: UriMatchReplace,

§

impl<R> UriMatchReplace for Vec<R>
where R: UriMatchReplace,

§

impl<R> UriMatchReplace for UriMatchReplaceFallthrough<&[R]>
where R: UriMatchReplace,

§

impl<R> UriMatchReplace for UriMatchReplaceFallthrough<Vec<R>>
where R: UriMatchReplace,

§

impl<R, const N: usize> UriMatchReplace for UriMatchReplaceFallthrough<[R; N]>
where R: UriMatchReplace,

§

impl<T1> UriMatchReplace for UriMatchReplaceFallthrough<(T1,)>
where T1: UriMatchReplace,

§

impl<T1, T2> UriMatchReplace for UriMatchReplaceFallthrough<(T1, T2)>

§

impl<T1, T2, T3> UriMatchReplace for UriMatchReplaceFallthrough<(T1, T2, T3)>

§

impl<T1, T2, T3, T4> UriMatchReplace for UriMatchReplaceFallthrough<(T1, T2, T3, T4)>

§

impl<T1, T2, T3, T4, T5> UriMatchReplace for UriMatchReplaceFallthrough<(T1, T2, T3, T4, T5)>

§

impl<T1, T2, T3, T4, T5, T6> UriMatchReplace for UriMatchReplaceFallthrough<(T1, T2, T3, T4, T5, T6)>

§

impl<T1, T2, T3, T4, T5, T6, T7> UriMatchReplace for UriMatchReplaceFallthrough<(T1, T2, T3, T4, T5, T6, T7)>

§

impl<T1, T2, T3, T4, T5, T6, T7, T8> UriMatchReplace for UriMatchReplaceFallthrough<(T1, T2, T3, T4, T5, T6, T7, T8)>

§

impl<T1, T2, T3, T4, T5, T6, T7, T8, T9> UriMatchReplace for UriMatchReplaceFallthrough<(T1, T2, T3, T4, T5, T6, T7, T8, T9)>

§

impl<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> UriMatchReplace for UriMatchReplaceFallthrough<(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)>

§

impl<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> UriMatchReplace for UriMatchReplaceFallthrough<(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)>

§

impl<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12> UriMatchReplace for UriMatchReplaceFallthrough<(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12)>