1 //! Rustdoc's JSON output interface
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.
6 use std::collections::HashMap;
7 use std::path::PathBuf;
9 use serde::{Deserialize, Serialize};
11 /// rustdoc format-version.
12 pub const FORMAT_VERSION: u32 = 16;
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, PartialEq, Eq, Serialize, Deserialize)]
19 /// The id of the root [`Module`] item of the local crate.
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,
37 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
38 pub struct ExternalCrate {
40 pub html_root_url: Option<String>,
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, PartialEq, Eq, Hash, Serialize, Deserialize)]
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.
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.
59 #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
61 /// The unique identifier of this item. Can be used to find this item in various mappings.
63 /// This can be used as a key to the `external_crates` map of [`Crate`] to see which crate
64 /// this item came from.
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
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>,
86 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
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),
96 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
97 pub struct Deprecation {
98 pub since: Option<String>,
99 pub note: Option<String>,
102 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
103 #[serde(rename_all = "snake_case")]
104 pub enum Visibility {
106 /// For the most part items are private by default. The exceptions are associated items of
107 /// public traits and variants of public enums.
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"`).
118 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
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> },
124 Parenthesized { inputs: Vec<Type>, output: Option<Type> },
127 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
128 #[serde(rename_all = "snake_case")]
129 pub enum GenericArg {
136 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
137 pub struct Constant {
138 #[serde(rename = "type")]
141 pub value: Option<String>,
142 pub is_literal: bool,
145 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
146 pub struct TypeBinding {
148 pub args: GenericArgs,
149 pub binding: TypeBindingKind,
152 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
153 #[serde(rename_all = "snake_case")]
154 pub enum TypeBindingKind {
156 Constraint(Vec<GenericBound>),
159 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
160 pub struct Id(pub String);
162 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
163 #[serde(rename_all = "snake_case")]
192 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
193 #[serde(tag = "kind", content = "inner", rename_all = "snake_case")]
198 rename: Option<String>,
211 TraitAlias(TraitAlias),
221 /// `type`s from an extern block
224 /// Declarative macro_rules! macro
226 ProcMacro(ProcMacro),
228 PrimitiveType(String),
231 #[serde(rename = "type")]
233 /// e.g. `const X: usize = 5;`
234 default: Option<String>,
238 bounds: Vec<GenericBound>,
239 /// e.g. `type X = usize;`
240 default: Option<Type>,
244 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
248 /// If `true`, this module is not part of the public API, but it contains
249 /// items that are re-exported as public API.
250 pub is_stripped: bool,
253 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
255 pub generics: Generics,
256 pub fields_stripped: bool,
261 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
263 pub struct_type: StructType,
264 pub generics: Generics,
265 pub fields_stripped: bool,
270 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
272 pub generics: Generics,
273 pub variants_stripped: bool,
274 pub variants: Vec<Id>,
278 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
279 #[serde(rename_all = "snake_case")]
280 #[serde(tag = "variant_kind", content = "variant_inner")]
287 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
288 #[serde(rename_all = "snake_case")]
289 pub enum StructType {
295 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
297 #[serde(rename = "const")]
299 #[serde(rename = "unsafe")]
301 #[serde(rename = "async")]
306 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
308 // We only have a concrete listing here for stable ABI's because their are so many
309 // See rustc_ast_passes::feature_gate::PostExpansionVisitor::check_abi for the list
312 Cdecl { unwind: bool },
313 Stdcall { unwind: bool },
314 Fastcall { unwind: bool },
315 Aapcs { unwind: bool },
316 Win64 { unwind: bool },
317 SysV64 { unwind: bool },
318 System { unwind: bool },
322 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
323 pub struct Function {
325 pub generics: Generics,
329 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
332 pub generics: Generics,
337 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
338 pub struct Generics {
339 pub params: Vec<GenericParamDef>,
340 pub where_predicates: Vec<WherePredicate>,
343 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
344 pub struct GenericParamDef {
346 pub kind: GenericParamDefKind,
349 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
350 #[serde(rename_all = "snake_case")]
351 pub enum GenericParamDefKind {
353 outlives: Vec<String>,
356 bounds: Vec<GenericBound>,
357 default: Option<Type>,
358 /// This is normally `false`, which means that this generic parameter is
359 /// declared in the Rust source text.
361 /// If it is `true`, this generic parameter has been introduced by the
362 /// compiler behind the scenes.
368 /// ```ignore (pseudo-rust)
369 /// pub fn f(_: impl Trait) {}
372 /// The compiler will transform this behind the scenes to
374 /// ```ignore (pseudo-rust)
375 /// pub fn f<impl Trait: Trait>(_: impl Trait) {}
378 /// In this example, the generic parameter named `impl Trait` (and which
379 /// is bound by `Trait`) is synthetic, because it was not originally in
380 /// the Rust source text.
384 #[serde(rename = "type")]
386 default: Option<String>,
390 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
391 #[serde(rename_all = "snake_case")]
392 pub enum WherePredicate {
394 #[serde(rename = "type")]
396 bounds: Vec<GenericBound>,
397 /// Used for Higher-Rank Trait Bounds (HRTBs)
399 /// where for<'a> &'a T: Iterator,"
404 generic_params: Vec<GenericParamDef>,
408 bounds: Vec<GenericBound>,
416 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
417 #[serde(rename_all = "snake_case")]
418 pub enum GenericBound {
420 #[serde(rename = "trait")]
422 /// Used for Higher-Rank Trait Bounds (HRTBs)
424 /// where F: for<'a, 'b> Fn(&'a u8, &'b u8)
429 generic_params: Vec<GenericParamDef>,
430 modifier: TraitBoundModifier,
435 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
436 #[serde(rename_all = "snake_case")]
437 pub enum TraitBoundModifier {
443 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
444 #[serde(rename_all = "snake_case")]
450 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
451 #[serde(rename_all = "snake_case")]
452 #[serde(tag = "kind", content = "inner")]
454 /// Structs, enums, and traits
458 args: Option<Box<GenericArgs>>,
459 param_names: Vec<GenericBound>,
461 /// Parameterized types
463 /// Fixed-size numeric types (plus int/usize/float), char, arrays, slices, and tuples
465 /// `extern "ABI" fn`
466 FunctionPointer(Box<FunctionPointer>),
467 /// `(String, u32, Box<usize>)`
473 #[serde(rename = "type")]
477 /// `impl TraitA + TraitB + ...`
478 ImplTrait(Vec<GenericBound>),
481 /// `*mut u32`, `*u8`, etc.
484 #[serde(rename = "type")]
487 /// `&'a mut String`, `&str`, etc.
489 lifetime: Option<String>,
491 #[serde(rename = "type")]
494 /// `<Type as Trait>::Name` or associated types like `T::Item` where `T: Iterator`
497 args: Box<GenericArgs>,
498 self_type: Box<Type>,
499 #[serde(rename = "trait")]
504 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
505 pub struct FunctionPointer {
507 /// Used for Higher-Rank Trait Bounds (HRTBs)
509 /// for<'c> fn(val: &'c i32) -> i32
514 pub generic_params: Vec<GenericParamDef>,
518 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
520 pub inputs: Vec<(String, Type)>,
521 pub output: Option<Type>,
522 pub c_variadic: bool,
525 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
530 pub generics: Generics,
531 pub bounds: Vec<GenericBound>,
532 pub implementations: Vec<Id>,
535 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
536 pub struct TraitAlias {
537 pub generics: Generics,
538 pub params: Vec<GenericBound>,
541 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
544 pub generics: Generics,
545 pub provided_trait_methods: Vec<String>,
546 #[serde(rename = "trait")]
547 pub trait_: Option<Type>,
548 #[serde(rename = "for")]
553 pub blanket_impl: Option<Type>,
556 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
557 #[serde(rename_all = "snake_case")]
559 /// The full path being imported.
561 /// May be different from the last segment of `source` when renaming imports:
562 /// `use source as name;`
564 /// The ID of the item being imported.
565 pub id: Option<Id>, // FIXME is this actually ever None?
566 /// Whether this import uses a glob: `use source::*;`
570 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
571 pub struct ProcMacro {
573 pub helpers: Vec<String>,
576 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
577 #[serde(rename_all = "snake_case")]
579 /// A bang macro `foo!()`.
581 /// An attribute macro `#[foo]`.
583 /// A derive macro `#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]`
587 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
589 #[serde(rename = "type")]
591 pub generics: Generics,
594 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
595 pub struct OpaqueTy {
596 pub bounds: Vec<GenericBound>,
597 pub generics: Generics,
600 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
602 #[serde(rename = "type")]