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#[derive(Deserialize, Clone, Debug)]
18pub struct CrateData {
19 pub items: Vec<Rc<IndexItem>>,
20}
21
22#[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#[derive(Deserialize, Clone, Debug)]
36pub struct Item {
37 pub name: Option<Symbol>,
40 pub visibility: Visibility,
41 pub kind: Box<ItemKind>,
44}
45
46#[derive(Deserialize, Clone, Debug)]
47pub enum ItemKind {
48 ExternCrateItem {
49 #[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 ),
60 OpaqueTyItem(OpaqueTy),
61 StaticItem(Static),
62 ConstantItem(Constant),
63 TraitItem(Trait),
64 TraitAliasItem(TraitAlias),
65 ImplItem(Impl),
66 TyMethodItem(Function),
69 MethodItem(Function, #[serde(skip)] Option<()>),
74 StructFieldItem(Type),
75 VariantItem(Variant),
76 ForeignFunctionItem(Function),
78 ForeignStaticItem(Static),
80 ForeignTypeItem,
82 MacroItem(Macro),
83 PrimitiveItem(PrimitiveType),
84 AssocConstItem(Type, Option<String>),
85 AssocTypeItem(Vec<GenericBound>, Option<Type>),
90 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#[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#[derive(Deserialize, Clone, PartialEq, Eq, Debug, Hash)]
211pub struct PolyTrait {
212 pub trait_: Type,
213 pub generic_params: Vec<GenericParamDef>,
214}
215
216#[derive(Deserialize, Clone, PartialEq, Eq, Debug, Hash)]
220pub enum Type {
221 ResolvedPath {
223 path: Path,
224 is_generic: bool,
226 },
227 DynTrait(Vec<PolyTrait>, Option<Lifetime>),
229 Generic(Symbol),
232 Primitive(PrimitiveType),
235 BareFunction(Box<BareFunctionDecl>),
237 Tuple(Vec<Type>),
238 Slice(Box<Type>),
239 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 QPath {
251 name: Symbol,
252 self_type: Box<Type>,
253 trait_: Box<Type>,
254 },
255
256 Infer,
258
259 ImplTrait(Vec<GenericBound>),
261}
262
263#[derive(Deserialize, Clone, PartialEq, Eq, Hash, Copy, Debug)]
264pub 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 Public,
298 Inherited,
302 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#[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 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 TyConst { expr: String },
421 Anonymous {
425 #[serde(skip)]
426 def_id: (),
427 },
428 Extern {
430 #[serde(skip)]
431 def_id: (),
432 },
433 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 Simple(Symbol),
464 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#[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}