]> git.lizzy.rs Git - rust.git/blob - src/tools/jsondoclint/src/item_kind.rs
Rollup merge of #106244 - atouchet:readme3, r=workingjubilee
[rust.git] / src / tools / jsondoclint / src / item_kind.rs
1 use rustdoc_json_types::{Item, ItemEnum, ItemKind, ItemSummary};
2
3 /// A univeral way to represent an [`ItemEnum`] or [`ItemKind`]
4 #[derive(Debug, Clone, Copy)]
5 pub(crate) enum Kind {
6     Module,
7     ExternCrate,
8     Import,
9     Struct,
10     StructField,
11     Union,
12     Enum,
13     Variant,
14     Function,
15     Typedef,
16     OpaqueTy,
17     Constant,
18     Trait,
19     TraitAlias,
20     Impl,
21     Static,
22     ForeignType,
23     Macro,
24     ProcAttribute,
25     ProcDerive,
26     AssocConst,
27     AssocType,
28     Primitive,
29     Keyword,
30     // Not in ItemKind
31     ProcMacro,
32 }
33
34 impl Kind {
35     pub fn can_appear_in_mod(self) -> bool {
36         use Kind::*;
37         match self {
38             Module => true,
39             ExternCrate => true,
40             Import => true,
41             Union => true,
42             Struct => true,
43             Enum => true,
44             Function => true,
45             Trait => true,
46             TraitAlias => true,
47             Impl => true,
48             Typedef => true,
49             Constant => true,
50             Static => true,
51             Macro => true,
52             ProcMacro => true,
53             Primitive => true,
54             ForeignType => true,
55
56             // FIXME(adotinthevoid): I'm not sure if these are corrent
57             Keyword => false,
58             OpaqueTy => false,
59             ProcAttribute => false,
60             ProcDerive => false,
61
62             // Only in traits
63             AssocConst => false,
64             AssocType => false,
65
66             StructField => false, // Only in structs or variants
67             Variant => false,     // Only in enums
68         }
69     }
70
71     pub fn can_appear_in_import(self) -> bool {
72         match self {
73             Kind::Variant => true,
74             Kind::Import => false,
75             other => other.can_appear_in_mod(),
76         }
77     }
78
79     pub fn can_appear_in_glob_import(self) -> bool {
80         match self {
81             Kind::Module => true,
82             Kind::Enum => true,
83             _ => false,
84         }
85     }
86
87     pub fn can_appear_in_trait(self) -> bool {
88         match self {
89             Kind::AssocConst => true,
90             Kind::AssocType => true,
91             Kind::Function => true,
92
93             Kind::Module => false,
94             Kind::ExternCrate => false,
95             Kind::Import => false,
96             Kind::Struct => false,
97             Kind::StructField => false,
98             Kind::Union => false,
99             Kind::Enum => false,
100             Kind::Variant => false,
101             Kind::Typedef => false,
102             Kind::OpaqueTy => false,
103             Kind::Constant => false,
104             Kind::Trait => false,
105             Kind::TraitAlias => false,
106             Kind::Impl => false,
107             Kind::Static => false,
108             Kind::ForeignType => false,
109             Kind::Macro => false,
110             Kind::ProcAttribute => false,
111             Kind::ProcDerive => false,
112             Kind::Primitive => false,
113             Kind::Keyword => false,
114             Kind::ProcMacro => false,
115         }
116     }
117
118     pub fn is_struct_field(self) -> bool {
119         matches!(self, Kind::StructField)
120     }
121     pub fn is_module(self) -> bool {
122         matches!(self, Kind::Module)
123     }
124     pub fn is_impl(self) -> bool {
125         matches!(self, Kind::Impl)
126     }
127     pub fn is_variant(self) -> bool {
128         matches!(self, Kind::Variant)
129     }
130     pub fn is_trait_or_alias(self) -> bool {
131         matches!(self, Kind::Trait | Kind::TraitAlias)
132     }
133     pub fn is_type(self) -> bool {
134         matches!(self, Kind::Struct | Kind::Enum | Kind::Union | Kind::Typedef)
135     }
136
137     pub fn from_item(i: &Item) -> Self {
138         use Kind::*;
139         match i.inner {
140             ItemEnum::Module(_) => Module,
141             ItemEnum::Import(_) => Import,
142             ItemEnum::Union(_) => Union,
143             ItemEnum::Struct(_) => Struct,
144             ItemEnum::StructField(_) => StructField,
145             ItemEnum::Enum(_) => Enum,
146             ItemEnum::Variant(_) => Variant,
147             ItemEnum::Function(_) => Function,
148             ItemEnum::Trait(_) => Trait,
149             ItemEnum::TraitAlias(_) => TraitAlias,
150             ItemEnum::Impl(_) => Impl,
151             ItemEnum::Typedef(_) => Typedef,
152             ItemEnum::OpaqueTy(_) => OpaqueTy,
153             ItemEnum::Constant(_) => Constant,
154             ItemEnum::Static(_) => Static,
155             ItemEnum::Macro(_) => Macro,
156             ItemEnum::ProcMacro(_) => ProcMacro,
157             ItemEnum::Primitive(_) => Primitive,
158             ItemEnum::ForeignType => ForeignType,
159             ItemEnum::ExternCrate { .. } => ExternCrate,
160             ItemEnum::AssocConst { .. } => AssocConst,
161             ItemEnum::AssocType { .. } => AssocType,
162         }
163     }
164
165     pub fn from_summary(s: &ItemSummary) -> Self {
166         use Kind::*;
167         match s.kind {
168             ItemKind::AssocConst => AssocConst,
169             ItemKind::AssocType => AssocType,
170             ItemKind::Constant => Constant,
171             ItemKind::Enum => Enum,
172             ItemKind::ExternCrate => ExternCrate,
173             ItemKind::ForeignType => ForeignType,
174             ItemKind::Function => Function,
175             ItemKind::Impl => Impl,
176             ItemKind::Import => Import,
177             ItemKind::Keyword => Keyword,
178             ItemKind::Macro => Macro,
179             ItemKind::Module => Module,
180             ItemKind::OpaqueTy => OpaqueTy,
181             ItemKind::Primitive => Primitive,
182             ItemKind::ProcAttribute => ProcAttribute,
183             ItemKind::ProcDerive => ProcDerive,
184             ItemKind::Static => Static,
185             ItemKind::Struct => Struct,
186             ItemKind::StructField => StructField,
187             ItemKind::Trait => Trait,
188             ItemKind::TraitAlias => TraitAlias,
189             ItemKind::Typedef => Typedef,
190             ItemKind::Union => Union,
191             ItemKind::Variant => Variant,
192         }
193     }
194 }