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 span: 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 {
133 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
134 pub struct Constant {
135 #[serde(rename = "type")]
138 pub value: Option<String>,
139 pub is_literal: bool,
142 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
143 pub struct TypeBinding {
145 pub binding: TypeBindingKind,
148 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
149 #[serde(rename_all = "snake_case")]
150 pub enum TypeBindingKind {
152 Constraint(Vec<GenericBound>),
155 #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
156 pub struct Id(pub String);
158 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
159 #[serde(rename_all = "snake_case")]
188 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
189 #[serde(tag = "kind", content = "inner", rename_all = "snake_case")]
194 rename: Option<String>,
207 TraitAlias(TraitAlias),
217 /// `type`s from an extern block
220 /// Declarative macro_rules! macro
222 ProcMacro(ProcMacro),
225 #[serde(rename = "type")]
227 /// e.g. `const X: usize = 5;`
228 default: Option<String>,
231 bounds: Vec<GenericBound>,
232 /// e.g. `type X = usize;`
233 default: Option<Type>,
237 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
243 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
245 pub generics: Generics,
246 pub fields_stripped: bool,
251 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
253 pub struct_type: StructType,
254 pub generics: Generics,
255 pub fields_stripped: bool,
260 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
262 pub generics: Generics,
263 pub variants_stripped: bool,
264 pub variants: Vec<Id>,
268 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
269 #[serde(rename_all = "snake_case")]
270 #[serde(tag = "variant_kind", content = "variant_inner")]
277 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
278 #[serde(rename_all = "snake_case")]
279 pub enum StructType {
286 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
287 #[serde(rename_all = "snake_case")]
288 pub enum Qualifiers {
294 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
295 pub struct Function {
297 pub generics: Generics,
298 pub header: HashSet<Qualifiers>,
302 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
305 pub generics: Generics,
306 pub header: HashSet<Qualifiers>,
311 #[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
312 pub struct Generics {
313 pub params: Vec<GenericParamDef>,
314 pub where_predicates: Vec<WherePredicate>,
317 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
318 pub struct GenericParamDef {
320 pub kind: GenericParamDefKind,
323 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
324 #[serde(rename_all = "snake_case")]
325 pub enum GenericParamDefKind {
326 Lifetime { outlives: Vec<String> },
327 Type { bounds: Vec<GenericBound>, default: Option<Type> },
328 Const { ty: Type, default: Option<String> },
331 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
332 #[serde(rename_all = "snake_case")]
333 pub enum WherePredicate {
334 BoundPredicate { ty: Type, bounds: Vec<GenericBound> },
335 RegionPredicate { lifetime: String, bounds: Vec<GenericBound> },
336 EqPredicate { lhs: Type, rhs: Type },
339 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
340 #[serde(rename_all = "snake_case")]
341 pub enum GenericBound {
343 #[serde(rename = "trait")]
346 generic_params: Vec<GenericParamDef>,
347 modifier: TraitBoundModifier,
352 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
353 #[serde(rename_all = "snake_case")]
354 pub enum TraitBoundModifier {
360 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
361 #[serde(rename_all = "snake_case")]
362 #[serde(tag = "kind", content = "inner")]
364 /// Structs, enums, and traits
368 args: Option<Box<GenericArgs>>,
369 param_names: Vec<GenericBound>,
371 /// Parameterized types
373 /// Fixed-size numeric types (plus int/usize/float), char, arrays, slices, and tuples
375 /// `extern "ABI" fn`
376 FunctionPointer(Box<FunctionPointer>),
377 /// `(String, u32, Box<usize>)`
383 #[serde(rename = "type")]
387 /// `impl TraitA + TraitB + ...`
388 ImplTrait(Vec<GenericBound>),
393 /// `*mut u32`, `*u8`, etc.
396 #[serde(rename = "type")]
399 /// `&'a mut String`, `&str`, etc.
401 lifetime: Option<String>,
403 #[serde(rename = "type")]
406 /// `<Type as Trait>::Name` or associated types like `T::Item` where `T: Iterator`
409 self_type: Box<Type>,
410 #[serde(rename = "trait")]
415 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
416 pub struct FunctionPointer {
418 pub generic_params: Vec<GenericParamDef>,
419 pub header: HashSet<Qualifiers>,
423 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
425 pub inputs: Vec<(String, Type)>,
426 pub output: Option<Type>,
427 pub c_variadic: bool,
430 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
435 pub generics: Generics,
436 pub bounds: Vec<GenericBound>,
437 pub implementors: Vec<Id>,
440 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
441 pub struct TraitAlias {
442 pub generics: Generics,
443 pub params: Vec<GenericBound>,
446 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
449 pub generics: Generics,
450 pub provided_trait_methods: Vec<String>,
451 #[serde(rename = "trait")]
452 pub trait_: Option<Type>,
453 #[serde(rename = "for")]
458 pub blanket_impl: Option<Type>,
461 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
462 #[serde(rename_all = "snake_case")]
464 /// The full path being imported.
466 /// May be different from the last segment of `source` when renaming imports:
467 /// `use source as name;`
469 /// The ID of the item being imported.
470 pub id: Option<Id>, // FIXME is this actually ever None?
471 /// Whether this import uses a glob: `use source::*;`
475 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
476 pub struct ProcMacro {
478 pub helpers: Vec<String>,
481 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
482 #[serde(rename_all = "snake_case")]
484 /// A bang macro `foo!()`.
486 /// An attribute macro `#[foo]`.
488 /// A derive macro `#[derive(Foo)]`
492 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
494 #[serde(rename = "type")]
496 pub generics: Generics,
499 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
500 pub struct OpaqueTy {
501 pub bounds: Vec<GenericBound>,
502 pub generics: Generics,
505 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
507 #[serde(rename = "type")]