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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
use glob::glob;
use lazy_static::lazy_static;
use quote::format_ident;
use regex::Regex;
use std::cmp::Ordering;

mod utils {
    pub fn get_segments(p: &str) -> Vec<&str> {
        let stripped = p.strip_prefix('/').unwrap_or(p);
        stripped.split('/').collect::<Vec<&str>>()
    }
}

use utils::get_segments;

lazy_static! {
        // Dynamic Route - /api/[id]
        static ref DYNAMIC_ROUTE_REGEX: Regex = Regex::new(r"\[[^/\.]+\]").unwrap();
        // Catch-all Route - /api/[...slug]
        static ref DYNAMIC_CATCH_ALL_REGEX: Regex = Regex::new(r"\[\.{3}\S+\]").unwrap();
        // Optional catch-all Route - /api/[[...slug]]
        static ref DYNAMIC_OPTIONAL_CATCH_ALL_REGEX: Regex = Regex::new(r"\[{2}\.{3}\S+\]{2}").unwrap();
}

#[derive(Debug, PartialEq, PartialOrd)]
pub enum RouteKind {
    Static,
    Dynamic,
    CatchAll,
    OptionalCatchAll,
}

#[derive(Debug)]
pub struct Route {
    pub kind: RouteKind,
    pub module_file: String,
    pub module_name: syn::Ident,
    pub path: String,
    pub segments: Option<Vec<String>>,
}

impl Ord for Route {
    fn cmp(&self, other: &Self) -> Ordering {
        match self.kind {
            // Sort by length in descending order
            RouteKind::Static => match other.kind {
                RouteKind::Static => other.path.len().cmp(&self.path.len()),
                _ => Ordering::Less,
            },
            // Sort by segment length in descending order
            RouteKind::Dynamic => match other.kind {
                RouteKind::Static => Ordering::Greater,
                RouteKind::Dynamic => match self.segments {
                    Some(ref s) => match other.segments {
                        Some(ref o) => {
                            // If segments have equal length, sort by the position of the first dynamic segment
                            // This has to be done so `api/[id]/static` is matched before `api/[id]/[dynamic]`
                            if s.len() == o.len() {
                                let s_pos = s
                                    .iter()
                                    .rev()
                                    .position(|ss| ss.starts_with('[') && ss.ends_with(']'));

                                let o_pos = o
                                    .iter()
                                    .rev()
                                    .position(|os| os.starts_with('[') && os.ends_with(']'));

                                return o_pos.cmp(&s_pos);
                            }

                            o.len().cmp(&s.len())
                        }
                        None => Ordering::Greater,
                    },
                    None => Ordering::Equal,
                },
                RouteKind::CatchAll | RouteKind::OptionalCatchAll => Ordering::Less,
            },
            // Sort by segment length in descending order
            RouteKind::CatchAll | RouteKind::OptionalCatchAll => match other.kind {
                RouteKind::Static => Ordering::Greater,
                RouteKind::Dynamic => Ordering::Greater,
                RouteKind::CatchAll | RouteKind::OptionalCatchAll => match self.segments {
                    Some(ref s) => match other.segments {
                        Some(ref o) => o.len().cmp(&s.len()),
                        None => Ordering::Greater,
                    },
                    None => Ordering::Equal,
                },
            },
        }
    }
}

impl PartialOrd for Route {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Eq for Route {}

impl PartialEq for Route {
    fn eq(&self, other: &Self) -> bool {
        self.kind == other.kind
    }
}

impl From<&str> for Route {
    fn from(file_path: &str) -> Self {
        let file_path = file_path.to_string();
        let route = file_path.strip_suffix(".rs").unwrap_or(&file_path);

        let module_name = file_path.strip_prefix('/').unwrap_or(&file_path);
        let module_name = module_name.replace('/', "_");

        let module_name = module_name.replace('[', "_");
        let module_name = module_name.replace(']', "_");
        let module_name = module_name.replace("...", "___");

        let module_name = module_name.replace('-', "_");
        let module_name = module_name.strip_suffix(".rs").unwrap_or(&module_name);

        // TODO validation that [...slug] and [[...slug]] can only be in the last segment
        let get_route_kind = |r: &str| -> RouteKind {
            if DYNAMIC_ROUTE_REGEX.is_match(r) {
                match DYNAMIC_OPTIONAL_CATCH_ALL_REGEX.is_match(r) {
                    true => return RouteKind::OptionalCatchAll,
                    // false => return RouteKind::Dynamic,
                    false => match DYNAMIC_CATCH_ALL_REGEX.is_match(r) {
                        true => return RouteKind::CatchAll,
                        false => return RouteKind::Dynamic,
                    },
                }
            }

            if DYNAMIC_OPTIONAL_CATCH_ALL_REGEX.is_match(r) {
                return RouteKind::OptionalCatchAll;
            }

            if DYNAMIC_CATCH_ALL_REGEX.is_match(r) {
                return RouteKind::CatchAll;
            }
            RouteKind::Static
        };

        let route_kind = get_route_kind(route);

        let segments = match route_kind {
            RouteKind::Static => None,
            RouteKind::Dynamic => Some(get_segments(route)),
            RouteKind::CatchAll => Some(get_segments(route)),
            RouteKind::OptionalCatchAll => Some(get_segments(route)),
        };

        let segments = segments.map(|s| s.iter().map(|s| s.to_string()).collect::<Vec<_>>());

        Route {
            kind: route_kind,
            // module_file: format!("../{}", file_path),
            module_file: file_path.to_owned(),
            module_name: format_ident!("{}", module_name.to_owned()),
            path: route.to_owned(),
            segments,
        }
    }
}

pub struct Router {
    pub routes: Vec<Route>,
}

impl Default for Router {
    fn default() -> Self {
        Self::new("api/**/*.rs")
    }
}

impl From<Vec<&str>> for Router {
    fn from(raw_paths: Vec<&str>) -> Self {
        let mut routes: Vec<Route> = raw_paths.into_iter().map(Route::from).collect();
        routes.sort();
        Router { routes }
    }
}

impl Router {
    pub fn new(file_pattern: &str) -> Self {
        let mut routes = glob(file_pattern)
            .expect("Failed to read glob pattern")
            .filter_map(|e| e.ok())
            .map(|raw_path| {
                let path = raw_path.to_str().unwrap();
                Route::from(path)
            })
            .collect::<Vec<_>>();

        routes.sort();
        Router { routes }
    }

    pub fn call(&self, req_path: &str) -> Option<&Route> {
        // Check if there is an optional catch all route
        if let Some(optional_catch_all) = self.routes.iter().find(|r| {
            let dynamic_optional_catch_all_exp = Regex::new(r"\[{2}\.{3}\S+\]{2}").unwrap();
            let optional_catchall_route =
                dynamic_optional_catch_all_exp.replace_all(r.path.as_str(), "");
            let optional_catchall_route = optional_catchall_route.trim_end_matches('/');

            r.kind == RouteKind::OptionalCatchAll && req_path == optional_catchall_route
        }) {
            return Some(optional_catch_all);
        };

        let result = self.routes.iter().find(|route| {
            match route.kind {
                RouteKind::Static => route.path == req_path,
                RouteKind::Dynamic => {
                    let path_segements = get_segments(req_path);
                    // Check if all segements are identical (ignoring wildcards)
                    match route.segments {
                        None => false,
                        Some(ref route_segments) => {
                            if route_segments.len() != path_segements.len() {
                                return false;
                            }

                            route_segments.iter().enumerate().all(|(i, rs)| {
                                (rs.contains('[') && rs.contains(']')) || rs == path_segements[i]
                            })
                        }
                    }
                }
                RouteKind::OptionalCatchAll => {
                    // todo extract logic
                    let optional_catchall_prefix =
                        DYNAMIC_OPTIONAL_CATCH_ALL_REGEX.replace_all(route.path.as_str(), "");
                    req_path.starts_with(optional_catchall_prefix.as_ref())
                }
                RouteKind::CatchAll => {
                    // todo extract logic
                    let catchall_prefix =
                        DYNAMIC_CATCH_ALL_REGEX.replace_all(route.path.as_str(), "");
                    req_path.starts_with(catchall_prefix.as_ref())
                }
            }
        });

        result
    }
}

#[cfg(test)]
mod tests {
    use super::Router;

    #[test]
    fn dynamic_routing() {
        let router = Router::from(vec![
            "api/posts.rs",
            "api/[id].rs",
            "api/posts/[id].rs",
            "api/[...id].rs",
            "api/nested/posts.rs",
            "api/nested/[id].rs",
            "api/nested/posts/[id].rs",
            "api/nested/[...id].rs",
            "api/optional/posts.rs",
            "api/optional/[id].rs",
            "api/optional/posts/[id].rs",
            "api/optional.rs",
            "api/optional/[[...id]].rs",
            "api/deep/nested/[id]/comments/[cid].rs",
            "api/other/[ab]/[cd]/ef.rs",
            "api/foo/[d]/bar/baz/[f].rs",
            "api/github/[owner]/[release]/baz/[f].rs",
            "api/github/[owner]/[name]/releases/[release].rs",
            "api/github/[owner]/[name]/releases/all.rs",
            "api/github/[owner]/[name]/releases/latest.rs",
            "api/github/[owner]/[name]/tags/[...all].rs",
            "api/github/[owner]/[name]/tags/latest.rs",
        ]);

        // Root
        insta::assert_debug_snapshot!(router.call("api/posts"));
        insta::assert_debug_snapshot!(router.call("api/[id]"));
        insta::assert_debug_snapshot!(router.call("api/posts/[id]"));
        insta::assert_debug_snapshot!(router.call("api"));
        insta::assert_debug_snapshot!(router.call("api/root/catch/all/route"));
        // Catch-all - Nested
        insta::assert_debug_snapshot!(router.call("api/nested/posts"));
        insta::assert_debug_snapshot!(router.call("api/nested/[id]"));
        insta::assert_debug_snapshot!(router.call("api/nested/posts/[id]"));
        insta::assert_debug_snapshot!(router.call("api/nested"));
        insta::assert_debug_snapshot!(router.call("api/nested/catch/all/route"));
        // Optional Catch-all - Nested
        insta::assert_debug_snapshot!(router.call("api/optional/posts"));
        insta::assert_debug_snapshot!(router.call("api/optional/[id]"));
        insta::assert_debug_snapshot!(router.call("api/optional/posts/[id]"));
        insta::assert_debug_snapshot!(router.call("api/optional"));
        insta::assert_debug_snapshot!(router.call("api/optional/catch/all/route"));
        // Dynamic Deep Nested
        insta::assert_debug_snapshot!(router.call("api/deep/nested/[id]/comments/[cid]"));
        insta::assert_debug_snapshot!(router.call("api/should/be/caught/by/root/catch/all"));
        insta::assert_debug_snapshot!(router.call("api/other/[ab]/[cd]/ef"));
        insta::assert_debug_snapshot!(router.call("api/foo/[d]/bar/baz/[f]"));
        // Dynamic Nested Static + Dynamic
        insta::assert_debug_snapshot!(router.call("api/github/ecklf/rust-at-home/releases/foo"));
        insta::assert_debug_snapshot!(router.call("api/github/ecklf/rust-at-home/releases/latest"));
        insta::assert_debug_snapshot!(router.call("api/github/ecklf/rust-at-home/releases/all"));
        insta::assert_debug_snapshot!(router.call("api/github/ecklf/rust-at-home/tags/v0.1.0"));
        insta::assert_debug_snapshot!(router.call("api/github/ecklf/rust-at-home/tags/latest"));
    }
}

#[cfg(test)]
mod route_tests {
    use super::{Route, RouteKind};

    #[test]
    fn it_creates_static_route() {
        let path = "api/handler";
        let route = Route::from(path);
        assert!(matches!(route.kind, RouteKind::Static));
        assert_eq!(route.path, path);
        assert!(route.segments.is_none());
    }

    #[test]
    fn it_creates_dynamic_route() {
        let path = "api/[dyn]";
        let route = Route::from(path);
        assert!(matches!(route.kind, RouteKind::Dynamic));
        assert_eq!(route.path, path);
        assert!(route.segments.is_some());
        assert_eq!(route.segments.unwrap(), vec!["api", "[dyn]"]);
    }

    #[test]
    fn it_creates_complex_dynamic_route() {
        let path = "api/[dyn]/handler/[dyn2]";
        let route = Route::from(path);
        assert!(matches!(route.kind, RouteKind::Dynamic));
        assert_eq!(route.path, path);
        assert!(route.segments.is_some());
        assert_eq!(
            route.segments.unwrap(),
            vec!["api", "[dyn]", "handler", "[dyn2]"]
        );
    }

    #[test]
    fn it_creates_catch_all_route() {
        let path = "api/[...all]";
        let route = Route::from(path);
        assert!(matches!(route.kind, RouteKind::CatchAll));
        assert_eq!(route.path, path);
        assert!(route.segments.is_some());
        assert_eq!(route.segments.unwrap(), vec!["api", "[...all]"]);
    }

    #[test]
    fn it_creates_complex_catch_all_route() {
        let path = "api/[dyn]/handler/[...all]";
        let route = Route::from(path);
        assert!(matches!(route.kind, RouteKind::CatchAll));
        assert_eq!(route.path, path);
        assert!(route.segments.is_some());
        assert_eq!(
            route.segments.unwrap(),
            vec!["api", "[dyn]", "handler", "[...all]"]
        );
    }

    #[test]
    fn it_creates_optional_catch_all_route() {
        let path = "api/[[...all]]";
        let route = Route::from(path);
        assert!(matches!(route.kind, RouteKind::OptionalCatchAll));
        assert_eq!(route.path, path);
        assert!(route.segments.is_some());
        assert_eq!(route.segments.unwrap(), vec!["api", "[[...all]]"]);
    }

    #[test]
    fn it_creates_complex_optional_catch_all_route() {
        let path = "api/[dyn]/handler/[[...all]]";
        let route = Route::from(path);
        assert!(matches!(route.kind, RouteKind::OptionalCatchAll));
        assert_eq!(route.path, path);
        assert!(route.segments.is_some());
        assert_eq!(
            route.segments.unwrap(),
            vec!["api", "[dyn]", "handler", "[[...all]]"]
        );
    }
}