rest_api/api_http_server/
routing.rs

1use super::super::database::table_schema::SqlTableSchema;
2
3
4/// Example
5/// ```
6/// let routes = routes!(
7///    ("/people", PeopleTableSchema),
8///    ("/jobs", JobsTableSchema)
9///);
10/// ```
11#[macro_export]
12macro_rules! routes {
13    ($( ($route:expr, $table:expr) ),*) => {{
14        let mut vec = Vec::new();
15        $( vec.push( Box::new(BasicRoute::new($route.to_string(), $table)) as Box<dyn Route + Send + Sync>); )*
16        vec
17    }};
18}
19
20pub trait Route {
21    // returns matching table name for route
22    fn matches_uri(&self, uri: String) -> bool;
23    fn get_schema(&self) -> &SqlTableSchema;
24}
25
26#[derive(Debug)]
27pub struct BasicRoute {
28    pub route: String,
29    pub table_schema: SqlTableSchema,
30}
31
32impl BasicRoute {
33    pub fn new(route: String, table_schema: SqlTableSchema) -> Self {
34        Self {
35            route,
36            table_schema
37        }
38    }
39}
40
41impl Route for BasicRoute {
42    fn matches_uri(&self, uri: String) -> bool {
43        uri == self.route
44    }
45    fn get_schema(&self) -> &SqlTableSchema {
46        &self.table_schema
47    }
48}
49
50pub fn split_uri_args(uri: String) -> (String, String) {
51    // split at last ?
52    let base_uri;
53    let uri_args;
54
55    let whole_uri = uri.to_string();
56    let path = whole_uri.chars().rev().position(|x| x == '?');
57
58    match path {
59        None => {
60            base_uri = whole_uri;
61            uri_args = String::new();
62        },
63        Some(v) => {
64            let (base, args) = whole_uri.split_at(whole_uri.len()-v);
65            let stripped = base.strip_suffix('?');
66            base_uri = stripped.unwrap_or(base).to_string();
67            uri_args = args.to_string();
68        }
69    }
70    (base_uri, uri_args)
71}