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::path::PathBuf;
8 use rustc_data_structures::fx::FxHashMap;
9 use serde::{Deserialize, Serialize};
11 /// A `Crate` is the root of the emitted JSON blob. It contains all type/documentation information
12 /// about the language items in the local crate, as well as info about external items to allow
13 /// tools to find or link to them.
14 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
16 /// The id of the root [`Module`] item of the local crate.
18 /// The version string given to `--crate-version`, if any.
19 pub crate_version: Option<String>,
20 /// Whether or not the output includes private items.
21 pub includes_private: bool,
22 /// A collection of all items in the local crate as well as some external traits and their
23 /// items that are referenced locally.
24 pub index: FxHashMap<Id, Item>,
25 /// Maps IDs to fully qualified paths and other info helpful for generating links.
26 pub paths: FxHashMap<Id, ItemSummary>,
27 /// Maps `crate_id` of items to a crate name and html_root_url if it exists.
28 pub external_crates: FxHashMap<u32, ExternalCrate>,
29 /// A single version number to be used in the future when making backwards incompatible changes
30 /// to the JSON output.
31 pub format_version: u32,
34 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
35 pub struct ExternalCrate {
37 pub html_root_url: Option<String>,
40 /// For external (not defined in the local crate) items, you don't get the same level of
41 /// information. This struct should contain enough to generate a link/reference to the item in
42 /// question, or can be used by a tool that takes the json output of multiple crates to find
43 /// the actual item definition with all the relevant info.
44 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
45 pub struct ItemSummary {
46 /// Can be used to look up the name and html_root_url of the crate this item came from in the
47 /// `external_crates` map.
49 /// The list of path components for the fully qualified path of this item (e.g.
50 /// `["std", "io", "lazy", "Lazy"]` for `std::io::lazy::Lazy`).
51 pub path: Vec<String>,
52 /// Whether this item is a struct, trait, macro, etc.
56 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
58 /// The unique identifier of this item. Can be used to find this item in various mappings.
60 /// This can be used as a key to the `external_crates` map of [`Crate`] to see which crate
61 /// this item came from.
63 /// Some items such as impls don't have names.
64 pub name: Option<String>,
65 /// The source location of this item (absent if it came from a macro expansion or inline
67 pub source: Option<Span>,
68 /// By default all documented items are public, but you can tell rustdoc to output private items
69 /// so this field is needed to differentiate.
70 pub visibility: Visibility,
71 /// The full markdown docstring of this item.
73 /// 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
74 pub links: FxHashMap<String, Id>,
75 /// Stringified versions of the attributes on this item (e.g. `"#[inline]"`)
76 pub attrs: Vec<String>,
77 pub deprecation: Option<Deprecation>,
82 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
84 /// The path to the source file for this span relative to the path `rustdoc` was invoked with.
85 pub filename: PathBuf,
86 /// Zero indexed Line and Column of the first character of the `Span`
87 pub begin: (usize, usize),
88 /// Zero indexed Line and Column of the last character of the `Span`
89 pub end: (usize, usize),
92 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
93 pub struct Deprecation {
94 pub since: Option<String>,
95 pub note: Option<String>,
98 #[serde(rename_all = "snake_case")]
99 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
100 pub enum Visibility {
102 /// For the most part items are private by default. The exceptions are associated items of
103 /// public traits and variants of public enums.
106 /// For `pub(in path)` visibility. `parent` is the module it's restricted to and `path` is how
107 /// that module was referenced (like `"super::super"` or `"crate::foo::bar"`).
114 #[serde(rename_all = "snake_case")]
115 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
116 pub enum GenericArgs {
117 /// <'a, 32, B: Copy, C = u32>
118 AngleBracketed { args: Vec<GenericArg>, bindings: Vec<TypeBinding> },
120 Parenthesized { inputs: Vec<Type>, output: Option<Type> },
123 #[serde(rename_all = "snake_case")]
124 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
125 pub enum GenericArg {
131 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
132 pub struct Constant {
133 #[serde(rename = "type")]
136 pub value: Option<String>,
137 pub is_literal: bool,
140 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
141 pub struct TypeBinding {
143 pub binding: TypeBindingKind,
146 #[serde(rename_all = "snake_case")]
147 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
148 pub enum TypeBindingKind {
150 Constraint(Vec<GenericBound>),
153 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
154 pub struct Id(pub String);
156 #[serde(rename_all = "snake_case")]
157 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
187 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
192 rename: Option<String>,
197 StructFieldItem(Type),
199 VariantItem(Variant),
201 FunctionItem(Function),
204 TraitAliasItem(TraitAlias),
208 TypedefItem(Typedef),
209 OpaqueTyItem(OpaqueTy),
210 ConstantItem(Constant),
214 /// `type`s from an extern block
217 /// Declarative macro_rules! macro
219 ProcMacroItem(ProcMacro),
222 #[serde(rename = "type")]
224 /// e.g. `const X: usize = 5;`
225 default: Option<String>,
228 bounds: Vec<GenericBound>,
229 /// e.g. `type X = usize;`
230 default: Option<Type>,
234 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
240 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
242 pub struct_type: StructType,
243 pub generics: Generics,
244 pub fields_stripped: bool,
249 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
251 pub generics: Generics,
252 pub variants_stripped: bool,
253 pub variants: Vec<Id>,
257 #[serde(rename_all = "snake_case")]
258 #[serde(tag = "variant_kind", content = "variant_inner")]
259 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
266 #[serde(rename_all = "snake_case")]
267 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
268 pub enum StructType {
274 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
275 pub struct Function {
277 pub generics: Generics,
282 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
285 pub generics: Generics,
290 #[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
291 pub struct Generics {
292 pub params: Vec<GenericParamDef>,
293 pub where_predicates: Vec<WherePredicate>,
296 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
297 pub struct GenericParamDef {
299 pub kind: GenericParamDefKind,
302 #[serde(rename_all = "snake_case")]
303 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
304 pub enum GenericParamDefKind {
306 Type { bounds: Vec<GenericBound>, default: Option<Type> },
310 #[serde(rename_all = "snake_case")]
311 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
312 pub enum WherePredicate {
313 BoundPredicate { ty: Type, bounds: Vec<GenericBound> },
314 RegionPredicate { lifetime: String, bounds: Vec<GenericBound> },
315 EqPredicate { lhs: Type, rhs: Type },
318 #[serde(rename_all = "snake_case")]
319 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
320 pub enum GenericBound {
322 #[serde(rename = "trait")]
325 generic_params: Vec<GenericParamDef>,
326 modifier: TraitBoundModifier,
331 #[serde(rename_all = "snake_case")]
332 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
333 pub enum TraitBoundModifier {
339 #[serde(rename_all = "snake_case")]
340 #[serde(tag = "kind", content = "inner")]
341 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
343 /// Structs, enums, and traits
347 args: Option<Box<GenericArgs>>,
348 param_names: Vec<GenericBound>,
350 /// Parameterized types
352 /// Fixed-size numeric types (plus int/usize/float), char, arrays, slices, and tuples
354 /// `extern "ABI" fn`
355 FunctionPointer(Box<FunctionPointer>),
356 /// `(String, u32, Box<usize>)`
362 #[serde(rename = "type")]
366 /// `impl TraitA + TraitB + ...`
367 ImplTrait(Vec<GenericBound>),
372 /// `*mut u32`, `*u8`, etc.
375 #[serde(rename = "type")]
378 /// `&'a mut String`, `&str`, etc.
380 lifetime: Option<String>,
382 #[serde(rename = "type")]
385 /// `<Type as Trait>::Name` or associated types like `T::Item` where `T: Iterator`
388 self_type: Box<Type>,
389 #[serde(rename = "trait")]
394 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
395 pub struct FunctionPointer {
397 pub generic_params: Vec<GenericParamDef>,
402 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
404 pub inputs: Vec<(String, Type)>,
405 pub output: Option<Type>,
406 pub c_variadic: bool,
409 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
414 pub generics: Generics,
415 pub bounds: Vec<GenericBound>,
416 pub implementors: Vec<Id>,
419 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
420 pub struct TraitAlias {
421 pub generics: Generics,
422 pub params: Vec<GenericBound>,
425 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
428 pub generics: Generics,
429 pub provided_trait_methods: Vec<String>,
430 #[serde(rename = "trait")]
431 pub trait_: Option<Type>,
432 #[serde(rename = "for")]
437 pub blanket_impl: Option<Type>,
440 #[serde(rename_all = "snake_case")]
441 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
443 /// The full path being imported.
445 /// May be different from the last segment of `source` when renaming imports:
446 /// `use source as name;`
448 /// The ID of the item being imported.
449 pub id: Option<Id>, // FIXME is this actually ever None?
450 /// Whether this import uses a glob: `use source::*;`
454 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
455 pub struct ProcMacro {
457 pub helpers: Vec<String>,
460 #[serde(rename_all = "snake_case")]
461 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
463 /// A bang macro `foo!()`.
465 /// An attribute macro `#[foo]`.
467 /// A derive macro `#[derive(Foo)]`
471 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
473 #[serde(rename = "type")]
475 pub generics: Generics,
478 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
479 pub struct OpaqueTy {
480 pub bounds: Vec<GenericBound>,
481 pub generics: Generics,
484 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
486 #[serde(rename = "type")]