33//! These types are the public API exposed through the `--output-format json` flag. The [`Crate`]
44//! struct is the root of the JSON blob and all other items are contained within.
55
6+ use std:: collections:: { HashMap , HashSet } ;
67use std:: path:: PathBuf ;
78
8- use std:: collections:: HashMap ;
99use serde:: { Deserialize , Serialize } ;
1010
1111/// A `Crate` is the root of the emitted JSON blob. It contains all type/documentation information
@@ -64,7 +64,7 @@ pub struct Item {
6464 pub name : Option < String > ,
6565 /// The source location of this item (absent if it came from a macro expansion or inline
6666 /// assembly).
67- pub source : Option < Span > ,
67+ pub span : Option < Span > ,
6868 /// By default all documented items are public, but you can tell rustdoc to output private items
6969 /// so this field is needed to differentiate.
7070 pub visibility : Visibility ,
@@ -76,7 +76,7 @@ pub struct Item {
7676 /// Stringified versions of the attributes on this item (e.g. `"#[inline]"`)
7777 pub attrs : Vec < String > ,
7878 pub deprecation : Option < Deprecation > ,
79- pub kind : ItemKind ,
79+ # [ serde ( flatten ) ]
8080 pub inner : ItemEnum ,
8181}
8282
@@ -96,8 +96,8 @@ pub struct Deprecation {
9696 pub note : Option < String > ,
9797}
9898
99- #[ serde( rename_all = "snake_case" ) ]
10099#[ derive( Clone , Debug , Serialize , Deserialize , PartialEq ) ]
100+ #[ serde( rename_all = "snake_case" ) ]
101101pub enum Visibility {
102102 Public ,
103103 /// For the most part items are private by default. The exceptions are associated items of
@@ -112,17 +112,17 @@ pub enum Visibility {
112112 } ,
113113}
114114
115- #[ serde( rename_all = "snake_case" ) ]
116115#[ derive( Clone , Debug , Serialize , Deserialize , PartialEq ) ]
116+ #[ serde( rename_all = "snake_case" ) ]
117117pub enum GenericArgs {
118118 /// <'a, 32, B: Copy, C = u32>
119119 AngleBracketed { args : Vec < GenericArg > , bindings : Vec < TypeBinding > } ,
120120 /// Fn(A, B) -> C
121121 Parenthesized { inputs : Vec < Type > , output : Option < Type > } ,
122122}
123123
124- #[ serde( rename_all = "snake_case" ) ]
125124#[ derive( Clone , Debug , Serialize , Deserialize , PartialEq ) ]
125+ #[ serde( rename_all = "snake_case" ) ]
126126pub enum GenericArg {
127127 Lifetime ( String ) ,
128128 Type ( Type ) ,
@@ -144,8 +144,8 @@ pub struct TypeBinding {
144144 pub binding : TypeBindingKind ,
145145}
146146
147- #[ serde( rename_all = "snake_case" ) ]
148147#[ derive( Clone , Debug , Serialize , Deserialize , PartialEq ) ]
148+ #[ serde( rename_all = "snake_case" ) ]
149149pub enum TypeBindingKind {
150150 Equality ( Type ) ,
151151 Constraint ( Vec < GenericBound > ) ,
@@ -154,8 +154,8 @@ pub enum TypeBindingKind {
154154#[ derive( Clone , Debug , PartialEq , Eq , Hash , Serialize , Deserialize ) ]
155155pub struct Id ( pub String ) ;
156156
157- #[ serde( rename_all = "snake_case" ) ]
158157#[ derive( Clone , Debug , Serialize , Deserialize , PartialEq ) ]
158+ #[ serde( rename_all = "snake_case" ) ]
159159pub enum ItemKind {
160160 Module ,
161161 ExternCrate ,
@@ -184,48 +184,49 @@ pub enum ItemKind {
184184 Keyword ,
185185}
186186
187- #[ serde( untagged) ]
188187#[ derive( Clone , Debug , Serialize , Deserialize , PartialEq ) ]
188+ #[ serde( tag = "kind" , content = "inner" , rename_all = "snake_case" ) ]
189189pub enum ItemEnum {
190- ModuleItem ( Module ) ,
191- ExternCrateItem {
190+ Module ( Module ) ,
191+ ExternCrate {
192192 name : String ,
193193 rename : Option < String > ,
194194 } ,
195- ImportItem ( Import ) ,
195+ Import ( Import ) ,
196196
197- StructItem ( Struct ) ,
198- StructFieldItem ( Type ) ,
199- EnumItem ( Enum ) ,
200- VariantItem ( Variant ) ,
197+ Union ( Union ) ,
198+ Struct ( Struct ) ,
199+ StructField ( Type ) ,
200+ Enum ( Enum ) ,
201+ Variant ( Variant ) ,
201202
202- FunctionItem ( Function ) ,
203+ Function ( Function ) ,
203204
204- TraitItem ( Trait ) ,
205- TraitAliasItem ( TraitAlias ) ,
206- MethodItem ( Method ) ,
207- ImplItem ( Impl ) ,
205+ Trait ( Trait ) ,
206+ TraitAlias ( TraitAlias ) ,
207+ Method ( Method ) ,
208+ Impl ( Impl ) ,
208209
209- TypedefItem ( Typedef ) ,
210- OpaqueTyItem ( OpaqueTy ) ,
211- ConstantItem ( Constant ) ,
210+ Typedef ( Typedef ) ,
211+ OpaqueTy ( OpaqueTy ) ,
212+ Constant ( Constant ) ,
212213
213- StaticItem ( Static ) ,
214+ Static ( Static ) ,
214215
215216 /// `type`s from an extern block
216- ForeignTypeItem ,
217+ ForeignType ,
217218
218219 /// Declarative macro_rules! macro
219- MacroItem ( String ) ,
220- ProcMacroItem ( ProcMacro ) ,
220+ Macro ( String ) ,
221+ ProcMacro ( ProcMacro ) ,
221222
222- AssocConstItem {
223+ AssocConst {
223224 #[ serde( rename = "type" ) ]
224225 type_ : Type ,
225226 /// e.g. `const X: usize = 5;`
226227 default : Option < String > ,
227228 } ,
228- AssocTypeItem {
229+ AssocType {
229230 bounds : Vec < GenericBound > ,
230231 /// e.g. `type X = usize;`
231232 default : Option < Type > ,
@@ -238,6 +239,14 @@ pub struct Module {
238239 pub items : Vec < Id > ,
239240}
240241
242+ #[ derive( Clone , Debug , Serialize , Deserialize , PartialEq ) ]
243+ pub struct Union {
244+ pub generics : Generics ,
245+ pub fields_stripped : bool ,
246+ pub fields : Vec < Id > ,
247+ pub impls : Vec < Id > ,
248+ }
249+
241250#[ derive( Clone , Debug , Serialize , Deserialize , PartialEq ) ]
242251pub struct Struct {
243252 pub struct_type : StructType ,
@@ -255,37 +264,46 @@ pub struct Enum {
255264 pub impls : Vec < Id > ,
256265}
257266
267+ #[ derive( Clone , Debug , Serialize , Deserialize , PartialEq ) ]
258268#[ serde( rename_all = "snake_case" ) ]
259269#[ serde( tag = "variant_kind" , content = "variant_inner" ) ]
260- #[ derive( Clone , Debug , Serialize , Deserialize , PartialEq ) ]
261270pub enum Variant {
262271 Plain ,
263272 Tuple ( Vec < Type > ) ,
264273 Struct ( Vec < Id > ) ,
265274}
266275
267- #[ serde( rename_all = "snake_case" ) ]
268276#[ derive( Clone , Debug , Serialize , Deserialize , PartialEq ) ]
277+ #[ serde( rename_all = "snake_case" ) ]
269278pub enum StructType {
270279 Plain ,
271280 Tuple ,
272281 Unit ,
273- Union ,
282+ }
283+
284+ #[ non_exhaustive]
285+ #[ derive( Clone , Debug , Serialize , Deserialize , PartialEq , Eq , Hash ) ]
286+ #[ serde( rename_all = "snake_case" ) ]
287+ pub enum Qualifiers {
288+ Const ,
289+ Unsafe ,
290+ Async ,
274291}
275292
276293#[ derive( Clone , Debug , Serialize , Deserialize , PartialEq ) ]
277294pub struct Function {
278295 pub decl : FnDecl ,
279296 pub generics : Generics ,
280- pub header : String ,
297+ pub header : HashSet < Qualifiers > ,
281298 pub abi : String ,
282299}
283300
284301#[ derive( Clone , Debug , Serialize , Deserialize , PartialEq ) ]
285302pub struct Method {
286303 pub decl : FnDecl ,
287304 pub generics : Generics ,
288- pub header : String ,
305+ pub header : HashSet < Qualifiers > ,
306+ pub abi : String ,
289307 pub has_body : bool ,
290308}
291309
@@ -301,24 +319,24 @@ pub struct GenericParamDef {
301319 pub kind : GenericParamDefKind ,
302320}
303321
304- #[ serde( rename_all = "snake_case" ) ]
305322#[ derive( Clone , Debug , Serialize , Deserialize , PartialEq ) ]
323+ #[ serde( rename_all = "snake_case" ) ]
306324pub enum GenericParamDefKind {
307325 Lifetime ,
308326 Type { bounds : Vec < GenericBound > , default : Option < Type > } ,
309327 Const ( Type ) ,
310328}
311329
312- #[ serde( rename_all = "snake_case" ) ]
313330#[ derive( Clone , Debug , Serialize , Deserialize , PartialEq ) ]
331+ #[ serde( rename_all = "snake_case" ) ]
314332pub enum WherePredicate {
315333 BoundPredicate { ty : Type , bounds : Vec < GenericBound > } ,
316334 RegionPredicate { lifetime : String , bounds : Vec < GenericBound > } ,
317335 EqPredicate { lhs : Type , rhs : Type } ,
318336}
319337
320- #[ serde( rename_all = "snake_case" ) ]
321338#[ derive( Clone , Debug , Serialize , Deserialize , PartialEq ) ]
339+ #[ serde( rename_all = "snake_case" ) ]
322340pub enum GenericBound {
323341 TraitBound {
324342 #[ serde( rename = "trait" ) ]
@@ -330,17 +348,17 @@ pub enum GenericBound {
330348 Outlives ( String ) ,
331349}
332350
333- #[ serde( rename_all = "snake_case" ) ]
334351#[ derive( Clone , Debug , Serialize , Deserialize , PartialEq ) ]
352+ #[ serde( rename_all = "snake_case" ) ]
335353pub enum TraitBoundModifier {
336354 None ,
337355 Maybe ,
338356 MaybeConst ,
339357}
340358
359+ #[ derive( Clone , Debug , Serialize , Deserialize , PartialEq ) ]
341360#[ serde( rename_all = "snake_case" ) ]
342361#[ serde( tag = "kind" , content = "inner" ) ]
343- #[ derive( Clone , Debug , Serialize , Deserialize , PartialEq ) ]
344362pub enum Type {
345363 /// Structs, enums, and traits
346364 ResolvedPath {
@@ -395,9 +413,9 @@ pub enum Type {
395413
396414#[ derive( Clone , Debug , Serialize , Deserialize , PartialEq ) ]
397415pub struct FunctionPointer {
398- pub is_unsafe : bool ,
399- pub generic_params : Vec < GenericParamDef > ,
400416 pub decl : FnDecl ,
417+ pub generic_params : Vec < GenericParamDef > ,
418+ pub header : HashSet < Qualifiers > ,
401419 pub abi : String ,
402420}
403421
@@ -439,11 +457,11 @@ pub struct Impl {
439457 pub blanket_impl : Option < Type > ,
440458}
441459
442- #[ serde( rename_all = "snake_case" ) ]
443460#[ derive( Clone , Debug , Serialize , Deserialize , PartialEq ) ]
461+ #[ serde( rename_all = "snake_case" ) ]
444462pub struct Import {
445463 /// The full path being imported.
446- pub span : String ,
464+ pub source : String ,
447465 /// May be different from the last segment of `source` when renaming imports:
448466 /// `use source as name;`
449467 pub name : String ,
@@ -459,8 +477,8 @@ pub struct ProcMacro {
459477 pub helpers : Vec < String > ,
460478}
461479
462- #[ serde( rename_all = "snake_case" ) ]
463480#[ derive( Clone , Debug , Serialize , Deserialize , PartialEq ) ]
481+ #[ serde( rename_all = "snake_case" ) ]
464482pub enum MacroKind {
465483 /// A bang macro `foo!()`.
466484 Bang ,
@@ -490,3 +508,6 @@ pub struct Static {
490508 pub mutable : bool ,
491509 pub expr : String ,
492510}
511+
512+ #[ cfg( test) ]
513+ mod tests;
0 commit comments