]> git.lizzy.rs Git - rust.git/blob - src/rustdoc-json-types/lib.rs
Rollup merge of #93068 - jsha:dot-spacing, r=GuillaumeGomez
[rust.git] / src / rustdoc-json-types / lib.rs
1 //! Rustdoc's JSON output interface
2 //!
3 //! These types are the public API exposed through the `--output-format json` flag. The [`Crate`]
4 //! struct is the root of the JSON blob and all other items are contained within.
5
6 use std::collections::{HashMap, HashSet};
7 use std::path::PathBuf;
8
9 use serde::{Deserialize, Serialize};
10
11 /// rustdoc format-version.
12 pub const FORMAT_VERSION: u32 = 10;
13
14 /// A `Crate` is the root of the emitted JSON blob. It contains all type/documentation information
15 /// about the language items in the local crate, as well as info about external items to allow
16 /// tools to find or link to them.
17 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
18 pub struct Crate {
19     /// The id of the root [`Module`] item of the local crate.
20     pub root: Id,
21     /// The version string given to `--crate-version`, if any.
22     pub crate_version: Option<String>,
23     /// Whether or not the output includes private items.
24     pub includes_private: bool,
25     /// A collection of all items in the local crate as well as some external traits and their
26     /// items that are referenced locally.
27     pub index: HashMap<Id, Item>,
28     /// Maps IDs to fully qualified paths and other info helpful for generating links.
29     pub paths: HashMap<Id, ItemSummary>,
30     /// Maps `crate_id` of items to a crate name and html_root_url if it exists.
31     pub external_crates: HashMap<u32, ExternalCrate>,
32     /// A single version number to be used in the future when making backwards incompatible changes
33     /// to the JSON output.
34     pub format_version: u32,
35 }
36
37 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
38 pub struct ExternalCrate {
39     pub name: String,
40     pub html_root_url: Option<String>,
41 }
42
43 /// For external (not defined in the local crate) items, you don't get the same level of
44 /// information. This struct should contain enough to generate a link/reference to the item in
45 /// question, or can be used by a tool that takes the json output of multiple crates to find
46 /// the actual item definition with all the relevant info.
47 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
48 pub struct ItemSummary {
49     /// Can be used to look up the name and html_root_url of the crate this item came from in the
50     /// `external_crates` map.
51     pub crate_id: u32,
52     /// The list of path components for the fully qualified path of this item (e.g.
53     /// `["std", "io", "lazy", "Lazy"]` for `std::io::lazy::Lazy`).
54     pub path: Vec<String>,
55     /// Whether this item is a struct, trait, macro, etc.
56     pub kind: ItemKind,
57 }
58
59 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
60 pub struct Item {
61     /// The unique identifier of this item. Can be used to find this item in various mappings.
62     pub id: Id,
63     /// This can be used as a key to the `external_crates` map of [`Crate`] to see which crate
64     /// this item came from.
65     pub crate_id: u32,
66     /// Some items such as impls don't have names.
67     pub name: Option<String>,
68     /// The source location of this item (absent if it came from a macro expansion or inline
69     /// assembly).
70     pub span: Option<Span>,
71     /// By default all documented items are public, but you can tell rustdoc to output private items
72     /// so this field is needed to differentiate.
73     pub visibility: Visibility,
74     /// The full markdown docstring of this item. Absent if there is no documentation at all,
75     /// Some("") if there is some documentation but it is empty (EG `#[doc = ""]`).
76     pub docs: Option<String>,
77     /// This mapping resolves [intra-doc links](https://github.com/rust-lang/rfcs/blob/master/text/1946-intra-rustdoc-links.md) from the docstring to their IDs
78     pub links: HashMap<String, Id>,
79     /// Stringified versions of the attributes on this item (e.g. `"#[inline]"`)
80     pub attrs: Vec<String>,
81     pub deprecation: Option<Deprecation>,
82     #[serde(flatten)]
83     pub inner: ItemEnum,
84 }
85
86 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
87 pub struct Span {
88     /// The path to the source file for this span relative to the path `rustdoc` was invoked with.
89     pub filename: PathBuf,
90     /// Zero indexed Line and Column of the first character of the `Span`
91     pub begin: (usize, usize),
92     /// Zero indexed Line and Column of the last character of the `Span`
93     pub end: (usize, usize),
94 }
95
96 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
97 pub struct Deprecation {
98     pub since: Option<String>,
99     pub note: Option<String>,
100 }
101
102 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
103 #[serde(rename_all = "snake_case")]
104 pub enum Visibility {
105     Public,
106     /// For the most part items are private by default. The exceptions are associated items of
107     /// public traits and variants of public enums.
108     Default,
109     Crate,
110     /// For `pub(in path)` visibility. `parent` is the module it's restricted to and `path` is how
111     /// that module was referenced (like `"super::super"` or `"crate::foo::bar"`).
112     Restricted {
113         parent: Id,
114         path: String,
115     },
116 }
117
118 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
119 #[serde(rename_all = "snake_case")]
120 pub enum GenericArgs {
121     /// <'a, 32, B: Copy, C = u32>
122     AngleBracketed { args: Vec<GenericArg>, bindings: Vec<TypeBinding> },
123     /// Fn(A, B) -> C
124     Parenthesized { inputs: Vec<Type>, output: Option<Type> },
125 }
126
127 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
128 #[serde(rename_all = "snake_case")]
129 pub enum GenericArg {
130     Lifetime(String),
131     Type(Type),
132     Const(Constant),
133     Infer,
134 }
135
136 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
137 pub struct Constant {
138     #[serde(rename = "type")]
139     pub type_: Type,
140     pub expr: String,
141     pub value: Option<String>,
142     pub is_literal: bool,
143 }
144
145 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
146 pub struct TypeBinding {
147     pub name: String,
148     pub binding: TypeBindingKind,
149 }
150
151 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
152 #[serde(rename_all = "snake_case")]
153 pub enum TypeBindingKind {
154     Equality(Term),
155     Constraint(Vec<GenericBound>),
156 }
157
158 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
159 pub struct Id(pub String);
160
161 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
162 #[serde(rename_all = "snake_case")]
163 pub enum ItemKind {
164     Module,
165     ExternCrate,
166     Import,
167     Struct,
168     StructField,
169     Union,
170     Enum,
171     Variant,
172     Function,
173     Typedef,
174     OpaqueTy,
175     Constant,
176     Trait,
177     TraitAlias,
178     Method,
179     Impl,
180     Static,
181     ForeignType,
182     Macro,
183     ProcAttribute,
184     ProcDerive,
185     AssocConst,
186     AssocType,
187     Primitive,
188     Keyword,
189 }
190
191 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
192 #[serde(tag = "kind", content = "inner", rename_all = "snake_case")]
193 pub enum ItemEnum {
194     Module(Module),
195     ExternCrate {
196         name: String,
197         rename: Option<String>,
198     },
199     Import(Import),
200
201     Union(Union),
202     Struct(Struct),
203     StructField(Type),
204     Enum(Enum),
205     Variant(Variant),
206
207     Function(Function),
208
209     Trait(Trait),
210     TraitAlias(TraitAlias),
211     Method(Method),
212     Impl(Impl),
213
214     Typedef(Typedef),
215     OpaqueTy(OpaqueTy),
216     Constant(Constant),
217
218     Static(Static),
219
220     /// `type`s from an extern block
221     ForeignType,
222
223     /// Declarative macro_rules! macro
224     Macro(String),
225     ProcMacro(ProcMacro),
226
227     PrimitiveType(String),
228
229     AssocConst {
230         #[serde(rename = "type")]
231         type_: Type,
232         /// e.g. `const X: usize = 5;`
233         default: Option<String>,
234     },
235     AssocType {
236         bounds: Vec<GenericBound>,
237         /// e.g. `type X = usize;`
238         default: Option<Type>,
239     },
240 }
241
242 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
243 pub struct Module {
244     pub is_crate: bool,
245     pub items: Vec<Id>,
246 }
247
248 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
249 pub struct Union {
250     pub generics: Generics,
251     pub fields_stripped: bool,
252     pub fields: Vec<Id>,
253     pub impls: Vec<Id>,
254 }
255
256 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
257 pub struct Struct {
258     pub struct_type: StructType,
259     pub generics: Generics,
260     pub fields_stripped: bool,
261     pub fields: Vec<Id>,
262     pub impls: Vec<Id>,
263 }
264
265 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
266 pub struct Enum {
267     pub generics: Generics,
268     pub variants_stripped: bool,
269     pub variants: Vec<Id>,
270     pub impls: Vec<Id>,
271 }
272
273 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
274 #[serde(rename_all = "snake_case")]
275 #[serde(tag = "variant_kind", content = "variant_inner")]
276 pub enum Variant {
277     Plain,
278     Tuple(Vec<Type>),
279     Struct(Vec<Id>),
280 }
281
282 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
283 #[serde(rename_all = "snake_case")]
284 pub enum StructType {
285     Plain,
286     Tuple,
287     Unit,
288 }
289
290 #[non_exhaustive]
291 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
292 #[serde(rename_all = "snake_case")]
293 pub enum Qualifiers {
294     Const,
295     Unsafe,
296     Async,
297 }
298
299 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
300 pub struct Function {
301     pub decl: FnDecl,
302     pub generics: Generics,
303     pub header: HashSet<Qualifiers>,
304     pub abi: String,
305 }
306
307 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
308 pub struct Method {
309     pub decl: FnDecl,
310     pub generics: Generics,
311     pub header: HashSet<Qualifiers>,
312     pub abi: String,
313     pub has_body: bool,
314 }
315
316 #[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
317 pub struct Generics {
318     pub params: Vec<GenericParamDef>,
319     pub where_predicates: Vec<WherePredicate>,
320 }
321
322 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
323 pub struct GenericParamDef {
324     pub name: String,
325     pub kind: GenericParamDefKind,
326 }
327
328 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
329 #[serde(rename_all = "snake_case")]
330 pub enum GenericParamDefKind {
331     Lifetime { outlives: Vec<String> },
332     Type { bounds: Vec<GenericBound>, default: Option<Type> },
333     Const { ty: Type, default: Option<String> },
334 }
335
336 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
337 #[serde(rename_all = "snake_case")]
338 pub enum WherePredicate {
339     BoundPredicate { ty: Type, bounds: Vec<GenericBound> },
340     RegionPredicate { lifetime: String, bounds: Vec<GenericBound> },
341     EqPredicate { lhs: Type, rhs: Term },
342 }
343
344 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
345 #[serde(rename_all = "snake_case")]
346 pub enum GenericBound {
347     TraitBound {
348         #[serde(rename = "trait")]
349         trait_: Type,
350         /// Used for HRTBs
351         generic_params: Vec<GenericParamDef>,
352         modifier: TraitBoundModifier,
353     },
354     Outlives(String),
355 }
356
357 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
358 #[serde(rename_all = "snake_case")]
359 pub enum TraitBoundModifier {
360     None,
361     Maybe,
362     MaybeConst,
363 }
364
365 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
366 #[serde(rename_all = "snake_case")]
367 pub enum Term {
368     Type(Type),
369     Constant(Constant),
370 }
371
372 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
373 #[serde(rename_all = "snake_case")]
374 #[serde(tag = "kind", content = "inner")]
375 pub enum Type {
376     /// Structs, enums, and traits
377     ResolvedPath {
378         name: String,
379         id: Id,
380         args: Option<Box<GenericArgs>>,
381         param_names: Vec<GenericBound>,
382     },
383     /// Parameterized types
384     Generic(String),
385     /// Fixed-size numeric types (plus int/usize/float), char, arrays, slices, and tuples
386     Primitive(String),
387     /// `extern "ABI" fn`
388     FunctionPointer(Box<FunctionPointer>),
389     /// `(String, u32, Box<usize>)`
390     Tuple(Vec<Type>),
391     /// `[u32]`
392     Slice(Box<Type>),
393     /// [u32; 15]
394     Array {
395         #[serde(rename = "type")]
396         type_: Box<Type>,
397         len: String,
398     },
399     /// `impl TraitA + TraitB + ...`
400     ImplTrait(Vec<GenericBound>),
401     /// `_`
402     Infer,
403     /// `*mut u32`, `*u8`, etc.
404     RawPointer {
405         mutable: bool,
406         #[serde(rename = "type")]
407         type_: Box<Type>,
408     },
409     /// `&'a mut String`, `&str`, etc.
410     BorrowedRef {
411         lifetime: Option<String>,
412         mutable: bool,
413         #[serde(rename = "type")]
414         type_: Box<Type>,
415     },
416     /// `<Type as Trait>::Name` or associated types like `T::Item` where `T: Iterator`
417     QualifiedPath {
418         name: String,
419         self_type: Box<Type>,
420         #[serde(rename = "trait")]
421         trait_: Box<Type>,
422     },
423 }
424
425 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
426 pub struct FunctionPointer {
427     pub decl: FnDecl,
428     pub generic_params: Vec<GenericParamDef>,
429     pub header: HashSet<Qualifiers>,
430     pub abi: String,
431 }
432
433 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
434 pub struct FnDecl {
435     pub inputs: Vec<(String, Type)>,
436     pub output: Option<Type>,
437     pub c_variadic: bool,
438 }
439
440 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
441 pub struct Trait {
442     pub is_auto: bool,
443     pub is_unsafe: bool,
444     pub items: Vec<Id>,
445     pub generics: Generics,
446     pub bounds: Vec<GenericBound>,
447     pub implementors: Vec<Id>,
448 }
449
450 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
451 pub struct TraitAlias {
452     pub generics: Generics,
453     pub params: Vec<GenericBound>,
454 }
455
456 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
457 pub struct Impl {
458     pub is_unsafe: bool,
459     pub generics: Generics,
460     pub provided_trait_methods: Vec<String>,
461     #[serde(rename = "trait")]
462     pub trait_: Option<Type>,
463     #[serde(rename = "for")]
464     pub for_: Type,
465     pub items: Vec<Id>,
466     pub negative: bool,
467     pub synthetic: bool,
468     pub blanket_impl: Option<Type>,
469 }
470
471 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
472 #[serde(rename_all = "snake_case")]
473 pub struct Import {
474     /// The full path being imported.
475     pub source: String,
476     /// May be different from the last segment of `source` when renaming imports:
477     /// `use source as name;`
478     pub name: String,
479     /// The ID of the item being imported.
480     pub id: Option<Id>, // FIXME is this actually ever None?
481     /// Whether this import uses a glob: `use source::*;`
482     pub glob: bool,
483 }
484
485 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
486 pub struct ProcMacro {
487     pub kind: MacroKind,
488     pub helpers: Vec<String>,
489 }
490
491 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
492 #[serde(rename_all = "snake_case")]
493 pub enum MacroKind {
494     /// A bang macro `foo!()`.
495     Bang,
496     /// An attribute macro `#[foo]`.
497     Attr,
498     /// A derive macro `#[derive(Foo)]`
499     Derive,
500 }
501
502 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
503 pub struct Typedef {
504     #[serde(rename = "type")]
505     pub type_: Type,
506     pub generics: Generics,
507 }
508
509 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
510 pub struct OpaqueTy {
511     pub bounds: Vec<GenericBound>,
512     pub generics: Generics,
513 }
514
515 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
516 pub struct Static {
517     #[serde(rename = "type")]
518     pub type_: Type,
519     pub mutable: bool,
520     pub expr: String,
521 }
522
523 #[cfg(test)]
524 mod tests;