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 = 14;
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)]
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, Serialize, Deserialize, PartialEq)]
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, 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.
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, Serialize, Deserialize, PartialEq)]
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, Serialize, Deserialize, PartialEq)]
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, Serialize, Deserialize, PartialEq)]
97 pub struct Deprecation {
98 pub since: Option<String>,
99 pub note: Option<String>,
102 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
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, 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> },
124 Parenthesized { inputs: Vec<Type>, output: Option<Type> },
127 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
128 #[serde(rename_all = "snake_case")]
129 pub enum GenericArg {
136 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
137 pub struct Constant {
138 #[serde(rename = "type")]
141 pub value: Option<String>,
142 pub is_literal: bool,
145 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
146 pub struct TypeBinding {
148 pub args: GenericArgs,
149 pub binding: TypeBindingKind,
152 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
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, Serialize, Deserialize, PartialEq)]
163 #[serde(rename_all = "snake_case")]
192 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
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, Serialize, Deserialize, PartialEq)]
250 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
252 pub generics: Generics,
253 pub fields_stripped: bool,
258 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
260 pub struct_type: StructType,
261 pub generics: Generics,
262 pub fields_stripped: bool,
267 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
269 pub generics: Generics,
270 pub variants_stripped: bool,
271 pub variants: Vec<Id>,
275 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
276 #[serde(rename_all = "snake_case")]
277 #[serde(tag = "variant_kind", content = "variant_inner")]
284 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
285 #[serde(rename_all = "snake_case")]
286 pub enum StructType {
292 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
294 #[serde(rename = "const")]
296 #[serde(rename = "unsafe")]
298 #[serde(rename = "async")]
303 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
305 // We only have a concrete listing here for stable ABI's because their are so many
306 // See rustc_ast_passes::feature_gate::PostExpansionVisitor::check_abi for the list
309 Cdecl { unwind: bool },
310 Stdcall { unwind: bool },
311 Fastcall { unwind: bool },
312 Aapcs { unwind: bool },
313 Win64 { unwind: bool },
314 SysV64 { unwind: bool },
315 System { unwind: bool },
319 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
320 pub struct Function {
322 pub generics: Generics,
326 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
329 pub generics: Generics,
334 #[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
335 pub struct Generics {
336 pub params: Vec<GenericParamDef>,
337 pub where_predicates: Vec<WherePredicate>,
340 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
341 pub struct GenericParamDef {
343 pub kind: GenericParamDefKind,
346 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
347 #[serde(rename_all = "snake_case")]
348 pub enum GenericParamDefKind {
350 outlives: Vec<String>,
353 bounds: Vec<GenericBound>,
354 default: Option<Type>,
355 /// This is normally `false`, which means that this generic parameter is
356 /// declared in the Rust source text.
358 /// If it is `true`, this generic parameter has been introduced by the
359 /// compiler behind the scenes.
365 /// ```ignore (pseudo-rust)
366 /// pub fn f(_: impl Trait) {}
369 /// The compiler will transform this behind the scenes to
371 /// ```ignore (pseudo-rust)
372 /// pub fn f<impl Trait: Trait>(_: impl Trait) {}
375 /// In this example, the generic parameter named `impl Trait` (and which
376 /// is bound by `Trait`) is synthetic, because it was not originally in
377 /// the Rust source text.
381 #[serde(rename = "type")]
383 default: Option<String>,
387 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
388 #[serde(rename_all = "snake_case")]
389 pub enum WherePredicate {
391 #[serde(rename = "type")]
393 bounds: Vec<GenericBound>,
397 bounds: Vec<GenericBound>,
405 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
406 #[serde(rename_all = "snake_case")]
407 pub enum GenericBound {
409 #[serde(rename = "trait")]
411 /// Used for Higher-Rank Trait Bounds (HRTBs)
413 /// where F: for<'a, 'b> Fn(&'a u8, &'b u8)
418 generic_params: Vec<GenericParamDef>,
419 modifier: TraitBoundModifier,
424 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
425 #[serde(rename_all = "snake_case")]
426 pub enum TraitBoundModifier {
432 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
433 #[serde(rename_all = "snake_case")]
439 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
440 #[serde(rename_all = "snake_case")]
441 #[serde(tag = "kind", content = "inner")]
443 /// Structs, enums, and traits
447 args: Option<Box<GenericArgs>>,
448 param_names: Vec<GenericBound>,
450 /// Parameterized types
452 /// Fixed-size numeric types (plus int/usize/float), char, arrays, slices, and tuples
454 /// `extern "ABI" fn`
455 FunctionPointer(Box<FunctionPointer>),
456 /// `(String, u32, Box<usize>)`
462 #[serde(rename = "type")]
466 /// `impl TraitA + TraitB + ...`
467 ImplTrait(Vec<GenericBound>),
470 /// `*mut u32`, `*u8`, etc.
473 #[serde(rename = "type")]
476 /// `&'a mut String`, `&str`, etc.
478 lifetime: Option<String>,
480 #[serde(rename = "type")]
483 /// `<Type as Trait>::Name` or associated types like `T::Item` where `T: Iterator`
486 args: Box<GenericArgs>,
487 self_type: Box<Type>,
488 #[serde(rename = "trait")]
493 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
494 pub struct FunctionPointer {
496 /// Used for Higher-Rank Trait Bounds (HRTBs)
498 /// for<'c> fn(val: &'c i32) -> i32
503 pub generic_params: Vec<GenericParamDef>,
507 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
509 pub inputs: Vec<(String, Type)>,
510 pub output: Option<Type>,
511 pub c_variadic: bool,
514 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
519 pub generics: Generics,
520 pub bounds: Vec<GenericBound>,
521 pub implementations: Vec<Id>,
524 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
525 pub struct TraitAlias {
526 pub generics: Generics,
527 pub params: Vec<GenericBound>,
530 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
533 pub generics: Generics,
534 pub provided_trait_methods: Vec<String>,
535 #[serde(rename = "trait")]
536 pub trait_: Option<Type>,
537 #[serde(rename = "for")]
542 pub blanket_impl: Option<Type>,
545 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
546 #[serde(rename_all = "snake_case")]
548 /// The full path being imported.
550 /// May be different from the last segment of `source` when renaming imports:
551 /// `use source as name;`
553 /// The ID of the item being imported.
554 pub id: Option<Id>, // FIXME is this actually ever None?
555 /// Whether this import uses a glob: `use source::*;`
559 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
560 pub struct ProcMacro {
562 pub helpers: Vec<String>,
565 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
566 #[serde(rename_all = "snake_case")]
568 /// A bang macro `foo!()`.
570 /// An attribute macro `#[foo]`.
572 /// A derive macro `#[derive(Foo)]`
576 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
578 #[serde(rename = "type")]
580 pub generics: Generics,
583 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
584 pub struct OpaqueTy {
585 pub bounds: Vec<GenericBound>,
586 pub generics: Generics,
589 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
591 #[serde(rename = "type")]