routefinder/
segment.rs

1use smartstring::alias::String as SmartString;
2/// the internal representation of a parsed component of a route
3///
4/// as an example, `/hello/:planet/*` would be represented as the
5/// following sequence `[Exact("hello"), Slash, Param("planet"),
6/// Slash, Wildcard]`
7#[derive(Debug, PartialEq, Eq, Clone)]
8pub enum Segment {
9    /// represented by a / in the route spec and matching one /
10    Slash,
11    /// represented by a . in the route spec and matching one . in the path
12    Dot,
13    /// represented by any free text in the route spec, this matches
14    /// exactly that text
15    Exact(SmartString),
16    /// represented by :name, where name is how the capture will be
17    /// available in [`Captures`][crate::Captures]. Param captures up to the next slash
18    /// or dot, whichever is next in the spec.
19    Param(SmartString),
20    /// represented by * in the spec, this will capture everything up
21    /// to the end of the path. a wildcard will also match nothing
22    /// (similar to the regex `(.*)$`). There can only be one wildcard
23    /// per route spec
24    Wildcard,
25}
26
27impl PartialOrd for Segment {
28    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
29        Some(self.cmp(other))
30    }
31}
32
33impl Ord for Segment {
34    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
35        use std::cmp::Ordering::*;
36        use Segment::*;
37        match (self, other) {
38            (Exact(_), Exact(_))
39            | (Slash, Slash)
40            | (Dot, Slash)
41            | (Slash, Dot)
42            | (Dot, Dot)
43            | (Param(_), Param(_))
44            | (Wildcard, Wildcard) => Equal,
45
46            (Dot, _) => Greater,
47            (Exact(_), _) => Greater,
48            (Param(_), Exact(_)) => Less,
49            (Param(_), _) => Greater,
50            (Wildcard, Exact(_)) | (Wildcard, Param(_)) => Less,
51            (Wildcard, _) => Greater,
52            _ => Less,
53        }
54    }
55}