cot 0.8.0

The Rust web framework for lazy developers.
Documentation
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
use std::collections::HashMap;

use cot::router::{Route, RouteKind};
use matchit::{Match, Router as MatchitRouter};

use crate::router::RouteConflictError;
use crate::router::path::{AbsolutePath, PathPart};

pub(super) const NESTED_ROUTER_PARAM: &str = "__cot_nested_router__";

#[derive(Debug, Clone, Hash, Eq, PartialEq)]
pub(super) struct MatchitPattern(String);

impl MatchitPattern {
    #[must_use]
    pub(super) fn new<T: Into<String>>(pattern: T) -> Self {
        Self(pattern.into())
    }

    #[must_use]
    pub(super) fn as_str(&self) -> &str {
        self.0.as_str()
    }
}

impl From<MatchitPattern> for String {
    fn from(value: MatchitPattern) -> Self {
        value.0
    }
}

#[derive(Debug, Clone, Copy)]
pub(super) enum Entry {
    Handler(usize),
    Router(usize),
}

#[derive(Debug, Clone)]
pub(super) struct RouteTrie {
    inner: MatchitRouter<Entry>,
}

impl RouteTrie {
    pub(super) fn build(routes: &[Route]) -> super::Result<Self> {
        let mut inner = MatchitRouter::new();

        let mut pattern_map: HashMap<MatchitPattern, (Option<usize>, Option<usize>)> =
            HashMap::new();
        for (i, route) in routes.iter().enumerate() {
            let pattern = if route.kind() == RouteKind::Router {
                // normalize path of sub-routers since we will attach an
                // internal wildcard sentinel. This should also reject routes
                // for routers(sub-routers) who's version without a trailing
                // slash already exist. (eg. `foo` and `foo/` cannot overlap
                // as sub-routers)
                router_mount_pattern(route)
            } else {
                MatchitPattern::try_from(route.url.clone())?
            };
            let entry = pattern_map.entry(pattern).or_default();
            match route.kind() {
                RouteKind::Handler => {
                    if let Some(existing) = entry.0 {
                        return Err(RouteConflictError::DuplicateHandler {
                            existing: routes[existing].url(),
                            new: route.url(),
                        }
                        .into());
                    }
                    entry.0 = Some(i);
                }
                RouteKind::Router => {
                    if let Some(existing) = entry.1 {
                        return Err(RouteConflictError::DuplicateRouter {
                            existing: routes[existing].url(),
                            new: route.url(),
                        }
                        .into());
                    }
                    entry.1 = Some(i);
                }
            }
        }

        let mut entries: Vec<_> = pattern_map.into_iter().collect();
        // sort for deterministic insertion behavior
        entries.sort_by_key(|(_, (handler_idx, router_idx))| {
            handler_idx
                .or(*router_idx)
                .expect("route index should exist")
        });

        for (_, (handler_idx, router_idx)) in entries {
            let value = match (handler_idx, router_idx) {
                (Some(h), None) => Entry::Handler(h),
                (None, Some(r)) => Entry::Router(r),
                // for cases where a handler overlaps a router for the same route/path, the handler
                // takes precedence.
                (Some(h), Some(_r)) => Entry::Handler(h),
                (None, None) => unreachable!("there should always be a route or handler or both"),
            };

            let route_idx = handler_idx
                .or(router_idx)
                .expect("route index should exist");

            // we insert the original path, not the (possibly trimmed) deduped
            // route so that routers(sub-routers) that were
            // mounted/declared with trailing slashes still match.
            let insertion_pattern = MatchitPattern::try_from(routes[route_idx].url.clone())?;
            Self::insert_or_diagnose(
                &mut inner,
                insertion_pattern,
                value,
                &routes[route_idx],
                routes,
            )?;

            // when a nested router is provided, we treat it as a "false"
            // wildcard segment and keep a sentinel there so we can
            // use that to find what sub router to search at lookup
            // time.
            if let Some(r) = router_idx {
                let prefix = AbsolutePath::new(routes[route_idx].url());
                let wildcard_suffix = AbsolutePath::new(format!("{{*{NESTED_ROUTER_PARAM}}}"));
                let wildcard = prefix.join(&wildcard_suffix);

                Self::insert_or_diagnose(
                    &mut inner,
                    MatchitPattern::new(wildcard.as_str()),
                    Entry::Router(r),
                    &routes[r],
                    routes,
                )?;
            }
        }

        Ok(Self { inner })
    }

    fn insert_or_diagnose(
        trie: &mut MatchitRouter<Entry>,
        pattern: MatchitPattern,
        value: Entry,
        new_route: &Route,
        routes: &[Route],
    ) -> super::Result<()> {
        trie.insert(pattern, value)
            .map_err(|err| Self::diagnose(new_route, err, routes).into())
    }

    fn diagnose(
        new_route: &Route,
        err: matchit::InsertError,
        routes: &[Route],
    ) -> RouteConflictError {
        match err {
            matchit::InsertError::Conflict { with } => {
                let existing_route = routes.iter().find(|r| {
                    MatchitPattern::try_from(r.url.clone()).is_ok_and(|p| p.as_str() == with)
                });

                if let Some(existing_route) = existing_route {
                    Self::classify(existing_route, new_route)
                } else {
                    RouteConflictError::RouteInsert(matchit::InsertError::Conflict { with })
                }
            }

            other => RouteConflictError::RouteInsert(other),
        }
    }

    fn classify(existing_route: &Route, new_route: &Route) -> RouteConflictError {
        for (existing_part, new_part) in
            existing_route.url.parts().iter().zip(new_route.url.parts())
        {
            match (existing_part, new_part) {
                (PathPart::Param { name: a }, PathPart::Param { name: b }) if a != b => {
                    return RouteConflictError::ConflictingParamName {
                        existing: existing_route.url(),
                        existing_name: a.clone(),
                        new: new_route.url(),
                        new_name: b.clone(),
                    };
                }
                (PathPart::Wildcard { name: a }, PathPart::Wildcard { name: b }) if a != b => {
                    return RouteConflictError::ConflictingWildcardName {
                        existing: existing_route.url(),
                        existing_name: a.clone(),
                        new: new_route.url(),
                        new_name: b.clone(),
                    };
                }
                (PathPart::Wildcard { .. }, PathPart::Wildcard { .. }) => {
                    return RouteConflictError::DuplicateWildcard {
                        existing: existing_route.url(),
                        new: new_route.url(),
                    };
                }
                _ => continue,
            }
        }

        // Every segment matched so this is a duplicate
        RouteConflictError::DuplicateHandler {
            existing: existing_route.url(),
            new: new_route.url(),
        }
    }

    pub(super) fn at<'a>(&'a self, path: &'a str) -> Option<Match<'a, 'a, &'a Entry>> {
        self.inner.at(path).ok()
    }
}

pub(super) fn router_mount_pattern(route: &Route) -> MatchitPattern {
    let url = route.url();
    let trimmed = url
        .strip_suffix('/')
        .filter(|s| !s.is_empty())
        .unwrap_or(&url);
    MatchitPattern::new(trimmed)
}

#[cfg(test)]
mod tests {
    use cot::router::Route;

    use super::*;
    use crate::html::Html;
    use crate::router::Router;

    async fn handler() -> Html {
        Html::new("ok")
    }

    fn route(url: &str) -> Route {
        Route::with_handler(url, handler)
    }

    #[test]
    fn build_single_handler_route() {
        let routes = vec![route("/users")];
        let trie = RouteTrie::build(&routes).unwrap();

        let m = trie.at("/users").unwrap();
        assert!(matches!(m.value, Entry::Handler(0)));
    }

    #[test]
    fn build_no_match_returns_none() {
        let routes = vec![route("/users")];
        let trie = RouteTrie::build(&routes).unwrap();

        assert!(trie.at("/other").is_none());
    }

    #[test]
    fn build_root_path_matches() {
        let routes = vec![route("/")];
        let trie = RouteTrie::build(&routes).unwrap();

        assert!(matches!(trie.at("/").unwrap().value, Entry::Handler(0)));
    }

    #[test]
    fn build_param_route_captures_value() {
        let routes = vec![route("/users/{id}")];
        let trie = RouteTrie::build(&routes).unwrap();

        let m = trie.at("/users/42").unwrap();
        assert!(matches!(m.value, Entry::Handler(0)));
        assert_eq!(m.params.get("id"), Some("42"));
    }

    #[test]
    fn build_wildcard_route_captures_remaining_path() {
        let routes = vec![route("/static/{*path}")];
        let trie = RouteTrie::build(&routes).unwrap();

        let m = trie.at("/static/css/app.css").unwrap();
        assert!(matches!(m.value, Entry::Handler(0)));
        assert_eq!(m.params.get("path"), Some("css/app.css"));
    }

    #[test]
    fn build_router_route_inserts_wildcard_sentinel() {
        let sub_router = Router::with_urls(vec![route("/inner")]);
        let routes = vec![Route::with_router("/api", sub_router)];
        let trie = RouteTrie::build(&routes).unwrap();

        assert!(matches!(trie.at("/api").unwrap().value, Entry::Router(0)));

        let m = trie.at("/api/inner").unwrap();
        assert!(matches!(m.value, Entry::Router(0)));
        assert_eq!(m.params.get(NESTED_ROUTER_PARAM), Some("inner"));
    }

    #[test]
    fn build_router_trailing_slash_prefix_does_not_double_slash() {
        let sub_router = Router::with_urls(vec![route("/inner")]);
        let routes = vec![Route::with_router("/api/", sub_router)];
        let trie = RouteTrie::build(&routes).unwrap();

        let m = trie.at("/api/inner").unwrap();
        assert_eq!(m.params.get(NESTED_ROUTER_PARAM), Some("inner"));
    }

    #[test]
    fn build_combined_handler_and_router_same_path() {
        let sub_router = Router::with_urls(vec![route("/inner")]);
        let routes = vec![Route::with_router("/api", sub_router), route("/api")];
        let trie = RouteTrie::build(&routes).unwrap();

        assert!(matches!(trie.at("/api").unwrap().value, Entry::Handler(1)));
    }

    #[test]
    fn static_route_priority_over_param_route() {
        let routes = vec![route("/users/{id}"), route("/users/new")];
        let trie = RouteTrie::build(&routes).unwrap();

        assert!(matches!(
            trie.at("/users/new").unwrap().value,
            Entry::Handler(1)
        ));
    }

    #[test]
    fn build_duplicate_handler_errors() {
        let routes = vec![route("/users"), route("/users")];
        let err = RouteTrie::build(&routes).unwrap_err();
        assert!(err.to_string().contains("duplicate route"));
    }

    #[test]
    fn build_duplicate_router_errors() {
        let routes = vec![
            Route::with_router("/users", Router::empty()),
            Route::with_router("/users", Router::empty()),
        ];
        let err = RouteTrie::build(&routes).unwrap_err();
        assert!(err.to_string().contains("duplicate nested router"));
    }

    #[test]
    fn build_conflicting_param_names_errors() {
        let routes = vec![route("/foo/{bar}/"), route("/foo/{baz}/")];
        let err = RouteTrie::build(&routes).unwrap_err();
        assert!(err.to_string().contains("conflicting route parameters"));
    }

    #[test]
    fn build_conflicting_wildcard_names_errors() {
        let routes = vec![route("/static/{*path}"), route("/static/{*file_path}")];
        let err = RouteTrie::build(&routes).unwrap_err();
        assert!(err.to_string().contains("conflicting wildcard parameters"));
    }

    #[test]
    fn build_duplicate_wildcard_errors() {
        let routes = vec![route("/static/{*path}"), route("/static/{*path}")];
        let err = RouteTrie::build(&routes).unwrap_err();
        assert!(err.to_string().contains("duplicate route"));
    }

    #[test]
    fn build_root_mounted_router_matches_root_path() {
        let sub_router = Router::with_urls(vec![route("/")]);
        let routes = vec![Route::with_router("", sub_router)];
        let trie = RouteTrie::build(&routes).unwrap();

        let m = trie.at("/").unwrap();
        assert!(matches!(m.value, Entry::Router(0)));
    }

    #[test]
    fn build_root_mounted_router_exact_match_has_no_wildcard_capture() {
        let sub_router = Router::with_urls(vec![route("/")]);
        let routes = vec![Route::with_router("/", sub_router)];
        let trie = RouteTrie::build(&routes).unwrap();

        let m = trie.at("/").unwrap();
        assert!(m.params.get(NESTED_ROUTER_PARAM).is_none());
    }

    #[test]
    fn matchit_pattern_new_and_as_str() {
        let pattern = MatchitPattern::new("/users/{id}");
        assert_eq!(pattern.as_str(), "/users/{id}");
    }

    #[test]
    fn matchit_pattern_into_string() {
        let pattern = MatchitPattern::new("/users");
        let s: String = pattern.into();
        assert_eq!(s, "/users");
    }

    #[test]
    fn build_root_mounted_router_pattern_not_trimmed_to_empty() {
        let sub_router = Router::with_urls(vec![route("/inner")]);
        let routes = vec![Route::with_router("/", sub_router)];
        let trie = RouteTrie::build(&routes).unwrap();
        assert!(trie.at("/inner").is_some());
    }

    #[test]
    fn build_router_mount_slash_and_no_slash_variants_conflict_with_clear_error() {
        let router1 = Router::with_urls(vec![route("/foo")]);
        let router2 = Router::with_urls(vec![route("/bar")]);
        let routes = vec![
            Route::with_router("/admin", router1),
            Route::with_router("/admin/", router2),
        ];
        let err = RouteTrie::build(&routes).unwrap_err();
        assert!(err.to_string().contains("duplicate nested router"));
    }
}