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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
use serde::{Deserialize, Serialize};
use tide::{Endpoint, Route};
use crate::https::AppState;
pub trait RouteMaps {
fn mapped_method(
&mut self,
routemap: &mut RouteMap,
method: http_types::Method,
ep: impl Endpoint<AppState>,
) -> &mut Self;
fn mapped_delete(&mut self, routemap: &mut RouteMap, ep: impl Endpoint<AppState>) -> &mut Self;
fn mapped_get(&mut self, routemap: &mut RouteMap, ep: impl Endpoint<AppState>) -> &mut Self;
fn mapped_patch(&mut self, routemap: &mut RouteMap, ep: impl Endpoint<AppState>) -> &mut Self;
fn mapped_post(&mut self, routemap: &mut RouteMap, ep: impl Endpoint<AppState>) -> &mut Self;
fn mapped_put(&mut self, routemap: &mut RouteMap, ep: impl Endpoint<AppState>) -> &mut Self;
fn mapped_update(&mut self, routemap: &mut RouteMap, ep: impl Endpoint<AppState>) -> &mut Self;
}
impl RouteMaps for Route<'_, AppState> {
fn mapped_method(
&mut self,
routemap: &mut RouteMap,
method: http_types::Method,
ep: impl Endpoint<AppState>,
) -> &mut Self {
let path_str = self.path().to_string();
let path = match path_str.is_empty() {
true => String::from("/"),
false => path_str,
};
routemap.routelist.push(RouteInfo { path, method });
self.method(method, ep)
}
fn mapped_delete(&mut self, routemap: &mut RouteMap, ep: impl Endpoint<AppState>) -> &mut Self {
self.mapped_method(routemap, http_types::Method::Delete, ep)
}
fn mapped_get(&mut self, routemap: &mut RouteMap, ep: impl Endpoint<AppState>) -> &mut Self {
self.mapped_method(routemap, http_types::Method::Get, ep)
}
fn mapped_patch(&mut self, routemap: &mut RouteMap, ep: impl Endpoint<AppState>) -> &mut Self {
self.mapped_method(routemap, http_types::Method::Patch, ep)
}
fn mapped_post(&mut self, routemap: &mut RouteMap, ep: impl Endpoint<AppState>) -> &mut Self {
self.mapped_method(routemap, http_types::Method::Post, ep)
}
fn mapped_put(&mut self, routemap: &mut RouteMap, ep: impl Endpoint<AppState>) -> &mut Self {
self.mapped_method(routemap, http_types::Method::Put, ep)
}
fn mapped_update(&mut self, routemap: &mut RouteMap, ep: impl Endpoint<AppState>) -> &mut Self {
self.mapped_method(routemap, http_types::Method::Update, ep)
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct RouteInfo {
pub path: String,
pub method: http_types::Method,
}
#[derive(Clone, Debug, Deserialize, Serialize, Default)]
pub struct RouteMap {
pub routelist: Vec<RouteInfo>,
}
impl RouteMap {
pub fn do_map(&self) -> String {
serde_json::to_string_pretty(self).unwrap()
}
pub fn push_self(&mut self, path: String, method: http_types::Method) {
self.routelist.push(RouteInfo { path, method });
}
}