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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
use std::collections::{BTreeMap, HashSet};

use crate::CharacterClass::{Ascii, InvalidChars, ValidChars};

#[derive(PartialEq, Eq, Clone, Default, Debug)]
pub struct CharSet {
    pub low_mask: u32,
    pub high_mask: u64,
    pub non_ascii: HashSet<char>,
}

impl CharSet {
    pub fn contains(&self, char: char) -> bool {
        let val = char as u32 - 1;

        if val > 127 {
            self.non_ascii.contains(&char)
        } else if val > 63 {
            let bit = 1 << (val - 64);
            self.high_mask & bit != 0
        } else {
            let bit = 1 << val;
            self.low_mask & bit != 0
        }
    }
}

#[derive(Debug)]
pub struct Params {
    pub map: BTreeMap<String, String>,
}

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

impl Params {
    pub fn new() -> Params {
        Params {
            map: BTreeMap::new(),
        }
    }

    pub fn insert(&mut self, key: String, value: String) {
        self.map.insert(key, value);
    }

    pub fn find(&self, key: &str) -> Option<&str> {
        self.map.get(key).map(|s| &s[..])
    }
}

#[derive(Clone, Debug)]
struct Thread {
    state: usize,
    captures: Vec<(usize, usize)>,
    capture_begin: Option<usize>,
}

impl Thread {
    pub fn new() -> Self {
        Self {
            state: 0,
            captures: Vec::new(),
            capture_begin: None,
        }
    }

    pub fn start_capture(&mut self, start: usize) {
        self.capture_begin = Some(start);
    }

    pub fn end_capture(&mut self, end: usize) {
        self.captures.push((self.capture_begin.unwrap(), end));
        self.capture_begin = None;
    }

    pub fn extract<'a>(&self, source: &'a str) -> Vec<&'a str> {
        self.captures
            .iter()
            .map(|&(start, end)| &source[start..end])
            .collect()
    }
}

#[derive(PartialEq, Eq, Clone, Debug)]
pub enum CharacterClass {
    Ascii(u64, u64, bool),
    ValidChars(CharSet),
    InvalidChars(CharSet),
}

impl CharacterClass {
    pub fn any() -> CharacterClass {
        Ascii(u64::MAX, u64::MAX, false)
    }

    pub fn valid_char(char: char) -> Self {
        let val = char as u32 - 1;

        if val > 127 {
            ValidChars(Self::char_to_set(char))
        } else if val > 63 {
            Ascii(1 << (val - 64), 0, false)
        } else {
            Ascii(0, 1 << val, false)
        }
    }

    pub fn invalid_char(char: char) -> Self {
        let val = char as u32 - 1;

        if val > 127 {
            InvalidChars(Self::char_to_set(char))
        } else if val > 63 {
            Ascii(u64::MAX ^ (1 << (val - 64)), u64::MAX, true)
        } else {
            Ascii(u64::MAX, u64::MAX ^ (1 << val), true)
        }
    }

    pub fn char_to_set(char: char) -> CharSet {
        let mut set = CharSet::default();
        set.non_ascii.insert(char);
        set
    }

    pub fn matches(&self, char: char) -> bool {
        match *self {
            ValidChars(ref valid) => valid.contains(char),
            InvalidChars(ref valid) => !valid.contains(char),
            Ascii(high, low, unicode) => {
                let val = char as u32 - 1;
                if val > 127 {
                    unicode
                } else if val > 63 {
                    high & (1 << (val - 64)) != 0
                } else {
                    low & (1 << val) != 0
                }
            }
        }
    }
}

#[derive(Debug)]
pub struct Metadata {
    pub statics: u32,
    pub dynamics: u32,
    pub wildcards: u32,
    pub param_names: Vec<String>,
}

impl Metadata {
    pub fn new() -> Metadata {
        Metadata {
            statics: 0,
            dynamics: 0,
            wildcards: 0,
            param_names: Vec::new(),
        }
    }
}

#[derive(Debug)]
pub struct State<T> {
    pub index: usize,
    pub chars: CharacterClass,
    pub next_states: Vec<usize>,
    pub acceptance: bool,
    pub start_capture: bool,
    pub end_capture: bool,
    pub metadata: Option<T>,
}

#[derive(Debug)]
pub struct NFA<T> {
    pub states: Vec<State<T>>,
    pub start_capture: Vec<bool>,
    pub end_capture: Vec<bool>,
    pub acceptance: Vec<bool>,
}

impl<T> NFA<T> {
    pub fn put(&mut self, index: usize, chars: CharacterClass) -> usize {
        {
            // Check if the state already exists
            // If it does, return just the index of it
            // So we don't have to create a new state
            let state = self.get(index);

            for &index in &state.next_states {
                let state = self.get(index);

                if state.chars == chars {
                    return index;
                }
            }
        }

        // If the state doesn't exist, we create a new one
        // And add it to the next states of the current state
        let state = self.new_state(chars);
        self.get_mut(index).next_states.push(state);

        state
    }

    pub fn get_mut(&mut self, index: usize) -> &mut State<T> {
        &mut self.states[index]
    }

    pub fn new_state(&mut self, chars: CharacterClass) -> usize {
        // The index of the new state is the length of the states vector
        // Example:
        // [0: 'a', 1: 'b', 2: 'c']
        // The index is 3, so the new state will be at index 3
        let index = self.states.len();
        let state = State::new(index, chars);
        self.states.push(state);

        self.acceptance.push(false);
        self.start_capture.push(false);
        self.end_capture.push(false);

        index
    }

    pub fn get(&self, index: usize) -> &State<T> {
        &self.states[index]
    }

    pub fn acceptance(&mut self, index: usize) {
        // Set the acceptance of the state at the given index to true
        self.get_mut(index).acceptance = true;
        self.acceptance[index] = true;
    }

    pub fn metadata(&mut self, index: usize, metadata: T) {
        self.get_mut(index).metadata = Some(metadata);
    }

    pub fn start_capture(&mut self, index: usize) {
        self.get_mut(index).start_capture = true;
        self.start_capture[index] = true;
    }

    pub fn end_capture(&mut self, index: usize) {
        self.get_mut(index).end_capture = true;
        self.end_capture[index] = true;
    }

    pub fn put_state(&mut self, index: usize, child: usize) {
        if !self.get(index).next_states.contains(&child) {
            self.get_mut(index).next_states.push(child);
        }
    }
}

impl<T> State<T> {
    pub fn new(index: usize, chars: CharacterClass) -> Self {
        Self {
            index,
            chars,
            next_states: Vec::new(),
            acceptance: false,
            start_capture: false,
            end_capture: false,
            metadata: None,
        }
    }
}

#[derive(Debug)]
pub struct Match<'a> {
    state: usize,
    captures: Vec<&'a str>,
}

impl<'a> Match<'a> {
    pub fn new(state: usize, captures: Vec<&'a str>) -> Self {
        Self { state, captures }
    }
}

#[derive(Debug)]
pub struct RouterMatch<T> {
    handler: T,
    params: Params,
}

impl<T> RouterMatch<T> {
    pub fn new(handler: T, params: Params) -> Self {
        Self { handler, params }
    }
}

impl<T> NFA<T> {
    pub fn new() -> NFA<T> {
        let root = State::new(0, CharacterClass::any());

        NFA {
            states: vec![root],
            start_capture: vec![false],
            end_capture: vec![false],
            acceptance: vec![false],
        }
    }

    pub fn process<'a>(&self, string: &'a str) -> Result<Match<'a>, String> {
        let mut threads = vec![Thread::new()];

        for (i, char) in string.char_indices() {
            let next_threads = self.process_char(threads, char, i);

            if next_threads.is_empty() {
                return Err(format!("No match found for {}", string));
            }

            threads = next_threads;
        }

        let mut returned = threads
            .into_iter()
            .filter(|thread| self.get(thread.state).acceptance);

        let thread = returned.next();

        match thread {
            None => Err(format!("No match found for {}", string)),
            Some(mut thread) => {
                if thread.capture_begin.is_some() {
                    thread.end_capture(string.len());
                }

                let state = self.get(thread.state);
                Ok(Match::new(state.index, thread.extract(string)))
            }
        }
    }

    pub fn process_char(&self, threads: Vec<Thread>, char: char, pos: usize) -> Vec<Thread> {
        let mut returned = Vec::with_capacity(threads.len());

        for mut thread in threads {
            let current_state = self.get(thread.state);

            let mut count = 0;
            let mut found_state = 0;

            for &index in &current_state.next_states {
                let state = &self.get(index);

                if state.chars.matches(char) {
                    count += 1;
                    found_state = index;
                }
            }

            if count == 1 {
                thread.state = found_state;
                capture(self, &mut thread, current_state.index, found_state, pos);
                returned.push(thread);
                continue;
            }

            for &index in &current_state.next_states {
                let state = &self.get(index);

                if state.chars.matches(char) {
                    let mut thread = fork_thread(&thread, state);
                    capture(self, &mut thread, current_state.index, index, pos);
                    returned.push(thread);
                }
            }
        }

        returned
    }
}

fn fork_thread<T>(thread: &Thread, state: &State<T>) -> Thread {
    let mut new_thread = thread.clone();
    new_thread.state = state.index;
    new_thread
}

fn capture<T>(
    nfa: &NFA<T>,
    thread: &mut Thread,
    current_state: usize,
    next_state: usize,
    pos: usize,
) {
    if thread.capture_begin.is_none() && nfa.start_capture[next_state] {
        thread.start_capture(pos);
    }

    if thread.capture_begin.is_some()
        && nfa.end_capture[current_state]
        && next_state > current_state
    {
        thread.end_capture(pos);
    }
}

#[derive(Debug)]
pub struct Router<T> {
    pub nfa: NFA<Metadata>,
    pub handlers: BTreeMap<usize, T>,
}

fn segments(route: &str) -> Vec<(Option<char>, &str)> {
    let predicate = |c| c == '.' || c == '/';

    let mut segments = vec![];
    let mut segment_start = 0;

    while segment_start < route.len() {
        let segment_end = route[segment_start + 1..]
            .find(predicate)
            .map(|i| i + segment_start + 1)
            .unwrap_or_else(|| route.len());
        let potential_sep = route.chars().nth(segment_start);
        let sep_and_segment = match potential_sep {
            Some(sep) if predicate(sep) => (Some(sep), &route[segment_start + 1..segment_end]),
            _ => (None, &route[segment_start..segment_end]),
        };

        segments.push(sep_and_segment);
        segment_start = segment_end;
    }

    segments
}

fn first_byte(s: &str) -> u8 {
    s.as_bytes()[0]
}

impl<T> Router<T> {
    pub fn new() -> Router<T> {
        Router {
            nfa: NFA::new(),
            handlers: BTreeMap::new(),
        }
    }

    pub fn recognize(&self, mut path: &str) -> Result<RouterMatch<&T>, String> {
        if first_byte(path) == b'/' {
            path = &path[1..];
        }

        let nfa = &self.nfa;
        let result = nfa.process(path);

        return result.map(|m| {
            let mut map = Params::new();
            let state = &nfa.get(m.state);
            let metadata = state.metadata.as_ref().unwrap();
            let param_names = metadata.param_names.clone();

            for (i, capture) in m.captures.iter().enumerate() {
                if !param_names[i].is_empty() {
                    map.insert(param_names[i].to_string(), capture.to_string());
                }
            }

            let handler = self.handlers.get(&m.state).unwrap();
            RouterMatch::new(handler, map)
        });
    }

    pub fn add(&mut self, mut route: &str, destiny: T) {
        if route.is_empty() {
            return;
        }

        // Remove leading slash if exists
        if first_byte(route) == b'/' {
            route = &route[1..];
        }

        let nfa = &mut self.nfa;
        let mut state = 0;
        let mut metadata = Metadata::new();

        for (separator, segment) in segments(route) {
            // If we have a separator,
            // we need to add a transition to the current state
            if let Some(separator) = separator {
                state = nfa.put(state, CharacterClass::valid_char(separator));
            }

            if segment.is_empty() {
                continue;
            }

            match first_byte(segment) {
                b':' => {
                    state = process_star_state(nfa, state);
                    metadata.dynamics += 1;
                    metadata.param_names.push(
                        // Add the param key without ':'
                        segment[1..].to_string(),
                    );
                }
                b'*' => {
                    state = process_star_state(nfa, state);
                    metadata.wildcards += 1;
                    metadata.param_names.push(
                        // Add the param key without '*'
                        segment[1..].to_string(),
                    );
                }
                _ => {
                    state = process_static_segment(segment, nfa, state);
                    metadata.statics += 1;
                }
            }
        }

        // Mark the state as an acceptance state
        nfa.acceptance(state);

        // Add the metadata to the state
        nfa.metadata(state, metadata);

        // Add the handler to the handlers map
        self.handlers.insert(state, destiny);
    }
}

fn process_star_state<T>(nfa: &mut NFA<T>, mut state: usize) -> usize {
    state = nfa.put(state, CharacterClass::invalid_char('/'));
    nfa.put_state(state, state);
    nfa.start_capture(state);
    nfa.end_capture(state);

    state
}

fn process_static_segment<T>(segment: &str, nfa: &mut NFA<T>, mut state: usize) -> usize {
    // When we are processing a static segment
    // we just need to add a transition for each character
    // to our current state
    for char in segment.chars() {
        state = nfa.put(state, CharacterClass::valid_char(char));
    }

    state
}

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

    #[test]
    fn test_segments() {
        let route = "users/:id";
        let expected = vec![(None, "users"), (Some('/'), ":id")];
        assert_eq!(segments(route), expected);

        let route = "/users/:id/posts";
        let expected = vec![
            (Some('/'), "users"),
            (Some('/'), ":id"),
            (Some('/'), "posts"),
        ];
        assert_eq!(segments(route), expected);

        let route = "/users/:id/posts/:post_id";
        let expected = vec![
            (Some('/'), "users"),
            (Some('/'), ":id"),
            (Some('/'), "posts"),
            (Some('/'), ":post_id"),
        ];
        assert_eq!(segments(route), expected);

        let route = "/users/:id/posts/:post_id/comments";
        let expected = vec![
            (Some('/'), "users"),
            (Some('/'), ":id"),
            (Some('/'), "posts"),
            (Some('/'), ":post_id"),
            (Some('/'), "comments"),
        ];
        assert_eq!(segments(route), expected);
    }

    #[test]
    fn test_add_static_routes() {
        let mut router = Router::new();

        router.add("users", "users");

        let nfa = &router.nfa;
        let handlers = &router.handlers;

        assert_eq!(
            nfa.states.len(),
            6 // One state for each character in "users" + 1 for the root
        );
        assert_eq!(handlers.len(), 1);

        let handler = handlers
            .get(&5) // The last state of the NFA
            .unwrap();

        assert_eq!(*handler, "users");
    }

    #[test]
    fn test_add_routes_with_star_wildcards() {
        let mut router = Router::new();

        router.add("users/*-profile", "users-wildcard");

        let nfa = router.nfa;
        let handlers = router.handlers;

        assert_eq!(handlers.len(), 1);
        assert_eq!(
            nfa.states.len(),
            8 // One state for each character in "users" + 1 for the root
        );
    }

    #[test]
    fn test_add_routes_with_colon_wildcards() {
        let mut router = Router::new();

        router.add("user/:id", "users-wildcard");

        let nfa = router.nfa;
        let handlers = router.handlers;

        assert_eq!(handlers.len(), 1);
        assert_eq!(nfa.states.len(), 7);

        let metadata = &nfa.states[6]; // Last state
        let metadata = metadata.metadata.as_ref().unwrap();
        let params = &metadata.param_names;

        assert_eq!(*params, vec!["id"]);
    }

    #[test]
    fn test_route_recognize_static_route() {
        let mut router = Router::new();

        router.add("/users", "users");

        let m = router.recognize("/users").unwrap();

        assert_eq!(*m.handler, "users");
        assert_eq!(m.params, Params::new());
    }

    #[test]
    fn test_route_recognize_colon_wildcard() {
        let mut router = Router::new();

        router.add("/user/:id", "user");

        let m = router.recognize("/user/1").unwrap();

        assert_eq!(*m.handler, "user");
        assert_eq!(m.params.find("id"), Some("1"));
    }

    #[test]
    fn test_route_recognize_colon_wildcard_multiple_params() {
        let mut router = Router::new();

        router.add("/user/:id/posts/:post_id", "user-post");

        let m = router.recognize("/user/9/posts/10").unwrap();

        assert_eq!(*m.handler, "user-post");
        assert_eq!(m.params.find("id"), Some("9"));
        assert_eq!(m.params.find("post_id"), Some("10"));
    }

    #[test]
    fn test_route_recognize_colon_wildcard_fail() {
        let mut router = Router::new();

        router.add("/user/:id", "user");

        let m = router.recognize("/user");

        assert!(m.is_err());
    }

    #[test]
    fn test_route_recognize_star_wildcard() {
        let mut router = Router::new();

        router.add("/fs/*path", "fs");

        let m = router.recognize("/fs/random-file-path").unwrap();

        assert_eq!(*m.handler, "fs");
        assert_eq!(m.params.find("path"), Some("random-file-path"));
    }
}

fn main() {}