1 | use syn::{ |
2 | visit_mut::{visit_file_mut, visit_item_mod_mut, VisitMut}, |
3 | File, Item, ItemMod, |
4 | }; |
5 | |
6 | pub(super) fn sort_semantically(file: &mut File) { |
7 | Visitor.visit_file_mut(file) |
8 | } |
9 | |
10 | struct Visitor; |
11 | |
12 | impl VisitMut for Visitor { |
13 | fn visit_file_mut(&mut self, file: &mut File) { |
14 | visit_items(&mut file.items); |
15 | visit_file_mut(self, node:file) |
16 | } |
17 | |
18 | fn visit_item_mod_mut(&mut self, item_mod: &mut ItemMod) { |
19 | if let Some((_, ref mut items: &mut Vec )) = item_mod.content { |
20 | visit_items(items); |
21 | } |
22 | visit_item_mod_mut(self, node:item_mod) |
23 | } |
24 | } |
25 | |
26 | fn visit_items(items: &mut [Item]) { |
27 | items.sort_by_key(|item: &Item| match item { |
28 | Item::Type(_) => 0, |
29 | Item::Struct(_) => 1, |
30 | Item::Const(_) => 2, |
31 | Item::Fn(_) => 3, |
32 | Item::Enum(_) => 4, |
33 | Item::Union(_) => 5, |
34 | Item::Static(_) => 6, |
35 | Item::Trait(_) => 7, |
36 | Item::TraitAlias(_) => 8, |
37 | Item::Impl(_) => 9, |
38 | Item::Mod(_) => 10, |
39 | Item::Use(_) => 11, |
40 | Item::Verbatim(_) => 12, |
41 | Item::ExternCrate(_) => 13, |
42 | Item::ForeignMod(_) => 14, |
43 | Item::Macro(_) => 15, |
44 | _ => 18, |
45 | }); |
46 | } |
47 | |