1use std::collections::{HashMap, HashSet};
9use std::path::PathBuf;
10
11use serde::{Deserialize, Serialize};
12
13#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
17pub struct Crate {
18 pub root: Id,
20 pub crate_version: Option<String>,
22 pub includes_private: bool,
24 pub index: HashMap<Id, Item>,
27 pub paths: HashMap<Id, ItemSummary>,
29 pub external_crates: HashMap<u32, ExternalCrate>,
31 pub format_version: u32,
34}
35
36#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
37pub struct ExternalCrate {
38 pub name: String,
39 pub html_root_url: Option<String>,
40}
41
42#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
47pub struct ItemSummary {
48 pub crate_id: u32,
51 pub path: Vec<String>,
54 pub kind: ItemKind,
56}
57
58#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
59pub struct Item {
60 pub id: Id,
62 pub crate_id: u32,
65 pub name: Option<String>,
67 pub span: Option<Span>,
70 pub visibility: Visibility,
73 pub docs: Option<String>,
76 pub links: HashMap<String, Id>,
78 pub attrs: Vec<String>,
80 pub deprecation: Option<Deprecation>,
81 #[serde(flatten)]
82 pub inner: ItemEnum,
83}
84
85#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
86pub struct Span {
87 pub filename: PathBuf,
89 pub begin: (usize, usize),
91 pub end: (usize, usize),
93}
94
95#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
96pub struct Deprecation {
97 pub since: Option<String>,
98 pub note: Option<String>,
99}
100
101#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
102#[serde(rename_all = "snake_case")]
103pub enum Visibility {
104 Public,
105 Default,
108 Crate,
109 Restricted {
112 parent: Id,
113 path: String,
114 },
115}
116
117#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
118#[serde(rename_all = "snake_case")]
119pub enum GenericArgs {
120 AngleBracketed {
122 args: Vec<GenericArg>,
123 bindings: Vec<TypeBinding>,
124 },
125 Parenthesized {
127 inputs: Vec<Type>,
128 output: Option<Type>,
129 },
130}
131
132#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
133#[serde(rename_all = "snake_case")]
134pub enum GenericArg {
135 Lifetime(String),
136 Type(Type),
137 Const(Constant),
138 Infer,
139}
140
141#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
142pub struct Constant {
143 #[serde(rename = "type")]
144 pub type_: Type,
145 pub expr: String,
146 pub value: Option<String>,
147 pub is_literal: bool,
148}
149
150#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
151pub struct TypeBinding {
152 pub name: String,
153 pub binding: TypeBindingKind,
154}
155
156#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
157#[serde(rename_all = "snake_case")]
158pub enum TypeBindingKind {
159 Equality(Type),
160 Constraint(Vec<GenericBound>),
161}
162
163#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
164pub struct Id(pub String);
165
166#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
167#[serde(rename_all = "snake_case")]
168pub enum ItemKind {
169 Module,
170 ExternCrate,
171 Import,
172 Struct,
173 StructField,
174 Union,
175 Enum,
176 Variant,
177 Function,
178 Typedef,
179 OpaqueTy,
180 Constant,
181 Trait,
182 TraitAlias,
183 Method,
184 Impl,
185 Static,
186 ForeignType,
187 Macro,
188 ProcAttribute,
189 ProcDerive,
190 AssocConst,
191 AssocType,
192 Primitive,
193 Keyword,
194}
195
196#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
197#[serde(tag = "kind", content = "inner", rename_all = "snake_case")]
198pub enum ItemEnum {
199 Module(Module),
200 ExternCrate {
201 name: String,
202 rename: Option<String>,
203 },
204 Import(Import),
205
206 Union(Union),
207 Struct(Struct),
208 StructField(Type),
209 Enum(Enum),
210 Variant(Variant),
211
212 Function(Function),
213
214 Trait(Trait),
215 TraitAlias(TraitAlias),
216 Method(Method),
217 Impl(Impl),
218
219 Typedef(Typedef),
220 OpaqueTy(OpaqueTy),
221 Constant(Constant),
222
223 Static(Static),
224
225 ForeignType,
227
228 Macro(String),
230 ProcMacro(ProcMacro),
231
232 PrimitiveType(String),
233
234 AssocConst {
235 #[serde(rename = "type")]
236 type_: Type,
237 default: Option<String>,
239 },
240 AssocType {
241 bounds: Vec<GenericBound>,
242 default: Option<Type>,
244 },
245}
246
247#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
248pub struct Module {
249 pub is_crate: bool,
250 pub items: Vec<Id>,
251}
252
253#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
254pub struct Union {
255 pub generics: Generics,
256 pub fields_stripped: bool,
257 pub fields: Vec<Id>,
258 pub impls: Vec<Id>,
259}
260
261#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
262pub struct Struct {
263 pub struct_type: StructType,
264 pub generics: Generics,
265 pub fields_stripped: bool,
266 pub fields: Vec<Id>,
267 pub impls: Vec<Id>,
268}
269
270#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
271pub struct Enum {
272 pub generics: Generics,
273 pub variants_stripped: bool,
274 pub variants: Vec<Id>,
275 pub impls: Vec<Id>,
276}
277
278#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
279#[serde(rename_all = "snake_case")]
280#[serde(tag = "variant_kind", content = "variant_inner")]
281pub enum Variant {
282 Plain,
283 Tuple(Vec<Type>),
284 Struct(Vec<Id>),
285}
286
287#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
288#[serde(rename_all = "snake_case")]
289pub enum StructType {
290 Plain,
291 Tuple,
292 Unit,
293}
294
295#[non_exhaustive]
296#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
297#[serde(rename_all = "snake_case")]
298pub enum Qualifiers {
299 Const,
300 Unsafe,
301 Async,
302}
303
304#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
305pub struct Function {
306 pub decl: FnDecl,
307 pub generics: Generics,
308 pub header: HashSet<Qualifiers>,
309 pub abi: String,
310}
311
312#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
313pub struct Method {
314 pub decl: FnDecl,
315 pub generics: Generics,
316 pub header: HashSet<Qualifiers>,
317 pub abi: String,
318 pub has_body: bool,
319}
320
321#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
322pub struct Generics {
323 pub params: Vec<GenericParamDef>,
324 pub where_predicates: Vec<WherePredicate>,
325}
326
327#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
328pub struct GenericParamDef {
329 pub name: String,
330 pub kind: GenericParamDefKind,
331}
332
333#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
334#[serde(rename_all = "snake_case")]
335pub enum GenericParamDefKind {
336 Lifetime {
337 outlives: Vec<String>,
338 },
339 Type {
340 bounds: Vec<GenericBound>,
341 default: Option<Type>,
342 },
343 Const {
344 ty: Type,
345 default: Option<String>,
346 },
347}
348
349#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
350#[serde(rename_all = "snake_case")]
351pub enum WherePredicate {
352 BoundPredicate {
353 ty: Type,
354 bounds: Vec<GenericBound>,
355 },
356 RegionPredicate {
357 lifetime: String,
358 bounds: Vec<GenericBound>,
359 },
360 EqPredicate {
361 lhs: Type,
362 rhs: Type,
363 },
364}
365
366#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
367#[serde(rename_all = "snake_case")]
368pub enum GenericBound {
369 TraitBound {
370 #[serde(rename = "trait")]
371 trait_: Type,
372 generic_params: Vec<GenericParamDef>,
374 modifier: TraitBoundModifier,
375 },
376 Outlives(String),
377}
378
379#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
380#[serde(rename_all = "snake_case")]
381pub enum TraitBoundModifier {
382 None,
383 Maybe,
384 MaybeConst,
385}
386
387#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
388#[serde(rename_all = "snake_case")]
389#[serde(tag = "kind", content = "inner")]
390pub enum Type {
391 ResolvedPath {
393 name: String,
394 id: Id,
395 args: Option<Box<GenericArgs>>,
396 param_names: Vec<GenericBound>,
397 },
398 Generic(String),
400 Primitive(String),
402 FunctionPointer(Box<FunctionPointer>),
404 Tuple(Vec<Type>),
406 Slice(Box<Type>),
408 Array {
410 #[serde(rename = "type")]
411 type_: Box<Type>,
412 len: String,
413 },
414 ImplTrait(Vec<GenericBound>),
416 Infer,
418 RawPointer {
420 mutable: bool,
421 #[serde(rename = "type")]
422 type_: Box<Type>,
423 },
424 BorrowedRef {
426 lifetime: Option<String>,
427 mutable: bool,
428 #[serde(rename = "type")]
429 type_: Box<Type>,
430 },
431 QualifiedPath {
433 name: String,
434 self_type: Box<Type>,
435 #[serde(rename = "trait")]
436 trait_: Box<Type>,
437 },
438}
439
440#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
441pub struct FunctionPointer {
442 pub decl: FnDecl,
443 pub generic_params: Vec<GenericParamDef>,
444 pub header: HashSet<Qualifiers>,
445 pub abi: String,
446}
447
448#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
449pub struct FnDecl {
450 pub inputs: Vec<(String, Type)>,
451 pub output: Option<Type>,
452 pub c_variadic: bool,
453}
454
455#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
456pub struct Trait {
457 pub is_auto: bool,
458 pub is_unsafe: bool,
459 pub items: Vec<Id>,
460 pub generics: Generics,
461 pub bounds: Vec<GenericBound>,
462 pub implementors: Vec<Id>,
463}
464
465#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
466pub struct TraitAlias {
467 pub generics: Generics,
468 pub params: Vec<GenericBound>,
469}
470
471#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
472pub struct Impl {
473 pub is_unsafe: bool,
474 pub generics: Generics,
475 pub provided_trait_methods: Vec<String>,
476 #[serde(rename = "trait")]
477 pub trait_: Option<Type>,
478 #[serde(rename = "for")]
479 pub for_: Type,
480 pub items: Vec<Id>,
481 pub negative: bool,
482 pub synthetic: bool,
483 pub blanket_impl: Option<Type>,
484}
485
486#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
487#[serde(rename_all = "snake_case")]
488pub struct Import {
489 pub source: String,
491 pub name: String,
494 pub id: Option<Id>, pub glob: bool,
498}
499
500#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
501pub struct ProcMacro {
502 pub kind: MacroKind,
503 pub helpers: Vec<String>,
504}
505
506#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
507#[serde(rename_all = "snake_case")]
508pub enum MacroKind {
509 Bang,
511 Attr,
513 Derive,
515}
516
517#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
518pub struct Typedef {
519 #[serde(rename = "type")]
520 pub type_: Type,
521 pub generics: Generics,
522}
523
524#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
525pub struct OpaqueTy {
526 pub bounds: Vec<GenericBound>,
527 pub generics: Generics,
528}
529
530#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
531pub struct Static {
532 #[serde(rename = "type")]
533 pub type_: Type,
534 pub mutable: bool,
535 pub expr: String,
536}
537
538pub const FORMAT_VERSION: u32 = 9;
540
541