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, HashSet};
7 use std::path::PathBuf;
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: HashMap<Id, Item>,
25 /// Maps IDs to fully qualified paths and other info helpful for generating links.
26 pub paths: HashMap<Id, ItemSummary>,
27 /// Maps `crate_id` of items to a crate name and html_root_url if it exists.
28 pub external_crates: HashMap<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: HashMap<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 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
100 #[serde(rename_all = "snake_case")]
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 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
116 #[serde(rename_all = "snake_case")]
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 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
125 #[serde(rename_all = "snake_case")]
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 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
148 #[serde(rename_all = "snake_case")]
149 pub enum TypeBindingKind {
151 Constraint(Vec<GenericBound>),
154 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
155 pub struct Id(pub String);
157 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
158 #[serde(rename_all = "snake_case")]
187 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
188 #[serde(tag = "kind", content = "inner", rename_all = "snake_case")]
193 rename: Option<String>,
206 TraitAlias(TraitAlias),
216 /// `type`s from an extern block
219 /// Declarative macro_rules! macro
221 ProcMacro(ProcMacro),
224 #[serde(rename = "type")]
226 /// e.g. `const X: usize = 5;`
227 default: Option<String>,
230 bounds: Vec<GenericBound>,
231 /// e.g. `type X = usize;`
232 default: Option<Type>,
236 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
242 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
244 pub generics: Generics,
245 pub fields_stripped: bool,
250 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
252 pub struct_type: StructType,
253 pub generics: Generics,
254 pub fields_stripped: bool,
259 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
261 pub generics: Generics,
262 pub variants_stripped: bool,
263 pub variants: Vec<Id>,
267 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
268 #[serde(rename_all = "snake_case")]
269 #[serde(tag = "variant_kind", content = "variant_inner")]
276 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
277 #[serde(rename_all = "snake_case")]
278 pub enum StructType {
285 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
286 #[serde(rename_all = "snake_case")]
287 pub enum Qualifiers {
293 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
294 pub struct Function {
296 pub generics: Generics,
297 pub header: HashSet<Qualifiers>,
301 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
304 pub generics: Generics,
305 pub header: HashSet<Qualifiers>,
310 #[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
311 pub struct Generics {
312 pub params: Vec<GenericParamDef>,
313 pub where_predicates: Vec<WherePredicate>,
316 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
317 pub struct GenericParamDef {
319 pub kind: GenericParamDefKind,
322 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
323 #[serde(rename_all = "snake_case")]
324 pub enum GenericParamDefKind {
326 Type { bounds: Vec<GenericBound>, default: Option<Type> },
330 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
331 #[serde(rename_all = "snake_case")]
332 pub enum WherePredicate {
333 BoundPredicate { ty: Type, bounds: Vec<GenericBound> },
334 RegionPredicate { lifetime: String, bounds: Vec<GenericBound> },
335 EqPredicate { lhs: Type, rhs: Type },
338 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
339 #[serde(rename_all = "snake_case")]
340 pub enum GenericBound {
342 #[serde(rename = "trait")]
345 generic_params: Vec<GenericParamDef>,
346 modifier: TraitBoundModifier,
351 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
352 #[serde(rename_all = "snake_case")]
353 pub enum TraitBoundModifier {
359 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
360 #[serde(rename_all = "snake_case")]
361 #[serde(tag = "kind", content = "inner")]
363 /// Structs, enums, and traits
367 args: Option<Box<GenericArgs>>,
368 param_names: Vec<GenericBound>,
370 /// Parameterized types
372 /// Fixed-size numeric types (plus int/usize/float), char, arrays, slices, and tuples
374 /// `extern "ABI" fn`
375 FunctionPointer(Box<FunctionPointer>),
376 /// `(String, u32, Box<usize>)`
382 #[serde(rename = "type")]
386 /// `impl TraitA + TraitB + ...`
387 ImplTrait(Vec<GenericBound>),
392 /// `*mut u32`, `*u8`, etc.
395 #[serde(rename = "type")]
398 /// `&'a mut String`, `&str`, etc.
400 lifetime: Option<String>,
402 #[serde(rename = "type")]
405 /// `<Type as Trait>::Name` or associated types like `T::Item` where `T: Iterator`
408 self_type: Box<Type>,
409 #[serde(rename = "trait")]
414 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
415 pub struct FunctionPointer {
417 pub generic_params: Vec<GenericParamDef>,
418 pub header: HashSet<Qualifiers>,
422 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
424 pub inputs: Vec<(String, Type)>,
425 pub output: Option<Type>,
426 pub c_variadic: bool,
429 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
434 pub generics: Generics,
435 pub bounds: Vec<GenericBound>,
436 pub implementors: Vec<Id>,
439 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
440 pub struct TraitAlias {
441 pub generics: Generics,
442 pub params: Vec<GenericBound>,
445 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
448 pub generics: Generics,
449 pub provided_trait_methods: Vec<String>,
450 #[serde(rename = "trait")]
451 pub trait_: Option<Type>,
452 #[serde(rename = "for")]
457 pub blanket_impl: Option<Type>,
460 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
461 #[serde(rename_all = "snake_case")]
463 /// The full path being imported.
465 /// May be different from the last segment of `source` when renaming imports:
466 /// `use source as name;`
468 /// The ID of the item being imported.
469 pub id: Option<Id>, // FIXME is this actually ever None?
470 /// Whether this import uses a glob: `use source::*;`
474 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
475 pub struct ProcMacro {
477 pub helpers: Vec<String>,
480 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
481 #[serde(rename_all = "snake_case")]
483 /// A bang macro `foo!()`.
485 /// An attribute macro `#[foo]`.
487 /// A derive macro `#[derive(Foo)]`
491 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
493 #[serde(rename = "type")]
495 pub generics: Generics,
498 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
499 pub struct OpaqueTy {
500 pub bounds: Vec<GenericBound>,
501 pub generics: Generics,
504 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
506 #[serde(rename = "type")]