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 /// 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)]
193 rename: Option<String>,
199 StructFieldItem(Type),
201 VariantItem(Variant),
203 FunctionItem(Function),
206 TraitAliasItem(TraitAlias),
210 TypedefItem(Typedef),
211 OpaqueTyItem(OpaqueTy),
212 ConstantItem(Constant),
216 /// `type`s from an extern block
219 /// Declarative macro_rules! macro
221 ProcMacroItem(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 {
284 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
285 pub struct Function {
287 pub generics: Generics,
292 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
295 pub generics: Generics,
301 #[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
302 pub struct Generics {
303 pub params: Vec<GenericParamDef>,
304 pub where_predicates: Vec<WherePredicate>,
307 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
308 pub struct GenericParamDef {
310 pub kind: GenericParamDefKind,
313 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
314 #[serde(rename_all = "snake_case")]
315 pub enum GenericParamDefKind {
317 Type { bounds: Vec<GenericBound>, default: Option<Type> },
321 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
322 #[serde(rename_all = "snake_case")]
323 pub enum WherePredicate {
324 BoundPredicate { ty: Type, bounds: Vec<GenericBound> },
325 RegionPredicate { lifetime: String, bounds: Vec<GenericBound> },
326 EqPredicate { lhs: Type, rhs: Type },
329 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
330 #[serde(rename_all = "snake_case")]
331 pub enum GenericBound {
333 #[serde(rename = "trait")]
336 generic_params: Vec<GenericParamDef>,
337 modifier: TraitBoundModifier,
342 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
343 #[serde(rename_all = "snake_case")]
344 pub enum TraitBoundModifier {
350 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
351 #[serde(rename_all = "snake_case")]
352 #[serde(tag = "kind", content = "inner")]
354 /// Structs, enums, and traits
358 args: Option<Box<GenericArgs>>,
359 param_names: Vec<GenericBound>,
361 /// Parameterized types
363 /// Fixed-size numeric types (plus int/usize/float), char, arrays, slices, and tuples
365 /// `extern "ABI" fn`
366 FunctionPointer(Box<FunctionPointer>),
367 /// `(String, u32, Box<usize>)`
373 #[serde(rename = "type")]
377 /// `impl TraitA + TraitB + ...`
378 ImplTrait(Vec<GenericBound>),
383 /// `*mut u32`, `*u8`, etc.
386 #[serde(rename = "type")]
389 /// `&'a mut String`, `&str`, etc.
391 lifetime: Option<String>,
393 #[serde(rename = "type")]
396 /// `<Type as Trait>::Name` or associated types like `T::Item` where `T: Iterator`
399 self_type: Box<Type>,
400 #[serde(rename = "trait")]
405 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
406 pub struct FunctionPointer {
408 pub generic_params: Vec<GenericParamDef>,
413 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
415 pub inputs: Vec<(String, Type)>,
416 pub output: Option<Type>,
417 pub c_variadic: bool,
420 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
425 pub generics: Generics,
426 pub bounds: Vec<GenericBound>,
427 pub implementors: Vec<Id>,
430 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
431 pub struct TraitAlias {
432 pub generics: Generics,
433 pub params: Vec<GenericBound>,
436 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
439 pub generics: Generics,
440 pub provided_trait_methods: Vec<String>,
441 #[serde(rename = "trait")]
442 pub trait_: Option<Type>,
443 #[serde(rename = "for")]
448 pub blanket_impl: Option<Type>,
451 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
452 #[serde(rename_all = "snake_case")]
454 /// The full path being imported.
456 /// May be different from the last segment of `source` when renaming imports:
457 /// `use source as name;`
459 /// The ID of the item being imported.
460 pub id: Option<Id>, // FIXME is this actually ever None?
461 /// Whether this import uses a glob: `use source::*;`
465 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
466 pub struct ProcMacro {
468 pub helpers: Vec<String>,
471 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
472 #[serde(rename_all = "snake_case")]
474 /// A bang macro `foo!()`.
476 /// An attribute macro `#[foo]`.
478 /// A derive macro `#[derive(Foo)]`
482 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
484 #[serde(rename = "type")]
486 pub generics: Generics,
489 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
490 pub struct OpaqueTy {
491 pub bounds: Vec<GenericBound>,
492 pub generics: Generics,
495 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
497 #[serde(rename = "type")]