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
use smartstring::alias::String as SmartString;
/// the internal representation of a parsed component of a route
///
/// as an example, `/hello/:planet/*` would be represented as the
/// following sequence `[Exact("hello"), Slash, Param("planet"),
/// Slash, Wildcard]`
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum Segment {
    /// represented by a / in the route spec and matching one /
    Slash,
    /// represented by a . in the route spec and matching one . in the path
    Dot,
    /// represented by any free text in the route spec, this matches
    /// exactly that text
    Exact(SmartString),
    /// represented by :name, where name is how the capture will be
    /// available in [`Captures`][crate::Captures]. Param captures up to the next slash
    /// or dot, whichever is next in the spec.
    Param(SmartString),
    /// represented by * in the spec, this will capture everything up
    /// to the end of the path. a wildcard will also match nothing
    /// (similar to the regex `(.*)$`). There can only be one wildcard
    /// per route spec
    Wildcard,
}

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

impl Ord for Segment {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        use std::cmp::Ordering::*;
        use Segment::*;
        match (self, other) {
            (Exact(_), Exact(_))
            | (Slash, Slash)
            | (Dot, Slash)
            | (Slash, Dot)
            | (Dot, Dot)
            | (Param(_), Param(_))
            | (Wildcard, Wildcard) => Equal,

            (Dot, _) => Greater,
            (Exact(_), _) => Greater,
            (Param(_), Exact(_)) => Less,
            (Param(_), _) => Greater,
            (Wildcard, Exact(_)) | (Wildcard, Param(_)) => Less,
            (Wildcard, _) => Greater,
            _ => Less,
        }
    }
}