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. Absent if there is no documentation at all,
72 /// Some("") if there is some documentation but it is empty (EG `#[doc = ""]`).
73 pub docs: Option<String>,
74 /// 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
75 pub links: FxHashMap<String, Id>,
76 /// Stringified versions of the attributes on this item (e.g. `"#[inline]"`)
77 pub attrs: Vec<String>,
78 pub deprecation: Option<Deprecation>,
83 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
85 /// The path to the source file for this span relative to the path `rustdoc` was invoked with.
86 pub filename: PathBuf,
87 /// Zero indexed Line and Column of the first character of the `Span`
88 pub begin: (usize, usize),
89 /// Zero indexed Line and Column of the last character of the `Span`
90 pub end: (usize, usize),
93 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
94 pub struct Deprecation {
95 pub since: Option<String>,
96 pub note: Option<String>,
99 #[serde(rename_all = "snake_case")]
100 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
101 pub enum Visibility {
103 /// For the most part items are private by default. The exceptions are associated items of
104 /// public traits and variants of public enums.
107 /// For `pub(in path)` visibility. `parent` is the module it's restricted to and `path` is how
108 /// that module was referenced (like `"super::super"` or `"crate::foo::bar"`).
115 #[serde(rename_all = "snake_case")]
116 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
117 pub enum GenericArgs {
118 /// <'a, 32, B: Copy, C = u32>
119 AngleBracketed { args: Vec<GenericArg>, bindings: Vec<TypeBinding> },
121 Parenthesized { inputs: Vec<Type>, output: Option<Type> },
124 #[serde(rename_all = "snake_case")]
125 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
126 pub enum GenericArg {
132 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
133 pub struct Constant {
134 #[serde(rename = "type")]
137 pub value: Option<String>,
138 pub is_literal: bool,
141 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
142 pub struct TypeBinding {
144 pub binding: TypeBindingKind,
147 #[serde(rename_all = "snake_case")]
148 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
149 pub enum TypeBindingKind {
151 Constraint(Vec<GenericBound>),
154 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
155 pub struct Id(pub String);
157 #[serde(rename_all = "snake_case")]
158 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
188 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
193 rename: Option<String>,
198 StructFieldItem(Type),
200 VariantItem(Variant),
202 FunctionItem(Function),
205 TraitAliasItem(TraitAlias),
209 TypedefItem(Typedef),
210 OpaqueTyItem(OpaqueTy),
211 ConstantItem(Constant),
215 /// `type`s from an extern block
218 /// Declarative macro_rules! macro
220 ProcMacroItem(ProcMacro),
223 #[serde(rename = "type")]
225 /// e.g. `const X: usize = 5;`
226 default: Option<String>,
229 bounds: Vec<GenericBound>,
230 /// e.g. `type X = usize;`
231 default: Option<Type>,
235 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
241 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
243 pub struct_type: StructType,
244 pub generics: Generics,
245 pub fields_stripped: bool,
250 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
252 pub generics: Generics,
253 pub variants_stripped: bool,
254 pub variants: Vec<Id>,
258 #[serde(rename_all = "snake_case")]
259 #[serde(tag = "variant_kind", content = "variant_inner")]
260 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
267 #[serde(rename_all = "snake_case")]
268 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
269 pub enum StructType {
276 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
277 pub struct Function {
279 pub generics: Generics,
284 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
287 pub generics: Generics,
292 #[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
293 pub struct Generics {
294 pub params: Vec<GenericParamDef>,
295 pub where_predicates: Vec<WherePredicate>,
298 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
299 pub struct GenericParamDef {
301 pub kind: GenericParamDefKind,
304 #[serde(rename_all = "snake_case")]
305 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
306 pub enum GenericParamDefKind {
308 Type { bounds: Vec<GenericBound>, default: Option<Type> },
312 #[serde(rename_all = "snake_case")]
313 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
314 pub enum WherePredicate {
315 BoundPredicate { ty: Type, bounds: Vec<GenericBound> },
316 RegionPredicate { lifetime: String, bounds: Vec<GenericBound> },
317 EqPredicate { lhs: Type, rhs: Type },
320 #[serde(rename_all = "snake_case")]
321 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
322 pub enum GenericBound {
324 #[serde(rename = "trait")]
327 generic_params: Vec<GenericParamDef>,
328 modifier: TraitBoundModifier,
333 #[serde(rename_all = "snake_case")]
334 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
335 pub enum TraitBoundModifier {
341 #[serde(rename_all = "snake_case")]
342 #[serde(tag = "kind", content = "inner")]
343 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
345 /// Structs, enums, and traits
349 args: Option<Box<GenericArgs>>,
350 param_names: Vec<GenericBound>,
352 /// Parameterized types
354 /// Fixed-size numeric types (plus int/usize/float), char, arrays, slices, and tuples
356 /// `extern "ABI" fn`
357 FunctionPointer(Box<FunctionPointer>),
358 /// `(String, u32, Box<usize>)`
364 #[serde(rename = "type")]
368 /// `impl TraitA + TraitB + ...`
369 ImplTrait(Vec<GenericBound>),
374 /// `*mut u32`, `*u8`, etc.
377 #[serde(rename = "type")]
380 /// `&'a mut String`, `&str`, etc.
382 lifetime: Option<String>,
384 #[serde(rename = "type")]
387 /// `<Type as Trait>::Name` or associated types like `T::Item` where `T: Iterator`
390 self_type: Box<Type>,
391 #[serde(rename = "trait")]
396 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
397 pub struct FunctionPointer {
399 pub generic_params: Vec<GenericParamDef>,
404 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
406 pub inputs: Vec<(String, Type)>,
407 pub output: Option<Type>,
408 pub c_variadic: bool,
411 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
416 pub generics: Generics,
417 pub bounds: Vec<GenericBound>,
418 pub implementors: Vec<Id>,
421 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
422 pub struct TraitAlias {
423 pub generics: Generics,
424 pub params: Vec<GenericBound>,
427 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
430 pub generics: Generics,
431 pub provided_trait_methods: Vec<String>,
432 #[serde(rename = "trait")]
433 pub trait_: Option<Type>,
434 #[serde(rename = "for")]
439 pub blanket_impl: Option<Type>,
442 #[serde(rename_all = "snake_case")]
443 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
445 /// The full path being imported.
447 /// May be different from the last segment of `source` when renaming imports:
448 /// `use source as name;`
450 /// The ID of the item being imported.
451 pub id: Option<Id>, // FIXME is this actually ever None?
452 /// Whether this import uses a glob: `use source::*;`
456 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
457 pub struct ProcMacro {
459 pub helpers: Vec<String>,
462 #[serde(rename_all = "snake_case")]
463 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
465 /// A bang macro `foo!()`.
467 /// An attribute macro `#[foo]`.
469 /// A derive macro `#[derive(Foo)]`
473 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
475 #[serde(rename = "type")]
477 pub generics: Generics,
480 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
481 pub struct OpaqueTy {
482 pub bounds: Vec<GenericBound>,
483 pub generics: Generics,
486 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
488 #[serde(rename = "type")]