1 use proc_macro2::Ident;
2 use std::cmp::Ordering;
3
4 #[derive(PartialEq, Eq)]
5 pub struct Path {
6 pub segments: Vec<Ident>,
7 }
8
9 impl PartialOrd for Path {
partial_cmp(&self, other: &Path) -> Option<Ordering>10 fn partial_cmp(&self, other: &Path) -> Option<Ordering> {
11 Some(self.cmp(other))
12 }
13 }
14
15 impl Ord for Path {
cmp(&self, other: &Path) -> Ordering16 fn cmp(&self, other: &Path) -> Ordering {
17 // Lexicographic ordering across path segments.
18 for (lhs, rhs) in self.segments.iter().zip(&other.segments) {
19 match cmp(&lhs.to_string(), &rhs.to_string()) {
20 Ordering::Equal => {}
21 non_eq => return non_eq,
22 }
23 }
24
25 self.segments.len().cmp(&other.segments.len())
26 }
27 }
28
29 // TODO: more intelligent comparison
30 // for example to handle numeric cases like E9 < E10.
cmp(lhs: &str, rhs: &str) -> Ordering31 fn cmp(lhs: &str, rhs: &str) -> Ordering {
32 // Sort `_` last.
33 match (lhs == "_", rhs == "_") {
34 (true, true) => return Ordering::Equal,
35 (true, false) => return Ordering::Greater,
36 (false, true) => return Ordering::Less,
37 (false, false) => {}
38 }
39
40 let lhs = lhs.to_ascii_lowercase();
41 let rhs = rhs.to_ascii_lowercase();
42
43 // For now: asciibetical ordering.
44 lhs.cmp(&rhs)
45 }
46