1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use std::fmt::Formatter;

#[derive(Debug, Clone, Copy)]
/// The route template that matched for the incoming request.
///
/// # Example
///
/// If you configure your [`Blueprint`] like this:
///
/// ```rust
/// use pavex::{f, blueprint::{Blueprint, router::GET}};
/// # use pavex::{request::RequestHead, response::Response};
/// # fn get_home(request: RequestHead) -> Response { todo!() }
/// # fn main() {
/// # let mut bp = Blueprint::new();
///
/// bp.route(GET, "/home/:home_id", f!(crate::get_home));
/// # }
/// ```
///
/// Then [`MatchedPathPattern`] will be set to `/home/:home_id` for a `GET /home/123` request.
///
/// # Framework primitive
///
/// `MatchedPathPattern` is a framework primitive—you don't need to register any constructor
/// with [`Blueprint`] to use it in your application.
///
/// # Use cases
///
/// The primary use case for [`MatchedPathPattern`] is telemetry—logging, metrics, etc.  
/// It lets you strip away the dynamic parts of the request path, thus reducing the cardinality of
/// your metrics and making it easier to aggregate them.
///
/// [`Blueprint`]: crate::blueprint::Blueprint
#[doc(alias("MatchedPath"))]
#[doc(alias("MatchedPathTemplate"))]
#[doc(alias("PathPattern"))]
#[doc(alias("PathTemplate"))]
#[doc(alias("MatchedRoute"))]
#[doc(alias("MatchedRouteTemplate"))]
pub struct MatchedPathPattern(&'static str);

impl MatchedPathPattern {
    /// Create a new matched route from a route template.
    ///
    /// # Example
    ///
    /// ```rust
    /// use pavex::request::path::MatchedPathPattern;
    ///
    /// let matched_route = MatchedPathPattern::new("/home/:home_id");
    /// ```
    pub fn new(route: &'static str) -> Self {
        Self(route)
    }

    /// Get a reference to the underlying route template.
    pub fn inner(self) -> &'static str {
        self.0
    }
}

impl std::fmt::Display for MatchedPathPattern {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}