@@ -4,7 +4,7 @@ use std::fmt;
44
55use rustc_hir:: def:: { CtorOf , DefKind , MacroKinds } ;
66use rustc_span:: hygiene:: MacroKind ;
7- use serde:: { Serialize , Serializer } ;
7+ use serde:: { Deserialize , Deserializer , Serialize , Serializer , de } ;
88
99use crate :: clean;
1010
@@ -68,6 +68,52 @@ impl Serialize for ItemType {
6868 }
6969}
7070
71+ impl < ' de > Deserialize < ' de > for ItemType {
72+ fn deserialize < D > ( deserializer : D ) -> Result < ItemType , D :: Error >
73+ where
74+ D : Deserializer < ' de > ,
75+ {
76+ struct ItemTypeVisitor ;
77+ impl < ' de > de:: Visitor < ' de > for ItemTypeVisitor {
78+ type Value = ItemType ;
79+ fn expecting ( & self , formatter : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
80+ write ! ( formatter, "an integer between 0 and 25" )
81+ }
82+ fn visit_u64 < E : de:: Error > ( self , v : u64 ) -> Result < ItemType , E > {
83+ Ok ( match v {
84+ 0 => ItemType :: Keyword ,
85+ 1 => ItemType :: Primitive ,
86+ 2 => ItemType :: Module ,
87+ 3 => ItemType :: ExternCrate ,
88+ 4 => ItemType :: Import ,
89+ 5 => ItemType :: Struct ,
90+ 6 => ItemType :: Enum ,
91+ 7 => ItemType :: Function ,
92+ 8 => ItemType :: TypeAlias ,
93+ 9 => ItemType :: Static ,
94+ 10 => ItemType :: Trait ,
95+ 11 => ItemType :: Impl ,
96+ 12 => ItemType :: TyMethod ,
97+ 13 => ItemType :: Method ,
98+ 14 => ItemType :: StructField ,
99+ 15 => ItemType :: Variant ,
100+ 16 => ItemType :: Macro ,
101+ 17 => ItemType :: AssocType ,
102+ 18 => ItemType :: Constant ,
103+ 19 => ItemType :: AssocConst ,
104+ 20 => ItemType :: Union ,
105+ 21 => ItemType :: ForeignType ,
106+ 23 => ItemType :: ProcAttribute ,
107+ 24 => ItemType :: ProcDerive ,
108+ 25 => ItemType :: TraitAlias ,
109+ _ => return Err ( E :: missing_field ( "unknown number" ) ) ,
110+ } )
111+ }
112+ }
113+ deserializer. deserialize_any ( ItemTypeVisitor )
114+ }
115+ }
116+
71117impl < ' a > From < & ' a clean:: Item > for ItemType {
72118 fn from ( item : & ' a clean:: Item ) -> ItemType {
73119 let kind = match & item. kind {
@@ -198,6 +244,10 @@ impl ItemType {
198244 pub ( crate ) fn is_adt ( & self ) -> bool {
199245 matches ! ( self , ItemType :: Struct | ItemType :: Union | ItemType :: Enum )
200246 }
247+ /// Keep this the same as isFnLikeTy in search.js
248+ pub ( crate ) fn is_fn_like ( & self ) -> bool {
249+ matches ! ( self , ItemType :: Function | ItemType :: Method | ItemType :: TyMethod )
250+ }
201251}
202252
203253impl fmt:: Display for ItemType {
0 commit comments