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 = 11;
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 binding: TypeBindingKind,
151 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
152 #[serde(rename_all = "snake_case")]
153 pub enum TypeBindingKind {
155 Constraint(Vec<GenericBound>),
158 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
159 pub struct Id(pub String);
161 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
162 #[serde(rename_all = "snake_case")]
191 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
192 #[serde(tag = "kind", content = "inner", rename_all = "snake_case")]
197 rename: Option<String>,
210 TraitAlias(TraitAlias),
220 /// `type`s from an extern block
223 /// Declarative macro_rules! macro
225 ProcMacro(ProcMacro),
227 PrimitiveType(String),
230 #[serde(rename = "type")]
232 /// e.g. `const X: usize = 5;`
233 default: Option<String>,
236 bounds: Vec<GenericBound>,
237 /// e.g. `type X = usize;`
238 default: Option<Type>,
242 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
248 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
250 pub generics: Generics,
251 pub fields_stripped: bool,
256 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
258 pub struct_type: StructType,
259 pub generics: Generics,
260 pub fields_stripped: bool,
265 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
267 pub generics: Generics,
268 pub variants_stripped: bool,
269 pub variants: Vec<Id>,
273 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
274 #[serde(rename_all = "snake_case")]
275 #[serde(tag = "variant_kind", content = "variant_inner")]
282 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
283 #[serde(rename_all = "snake_case")]
284 pub enum StructType {
290 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
292 #[serde(rename = "const")]
294 #[serde(rename = "unsafe")]
296 #[serde(rename = "async")]
301 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
303 // We only have a concrete listing here for stable ABI's because their are so many
304 // See rustc_ast_passes::feature_gate::PostExpansionVisitor::check_abi for the list
307 Cdecl { unwind: bool },
308 Stdcall { unwind: bool },
309 Fastcall { unwind: bool },
310 Aapcs { unwind: bool },
311 Win64 { unwind: bool },
312 SysV64 { unwind: bool },
313 System { unwind: bool },
317 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
318 pub struct Function {
320 pub generics: Generics,
324 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
327 pub generics: Generics,
332 #[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
333 pub struct Generics {
334 pub params: Vec<GenericParamDef>,
335 pub where_predicates: Vec<WherePredicate>,
338 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
339 pub struct GenericParamDef {
341 pub kind: GenericParamDefKind,
344 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
345 #[serde(rename_all = "snake_case")]
346 pub enum GenericParamDefKind {
347 Lifetime { outlives: Vec<String> },
348 Type { bounds: Vec<GenericBound>, default: Option<Type> },
349 Const { ty: Type, default: Option<String> },
352 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
353 #[serde(rename_all = "snake_case")]
354 pub enum WherePredicate {
355 BoundPredicate { ty: Type, bounds: Vec<GenericBound> },
356 RegionPredicate { lifetime: String, bounds: Vec<GenericBound> },
357 EqPredicate { lhs: Type, rhs: Term },
360 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
361 #[serde(rename_all = "snake_case")]
362 pub enum GenericBound {
364 #[serde(rename = "trait")]
367 generic_params: Vec<GenericParamDef>,
368 modifier: TraitBoundModifier,
373 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
374 #[serde(rename_all = "snake_case")]
375 pub enum TraitBoundModifier {
381 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
382 #[serde(rename_all = "snake_case")]
388 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
389 #[serde(rename_all = "snake_case")]
390 #[serde(tag = "kind", content = "inner")]
392 /// Structs, enums, and traits
396 args: Option<Box<GenericArgs>>,
397 param_names: Vec<GenericBound>,
399 /// Parameterized types
401 /// Fixed-size numeric types (plus int/usize/float), char, arrays, slices, and tuples
403 /// `extern "ABI" fn`
404 FunctionPointer(Box<FunctionPointer>),
405 /// `(String, u32, Box<usize>)`
411 #[serde(rename = "type")]
415 /// `impl TraitA + TraitB + ...`
416 ImplTrait(Vec<GenericBound>),
419 /// `*mut u32`, `*u8`, etc.
422 #[serde(rename = "type")]
425 /// `&'a mut String`, `&str`, etc.
427 lifetime: Option<String>,
429 #[serde(rename = "type")]
432 /// `<Type as Trait>::Name` or associated types like `T::Item` where `T: Iterator`
435 self_type: Box<Type>,
436 #[serde(rename = "trait")]
441 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
442 pub struct FunctionPointer {
444 pub generic_params: Vec<GenericParamDef>,
448 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
450 pub inputs: Vec<(String, Type)>,
451 pub output: Option<Type>,
452 pub c_variadic: bool,
455 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
460 pub generics: Generics,
461 pub bounds: Vec<GenericBound>,
462 pub implementors: Vec<Id>,
465 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
466 pub struct TraitAlias {
467 pub generics: Generics,
468 pub params: Vec<GenericBound>,
471 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
474 pub generics: Generics,
475 pub provided_trait_methods: Vec<String>,
476 #[serde(rename = "trait")]
477 pub trait_: Option<Type>,
478 #[serde(rename = "for")]
483 pub blanket_impl: Option<Type>,
486 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
487 #[serde(rename_all = "snake_case")]
489 /// The full path being imported.
491 /// May be different from the last segment of `source` when renaming imports:
492 /// `use source as name;`
494 /// The ID of the item being imported.
495 pub id: Option<Id>, // FIXME is this actually ever None?
496 /// Whether this import uses a glob: `use source::*;`
500 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
501 pub struct ProcMacro {
503 pub helpers: Vec<String>,
506 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
507 #[serde(rename_all = "snake_case")]
509 /// A bang macro `foo!()`.
511 /// An attribute macro `#[foo]`.
513 /// A derive macro `#[derive(Foo)]`
517 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
519 #[serde(rename = "type")]
521 pub generics: Generics,
524 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
525 pub struct OpaqueTy {
526 pub bounds: Vec<GenericBound>,
527 pub generics: Generics,
530 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
532 #[serde(rename = "type")]