Skip to main content

nfw_core/routing/
route.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, Serialize, Deserialize)]
4pub enum RouteSegment {
5    Static(String),
6    Dynamic(String),
7    CatchAll(String),
8    OptionalCatchAll(String),
9}
10
11#[derive(Debug, Clone, Serialize, Deserialize)]
12pub struct Route {
13    pub path: String,
14    pub method: RouteMethod,
15    pub file_path: String,
16    pub handler_name: String,
17    #[serde(default)]
18    pub segments: Vec<RouteSegment>,
19}
20
21#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
22pub enum RouteMethod {
23    Get,
24    Post,
25    Put,
26    Delete,
27    Patch,
28    Options,
29    Head,
30}
31
32impl RouteMethod {
33    pub fn as_str(&self) -> &'static str {
34        match self {
35            RouteMethod::Get => "GET",
36            RouteMethod::Post => "POST",
37            RouteMethod::Put => "PUT",
38            RouteMethod::Delete => "DELETE",
39            RouteMethod::Patch => "PATCH",
40            RouteMethod::Options => "OPTIONS",
41            RouteMethod::Head => "HEAD",
42        }
43    }
44}
45
46impl std::fmt::Display for Route {
47    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
48        write!(
49            f,
50            "{} {} -> {}",
51            self.method.as_str(),
52            self.path,
53            self.file_path
54        )
55    }
56}