Skip to main content

a3s_boot/routing/route/
definition.rs

1use crate::{ExceptionFilter, Guard, HttpMethod, Interceptor, Pipe, Result};
2use std::collections::BTreeMap;
3use std::sync::Arc;
4
5use crate::routing::handler::RouteHandler;
6use crate::routing::path::{
7    match_path_params, match_path_shape, route_param_names, route_shape_key, validate_route_path,
8};
9
10/// A framework-neutral route definition.
11#[derive(Clone)]
12pub struct RouteDefinition {
13    pub(super) method: HttpMethod,
14    pub(super) path: String,
15    pub(super) handler: Arc<dyn RouteHandler>,
16    pub(super) pipes: Vec<Arc<dyn Pipe>>,
17    pub(super) guards: Vec<Arc<dyn Guard>>,
18    pub(super) interceptors: Vec<Arc<dyn Interceptor>>,
19    pub(super) filters: Vec<Arc<dyn ExceptionFilter>>,
20    pub(super) module_name: Option<String>,
21    pub(super) controller_prefix: Option<String>,
22}
23
24impl RouteDefinition {
25    pub fn new<H>(method: HttpMethod, path: impl Into<String>, handler: H) -> Result<Self>
26    where
27        H: RouteHandler,
28    {
29        let path = path.into();
30        validate_route_path(&path)?;
31        Ok(Self {
32            method,
33            path,
34            handler: Arc::new(handler),
35            pipes: Vec::new(),
36            guards: Vec::new(),
37            interceptors: Vec::new(),
38            filters: Vec::new(),
39            module_name: None,
40            controller_prefix: None,
41        })
42    }
43
44    pub fn method(&self) -> HttpMethod {
45        self.method
46    }
47
48    pub fn path(&self) -> &str {
49        &self.path
50    }
51
52    pub fn path_shape(&self) -> String {
53        route_shape_key(&self.path)
54    }
55
56    pub fn path_param_names(&self) -> Vec<&str> {
57        route_param_names(&self.path)
58    }
59
60    pub fn matches_path(&self, path: &str) -> bool {
61        match_path_shape(&self.path, path)
62    }
63
64    pub fn path_params(&self, path: &str) -> Result<Option<BTreeMap<String, String>>> {
65        match_path_params(&self.path, path)
66    }
67
68    pub fn module_name(&self) -> Option<&str> {
69        self.module_name.as_deref()
70    }
71
72    pub fn controller_prefix(&self) -> Option<&str> {
73        self.controller_prefix.as_deref()
74    }
75
76    pub fn handler(&self) -> Arc<dyn RouteHandler> {
77        Arc::clone(&self.handler)
78    }
79}