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
///! Route-mapping magic for tide
///
/// Instead of adding routes with (for example) the .post method you add them with .mapped_post, passing an instance of [RouteMap] and it'll do the rest...
use serde::{Deserialize, Serialize};
use tide::{Endpoint, Route};

use crate::https::AppState;

// Extends the tide::Route for RouteMaps, this would really be nice if it was generic :(
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> {
    // add a mapped method to the list
    fn mapped_method(
        &mut self,
        routemap: &mut RouteMap,
        method: http_types::Method,
        ep: impl Endpoint<AppState>,
    ) -> &mut Self {
        // TODO: truly weird things involving ASTs and sacrifices to eldritch gods to figure out how to represent the Endpoint

        // if the path is empty then it's the root path...
        let path_str = self.path().to_string();
        let path = match path_str.is_empty() {
            true => String::from("/"),
            false => path_str,
        };

        // debug!("Mapping route: {:?}", path);
        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)]
/// Information about a given route
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 {
    // Serializes the object out to a pretty JSON blob
    pub fn do_map(&self) -> String {
        serde_json::to_string_pretty(self).unwrap()
    }

    // Inject the route for the routemap endpoint
    pub fn push_self(&mut self, path: String, method: http_types::Method) {
        self.routelist.push(RouteInfo { path, method });
    }
}