roogle_index/
types.rs

1use std::collections::HashMap;
2use std::rc::Rc;
3
4use serde::Deserialize;
5
6type Symbol = String;
7
8#[non_exhaustive]
9#[derive(Deserialize, Clone, Debug)]
10pub struct Index {
11    #[serde(flatten)]
12    pub crates: HashMap<String, CrateData>,
13}
14
15// Lines below are copied & adjusted from librustdoc.
16
17#[derive(Deserialize, Clone, Debug)]
18pub struct CrateData {
19    pub items: Vec<Rc<IndexItem>>,
20}
21
22/// Struct representing one entry in the JS search index. These are all emitted
23/// by hand to a large JS file at the end of cache-creation.
24#[derive(Deserialize, Clone, Debug)]
25pub struct IndexItem {
26    pub name: String,
27    pub kind: Box<ItemKind>,
28    pub path: String,
29    pub desc: String,
30}
31
32/// Anything with a source location and set of attributes and, optionally, a
33/// name. That is, anything that can be documented. This doesn't correspond
34/// directly to the AST's concept of an item; it's a strict superset.
35#[derive(Deserialize, Clone, Debug)]
36pub struct Item {
37    /// The name of this item.
38    /// Optional because not every item has a name, e.g. impls.
39    pub name: Option<Symbol>,
40    pub visibility: Visibility,
41    /// Information about this item that is specific to what kind of item it is.
42    /// E.g., struct vs enum vs function.
43    pub kind: Box<ItemKind>,
44}
45
46#[derive(Deserialize, Clone, Debug)]
47pub enum ItemKind {
48    ExternCrateItem {
49        /// The crate's name, *not* the name it's imported as.
50        #[serde(skip)]
51        src: Option<Symbol>,
52    },
53    ImportItem(Import),
54    StructItem(Struct),
55    UnionItem(Union),
56    EnumItem(Enum),
57    FunctionItem(Function),
58    ModuleItem(Module),
59    TypedefItem(Typedef, bool /* is associated type */),
60    OpaqueTyItem(OpaqueTy),
61    StaticItem(Static),
62    ConstantItem(Constant),
63    TraitItem(Trait),
64    TraitAliasItem(TraitAlias),
65    ImplItem(Impl),
66    /// A method signature only. Used for required methods in traits (ie,
67    /// non-default-methods).
68    TyMethodItem(Function),
69    /// A method with a body.
70    // HACK(hkamtsumoto): `The second field was originally `rustc_hir::Defaultness`, which is hard
71    // to implement `Deserialize`. As a workaround, we skip deserializing it and use a fake unit
72    // type instead.
73    MethodItem(Function, #[serde(skip)] Option<()>),
74    StructFieldItem(Type),
75    VariantItem(Variant),
76    /// `fn`s from an extern block
77    ForeignFunctionItem(Function),
78    /// `static`s from an extern block
79    ForeignStaticItem(Static),
80    /// `type`s from an extern block
81    ForeignTypeItem,
82    MacroItem(Macro),
83    PrimitiveItem(PrimitiveType),
84    AssocConstItem(Type, Option<String>),
85    /// An associated item in a trait or trait impl.
86    ///
87    /// The bounds may be non-empty if there is a `where` clause.
88    /// The `Option<Type>` is the default concrete type (e.g. `trait Trait { type Target = usize; }`)
89    AssocTypeItem(Vec<GenericBound>, Option<Type>),
90    /// An item that has been stripped by a rustdoc pass
91    StrippedItem(Box<ItemKind>),
92    KeywordItem(Symbol),
93}
94
95#[derive(Deserialize, Clone, Debug)]
96pub struct Module {
97    pub items: Vec<Item>,
98}
99
100#[derive(Deserialize, Clone, PartialEq, Eq, Debug, Hash)]
101pub enum GenericBound {
102    TraitBound(PolyTrait, #[serde(skip)] ()),
103    Outlives(Lifetime),
104}
105
106#[derive(Deserialize, Clone, PartialEq, Eq, Debug, Hash)]
107pub struct Lifetime(pub Symbol);
108
109#[derive(Deserialize, Clone, Debug)]
110pub enum WherePredicate {
111    BoundPredicate {
112        ty: Type,
113        bounds: Vec<GenericBound>,
114        bound_params: Vec<Lifetime>,
115    },
116    RegionPredicate {
117        lifetime: Lifetime,
118        bounds: Vec<GenericBound>,
119    },
120    EqPredicate {
121        lhs: Type,
122        rhs: Type,
123    },
124}
125
126#[derive(Deserialize, Clone, PartialEq, Eq, Debug, Hash)]
127pub enum GenericParamDefKind {
128    Lifetime,
129    Type {
130        bounds: Vec<GenericBound>,
131        default: Option<Type>,
132    },
133    Const {
134        ty: Type,
135        default: Option<String>,
136    },
137}
138
139#[derive(Deserialize, Clone, PartialEq, Eq, Debug, Hash)]
140pub struct GenericParamDef {
141    pub name: Symbol,
142    pub kind: GenericParamDefKind,
143}
144
145// maybe use a Generic enum and use Vec<Generic>?
146#[derive(Deserialize, Clone, Debug, Default)]
147pub struct Generics {
148    pub params: Vec<GenericParamDef>,
149    pub where_predicates: Vec<WherePredicate>,
150}
151
152#[derive(Deserialize, Clone, Debug)]
153pub struct Function {
154    pub decl: FnDecl,
155    pub generics: Generics,
156}
157
158#[derive(Deserialize, Clone, PartialEq, Eq, Debug, Hash)]
159pub struct FnDecl {
160    pub inputs: Arguments,
161    pub output: FnRetTy,
162    pub c_variadic: bool,
163}
164
165#[derive(Deserialize, Clone, PartialEq, Eq, Debug, Hash)]
166pub struct Arguments {
167    pub values: Vec<Argument>,
168}
169
170#[derive(Deserialize, Clone, PartialEq, Eq, Debug, Hash)]
171pub struct Argument {
172    pub type_: Type,
173    pub name: Symbol,
174}
175
176#[derive(Deserialize, Clone, PartialEq, Eq, Debug, Hash)]
177pub enum Mutability {
178    Mut,
179    Not,
180}
181
182#[derive(Deserialize, Clone, PartialEq, Debug)]
183pub enum SelfTy {
184    SelfValue,
185    SelfBorrowed(Option<Lifetime>, Mutability),
186    SelfExplicit(Type),
187}
188
189#[derive(Deserialize, Clone, PartialEq, Eq, Debug, Hash)]
190pub enum FnRetTy {
191    Return(Type),
192    DefaultReturn,
193}
194
195#[derive(Deserialize, Clone, Debug)]
196pub struct Trait {
197    pub items: Vec<Item>,
198    pub generics: Generics,
199    pub bounds: Vec<GenericBound>,
200    pub is_auto: bool,
201}
202
203#[derive(Deserialize, Clone, Debug)]
204pub struct TraitAlias {
205    pub generics: Generics,
206    pub bounds: Vec<GenericBound>,
207}
208
209/// A trait reference, which may have higher ranked lifetimes.
210#[derive(Deserialize, Clone, PartialEq, Eq, Debug, Hash)]
211pub struct PolyTrait {
212    pub trait_: Type,
213    pub generic_params: Vec<GenericParamDef>,
214}
215
216/// A representation of a type suitable for hyperlinking purposes. Ideally, one can get the original
217/// type out of the AST/`TyCtxt` given one of these, if more information is needed. Most
218/// importantly, it does not preserve mutability or boxes.
219#[derive(Deserialize, Clone, PartialEq, Eq, Debug, Hash)]
220pub enum Type {
221    /// Structs/enums/traits (most that would be an `hir::TyKind::Path`).
222    ResolvedPath {
223        path: Path,
224        /// `true` if is a `T::Name` path for associated types.
225        is_generic: bool,
226    },
227    /// `dyn for<'a> Trait<'a> + Send + 'static`
228    DynTrait(Vec<PolyTrait>, Option<Lifetime>),
229    /// For parameterized types, so the consumer of the JSON don't go
230    /// looking for types which don't exist anywhere.
231    Generic(Symbol),
232    /// Primitives are the fixed-size numeric types (plus int/usize/float), char,
233    /// arrays, slices, and tuples.
234    Primitive(PrimitiveType),
235    /// `extern "ABI" fn`
236    BareFunction(Box<BareFunctionDecl>),
237    Tuple(Vec<Type>),
238    Slice(Box<Type>),
239    /// The `String` field is about the size or the constant representing the array's length.
240    Array(Box<Type>, String),
241    Never,
242    RawPointer(Mutability, Box<Type>),
243    BorrowedRef {
244        lifetime: Option<Lifetime>,
245        mutability: Mutability,
246        type_: Box<Type>,
247    },
248
249    // `<Type as Trait>::Name`
250    QPath {
251        name: Symbol,
252        self_type: Box<Type>,
253        trait_: Box<Type>,
254    },
255
256    // `_`
257    Infer,
258
259    // `impl TraitA + TraitB + ...`
260    ImplTrait(Vec<GenericBound>),
261}
262
263#[derive(Deserialize, Clone, PartialEq, Eq, Hash, Copy, Debug)]
264/// N.B. this has to be different from `hir::PrimTy` because it also includes types that aren't
265/// paths, like `Unit`.
266pub enum PrimitiveType {
267    Isize,
268    I8,
269    I16,
270    I32,
271    I64,
272    I128,
273    Usize,
274    U8,
275    U16,
276    U32,
277    U64,
278    U128,
279    F32,
280    F64,
281    Char,
282    Bool,
283    Str,
284    Slice,
285    Array,
286    Tuple,
287    Unit,
288    RawPointer,
289    Reference,
290    Fn,
291    Never,
292}
293
294#[derive(Deserialize, Copy, Clone, Debug)]
295pub enum Visibility {
296    /// `pub`
297    Public,
298    /// Visibility inherited from parent.
299    ///
300    /// For example, this is the visibility of private items and of enum variants.
301    Inherited,
302    /// `pub(crate)`, `pub(super)`, or `pub(in path::to::somewhere)`
303    Restricted(#[serde(skip)] ()),
304}
305
306#[derive(Deserialize, Clone, Debug)]
307pub struct Struct {
308    pub generics: Generics,
309    pub fields: Vec<Item>,
310    pub fields_stripped: bool,
311}
312
313#[derive(Deserialize, Clone, Debug)]
314pub struct Union {
315    pub generics: Generics,
316    pub fields: Vec<Item>,
317    pub fields_stripped: bool,
318}
319
320/// This is a more limited form of the standard Struct, different in that
321/// it lacks the things most items have (name, id, parameterization). Found
322/// only as a variant in an enum.
323#[derive(Deserialize, Clone, Debug)]
324pub struct VariantStruct {
325    pub fields: Vec<Item>,
326    pub fields_stripped: bool,
327}
328
329#[derive(Deserialize, Clone, Debug)]
330pub struct Enum {
331    #[serde(skip)]
332    pub variants: Vec<Item>,
333    pub generics: Generics,
334    pub variants_stripped: bool,
335}
336
337#[derive(Deserialize, Clone, Debug)]
338pub enum Variant {
339    CLike,
340    Tuple(Vec<Type>),
341    Struct(VariantStruct),
342}
343
344#[derive(Deserialize, Clone, PartialEq, Eq, Debug, Hash)]
345pub struct Path {
346    pub global: bool,
347    pub segments: Vec<PathSegment>,
348}
349
350#[derive(Deserialize, Clone, PartialEq, Eq, Debug, Hash)]
351pub enum GenericArg {
352    Lifetime(Lifetime),
353    Type(Type),
354    Const(Constant),
355    Infer,
356}
357
358#[derive(Deserialize, Clone, PartialEq, Eq, Debug, Hash)]
359pub enum GenericArgs {
360    AngleBracketed {
361        args: Vec<GenericArg>,
362        bindings: Vec<TypeBinding>,
363    },
364    Parenthesized {
365        inputs: Vec<Type>,
366        output: Option<Type>,
367    },
368}
369
370#[derive(Deserialize, Clone, PartialEq, Eq, Debug, Hash)]
371pub struct PathSegment {
372    pub name: Symbol,
373    pub args: GenericArgs,
374}
375
376#[derive(Deserialize, Clone, Debug)]
377pub struct Typedef {
378    pub type_: Type,
379    pub generics: Generics,
380    /// `type_` can come from either the HIR or from metadata. If it comes from HIR, it may be a type
381    /// alias instead of the final type. This will always have the final type, regardless of whether
382    /// `type_` came from HIR or from metadata.
383    ///
384    /// If `item_type.is_none()`, `type_` is guarenteed to come from metadata (and therefore hold the
385    /// final type).
386    pub item_type: Option<Type>,
387}
388
389#[derive(Deserialize, Clone, Debug)]
390pub struct OpaqueTy {
391    pub bounds: Vec<GenericBound>,
392    pub generics: Generics,
393}
394
395#[derive(Deserialize, Clone, PartialEq, Eq, Debug, Hash)]
396pub struct BareFunctionDecl {
397    pub generic_params: Vec<GenericParamDef>,
398    pub decl: FnDecl,
399}
400
401#[derive(Deserialize, Clone, Debug)]
402pub struct Static {
403    pub type_: Type,
404    pub mutability: Mutability,
405}
406
407#[derive(Deserialize, Clone, PartialEq, Eq, Hash, Debug)]
408pub struct Constant {
409    pub type_: Type,
410    pub kind: ConstantKind,
411}
412
413#[derive(Deserialize, Clone, PartialEq, Eq, Hash, Debug)]
414pub enum ConstantKind {
415    /// This is the wrapper around `ty::Const` for a non-local constant. Because it doesn't have a
416    /// `BodyId`, we need to handle it on its own.
417    ///
418    /// Note that `ty::Const` includes generic parameters, and may not always be uniquely identified
419    /// by a DefId. So this field must be different from `Extern`.
420    TyConst { expr: String },
421    /// A constant (expression) that's not an item or associated item. These are usually found
422    /// nested inside types (e.g., array lengths) or expressions (e.g., repeat counts), and also
423    /// used to define explicit discriminant values for enum variants.
424    Anonymous {
425        #[serde(skip)]
426        def_id: (),
427    },
428    /// A constant from a different crate.
429    Extern {
430        #[serde(skip)]
431        def_id: (),
432    },
433    /// `const FOO: u32 = ...;`
434    Local {
435        #[serde(skip)]
436        def_id: (),
437        #[serde(skip)]
438        body: (),
439    },
440}
441
442#[derive(Deserialize, Clone, Debug)]
443pub struct Impl {
444    pub generics: Generics,
445    pub trait_: Option<Type>,
446    pub for_: Type,
447    pub items: Vec<Item>,
448    pub negative_polarity: bool,
449    pub synthetic: bool,
450    pub blanket_impl: Option<Box<Type>>,
451}
452
453#[derive(Deserialize, Clone, Debug)]
454pub struct Import {
455    pub kind: ImportKind,
456    pub source: ImportSource,
457    pub should_be_displayed: bool,
458}
459
460#[derive(Deserialize, Clone, Debug)]
461pub enum ImportKind {
462    // use source as str;
463    Simple(Symbol),
464    // use source::*;
465    Glob,
466}
467
468#[derive(Deserialize, Clone, Debug)]
469pub struct ImportSource {
470    pub path: Path,
471}
472
473#[derive(Deserialize, Clone, Debug)]
474pub struct Macro {
475    pub source: String,
476    #[serde(skip)]
477    pub imported_from: Option<Symbol>,
478}
479
480#[derive(Deserialize, Clone, Debug)]
481pub struct ProcMacro {
482    #[serde(skip)]
483    pub helpers: Vec<Symbol>,
484}
485
486/// An type binding on an associated type (e.g., `A = Bar` in `Foo<A = Bar>` or
487/// `A: Send + Sync` in `Foo<A: Send + Sync>`).
488#[derive(Deserialize, Clone, PartialEq, Eq, Debug, Hash)]
489pub struct TypeBinding {
490    pub name: Symbol,
491    pub kind: TypeBindingKind,
492}
493
494#[derive(Deserialize, Clone, PartialEq, Eq, Debug, Hash)]
495pub enum TypeBindingKind {
496    Equality { ty: Type },
497    Constraint { bounds: Vec<GenericBound> },
498}