pincer_core/
path_template.rs

1//! Path template for middleware access.
2
3/// The original path template before parameter substitution.
4///
5/// This is stored in request extensions to allow middleware to access
6/// the template pattern (e.g., `/users/{id}`) rather than the resolved
7/// path (e.g., `/users/123`).
8///
9/// # Example
10///
11/// ```ignore
12/// // In middleware
13/// if let Some(template) = request.extensions().get::<PathTemplate>() {
14///     println!("Path template: {}", template.as_str());
15/// }
16/// ```
17#[derive(Debug, Clone, PartialEq, Eq, Hash)]
18pub struct PathTemplate(&'static str);
19
20impl PathTemplate {
21    /// Create a new path template.
22    #[must_use]
23    pub const fn new(template: &'static str) -> Self {
24        Self(template)
25    }
26
27    /// Get the template string.
28    #[must_use]
29    pub const fn as_str(&self) -> &'static str {
30        self.0
31    }
32}
33
34impl std::fmt::Display for PathTemplate {
35    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
36        write!(f, "{}", self.0)
37    }
38}
39
40impl AsRef<str> for PathTemplate {
41    fn as_ref(&self) -> &str {
42        self.0
43    }
44}
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49
50    #[test]
51    fn path_template_as_str() {
52        let template = PathTemplate::new("/users/{id}/posts/{post_id}");
53        assert_eq!(template.as_str(), "/users/{id}/posts/{post_id}");
54    }
55
56    #[test]
57    fn path_template_as_ref() {
58        let template = PathTemplate::new("/users/{id}");
59        let s: &str = template.as_ref();
60        assert_eq!(s, "/users/{id}");
61    }
62}