pavex/request/path/matched_path.rs
1use std::fmt::Formatter;
2
3#[derive(Debug, Clone, Copy)]
4/// The route template that matched for the incoming request.
5///
6/// # Example
7///
8/// If you configure your [`Blueprint`] like this:
9///
10/// ```rust
11/// use pavex::{get, Response};
12/// #[get(path = "/home/{home_id}")]
13/// pub fn get_home(/* [...] */) -> Response {
14/// // [...]
15/// # Response::ok()
16/// }
17/// ```
18///
19/// Then [`MatchedPathPattern`] will be set to `/home/{home_id}` for a `GET /home/123` request.
20///
21/// # Framework primitive
22///
23/// `MatchedPathPattern` is a framework primitive—you don't need to register any constructor
24/// with [`Blueprint`] to use it in your application.
25///
26/// # Use cases
27///
28/// The primary use case for [`MatchedPathPattern`] is telemetry—logging, metrics, etc.
29/// It lets you strip away the dynamic parts of the request path, thus reducing the cardinality of
30/// your metrics and making it easier to aggregate them.
31///
32/// [`Blueprint`]: crate::Blueprint
33#[doc(alias("MatchedPath"))]
34#[doc(alias("MatchedPathTemplate"))]
35#[doc(alias("PathPattern"))]
36#[doc(alias("PathTemplate"))]
37#[doc(alias("MatchedRoute"))]
38#[doc(alias("MatchedRouteTemplate"))]
39pub struct MatchedPathPattern(&'static str);
40
41impl MatchedPathPattern {
42 /// Create a new matched route from a route template.
43 ///
44 /// # Example
45 ///
46 /// ```rust
47 /// use pavex::request::path::MatchedPathPattern;
48 ///
49 /// let matched_route = MatchedPathPattern::new("/home/:home_id");
50 /// ```
51 pub fn new(route: &'static str) -> Self {
52 Self(route)
53 }
54
55 /// Get a reference to the underlying route template.
56 pub fn inner(self) -> &'static str {
57 self.0
58 }
59}
60
61impl std::fmt::Display for MatchedPathPattern {
62 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
63 write!(f, "{}", self.0)
64 }
65}