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 = 17;
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 pub struct DynTrait {
120 /// All the traits implemented. One of them is the vtable, and the rest must be auto traits.
121 pub traits: Vec<PolyTrait>,
122 /// The lifetime of the whole dyn object
124 /// dyn Debug + 'static
129 pub lifetime: Option<String>,
132 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
133 /// A trait and potential HRTBs
134 pub struct PolyTrait {
135 #[serde(rename = "trait")]
137 /// Used for Higher-Rank Trait Bounds (HRTBs)
139 /// dyn for<'a> Fn() -> &'a i32"
144 pub generic_params: Vec<GenericParamDef>,
147 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
148 #[serde(rename_all = "snake_case")]
149 pub enum GenericArgs {
150 /// <'a, 32, B: Copy, C = u32>
151 AngleBracketed { args: Vec<GenericArg>, bindings: Vec<TypeBinding> },
153 Parenthesized { inputs: Vec<Type>, output: Option<Type> },
156 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
157 #[serde(rename_all = "snake_case")]
158 pub enum GenericArg {
165 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
166 pub struct Constant {
167 #[serde(rename = "type")]
170 pub value: Option<String>,
171 pub is_literal: bool,
174 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
175 pub struct TypeBinding {
177 pub args: GenericArgs,
178 pub binding: TypeBindingKind,
181 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
182 #[serde(rename_all = "snake_case")]
183 pub enum TypeBindingKind {
185 Constraint(Vec<GenericBound>),
188 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
189 pub struct Id(pub String);
191 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
192 #[serde(rename_all = "snake_case")]
221 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
222 #[serde(tag = "kind", content = "inner", rename_all = "snake_case")]
227 rename: Option<String>,
240 TraitAlias(TraitAlias),
250 /// `type`s from an extern block
253 /// Declarative macro_rules! macro
255 ProcMacro(ProcMacro),
257 PrimitiveType(String),
260 #[serde(rename = "type")]
262 /// e.g. `const X: usize = 5;`
263 default: Option<String>,
267 bounds: Vec<GenericBound>,
268 /// e.g. `type X = usize;`
269 default: Option<Type>,
273 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
277 /// If `true`, this module is not part of the public API, but it contains
278 /// items that are re-exported as public API.
279 pub is_stripped: bool,
282 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
284 pub generics: Generics,
285 pub fields_stripped: bool,
290 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
292 pub struct_type: StructType,
293 pub generics: Generics,
294 pub fields_stripped: bool,
299 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
301 pub generics: Generics,
302 pub variants_stripped: bool,
303 pub variants: Vec<Id>,
307 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
308 #[serde(rename_all = "snake_case")]
309 #[serde(tag = "variant_kind", content = "variant_inner")]
316 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
317 #[serde(rename_all = "snake_case")]
318 pub enum StructType {
324 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
326 #[serde(rename = "const")]
328 #[serde(rename = "unsafe")]
330 #[serde(rename = "async")]
335 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
337 // We only have a concrete listing here for stable ABI's because their are so many
338 // See rustc_ast_passes::feature_gate::PostExpansionVisitor::check_abi for the list
341 Cdecl { unwind: bool },
342 Stdcall { unwind: bool },
343 Fastcall { unwind: bool },
344 Aapcs { unwind: bool },
345 Win64 { unwind: bool },
346 SysV64 { unwind: bool },
347 System { unwind: bool },
351 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
352 pub struct Function {
354 pub generics: Generics,
358 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
361 pub generics: Generics,
366 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
367 pub struct Generics {
368 pub params: Vec<GenericParamDef>,
369 pub where_predicates: Vec<WherePredicate>,
372 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
373 pub struct GenericParamDef {
375 pub kind: GenericParamDefKind,
378 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
379 #[serde(rename_all = "snake_case")]
380 pub enum GenericParamDefKind {
382 outlives: Vec<String>,
385 bounds: Vec<GenericBound>,
386 default: Option<Type>,
387 /// This is normally `false`, which means that this generic parameter is
388 /// declared in the Rust source text.
390 /// If it is `true`, this generic parameter has been introduced by the
391 /// compiler behind the scenes.
397 /// ```ignore (pseudo-rust)
398 /// pub fn f(_: impl Trait) {}
401 /// The compiler will transform this behind the scenes to
403 /// ```ignore (pseudo-rust)
404 /// pub fn f<impl Trait: Trait>(_: impl Trait) {}
407 /// In this example, the generic parameter named `impl Trait` (and which
408 /// is bound by `Trait`) is synthetic, because it was not originally in
409 /// the Rust source text.
413 #[serde(rename = "type")]
415 default: Option<String>,
419 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
420 #[serde(rename_all = "snake_case")]
421 pub enum WherePredicate {
423 #[serde(rename = "type")]
425 bounds: Vec<GenericBound>,
426 /// Used for Higher-Rank Trait Bounds (HRTBs)
428 /// where for<'a> &'a T: Iterator,"
433 generic_params: Vec<GenericParamDef>,
437 bounds: Vec<GenericBound>,
445 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
446 #[serde(rename_all = "snake_case")]
447 pub enum GenericBound {
449 #[serde(rename = "trait")]
451 /// Used for Higher-Rank Trait Bounds (HRTBs)
453 /// where F: for<'a, 'b> Fn(&'a u8, &'b u8)
458 generic_params: Vec<GenericParamDef>,
459 modifier: TraitBoundModifier,
464 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
465 #[serde(rename_all = "snake_case")]
466 pub enum TraitBoundModifier {
472 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
473 #[serde(rename_all = "snake_case")]
479 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
480 #[serde(rename_all = "snake_case")]
481 #[serde(tag = "kind", content = "inner")]
483 /// Structs, enums, and traits
487 args: Option<Box<GenericArgs>>,
488 param_names: Vec<GenericBound>,
491 /// Parameterized types
493 /// Fixed-size numeric types (plus int/usize/float), char, arrays, slices, and tuples
495 /// `extern "ABI" fn`
496 FunctionPointer(Box<FunctionPointer>),
497 /// `(String, u32, Box<usize>)`
503 #[serde(rename = "type")]
507 /// `impl TraitA + TraitB + ...`
508 ImplTrait(Vec<GenericBound>),
511 /// `*mut u32`, `*u8`, etc.
514 #[serde(rename = "type")]
517 /// `&'a mut String`, `&str`, etc.
519 lifetime: Option<String>,
521 #[serde(rename = "type")]
524 /// `<Type as Trait>::Name` or associated types like `T::Item` where `T: Iterator`
527 args: Box<GenericArgs>,
528 self_type: Box<Type>,
529 #[serde(rename = "trait")]
534 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
535 pub struct FunctionPointer {
537 /// Used for Higher-Rank Trait Bounds (HRTBs)
539 /// for<'c> fn(val: &'c i32) -> i32
544 pub generic_params: Vec<GenericParamDef>,
548 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
550 pub inputs: Vec<(String, Type)>,
551 pub output: Option<Type>,
552 pub c_variadic: bool,
555 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
560 pub generics: Generics,
561 pub bounds: Vec<GenericBound>,
562 pub implementations: Vec<Id>,
565 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
566 pub struct TraitAlias {
567 pub generics: Generics,
568 pub params: Vec<GenericBound>,
571 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
574 pub generics: Generics,
575 pub provided_trait_methods: Vec<String>,
576 #[serde(rename = "trait")]
577 pub trait_: Option<Type>,
578 #[serde(rename = "for")]
583 pub blanket_impl: Option<Type>,
586 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
587 #[serde(rename_all = "snake_case")]
589 /// The full path being imported.
591 /// May be different from the last segment of `source` when renaming imports:
592 /// `use source as name;`
594 /// The ID of the item being imported. Will be `None` in case of re-exports of primitives:
596 /// pub use i32 as my_i32;
599 /// Whether this import uses a glob: `use source::*;`
603 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
604 pub struct ProcMacro {
606 pub helpers: Vec<String>,
609 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
610 #[serde(rename_all = "snake_case")]
612 /// A bang macro `foo!()`.
614 /// An attribute macro `#[foo]`.
616 /// A derive macro `#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]`
620 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
622 #[serde(rename = "type")]
624 pub generics: Generics,
627 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
628 pub struct OpaqueTy {
629 pub bounds: Vec<GenericBound>,
630 pub generics: Generics,
633 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
635 #[serde(rename = "type")]