Rollup merge of #81287 - CraftSpider:json-crate, r=jyn514,GuillaumeGomez

Split rustdoc JSON types into separately versioned crate

For now just an in-tree change.

In the future, this may be exposed as a standalone crate with standard semver.
This commit is contained in:
Yuki Okushi 2021-01-29 09:17:34 +09:00 committed by GitHub
commit 788036df28
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 88 additions and 59 deletions

View File

@ -4392,12 +4392,20 @@ dependencies = [
"pulldown-cmark 0.8.0",
"regex",
"rustc-rayon",
"rustdoc-json-types",
"serde",
"serde_json",
"smallvec 1.4.2",
"tempfile",
]
[[package]]
name = "rustdoc-json-types"
version = "0.1.0"
dependencies = [
"serde",
]
[[package]]
name = "rustdoc-themes"
version = "0.1.0"

View File

@ -4,6 +4,7 @@ members = [
"compiler/rustc",
"library/std",
"library/test",
"src/rustdoc-json-types",
"src/tools/cargotest",
"src/tools/clippy",
"src/tools/compiletest",

View File

@ -17,6 +17,7 @@ smallvec = "1.0"
tempfile = "3"
itertools = "0.9"
regex = "1"
rustdoc-json-types = { path = "../rustdoc-json-types" }
[dev-dependencies]
expect-test = "1.0"

View File

@ -9,9 +9,10 @@ use rustc_hir::def::CtorKind;
use rustc_span::def_id::{DefId, CRATE_DEF_INDEX};
use rustc_span::Pos;
use rustdoc_json_types::*;
use crate::clean;
use crate::formats::item_type::ItemType;
use crate::json::types::*;
use crate::json::JsonRenderer;
impl JsonRenderer<'_> {
@ -22,7 +23,7 @@ impl JsonRenderer<'_> {
match *kind {
clean::StrippedItem(_) => None,
kind => Some(Item {
id: def_id.into(),
id: from_def_id(def_id),
crate_id: def_id.krate.as_u32(),
name: name.map(|sym| sym.to_string()),
source: self.convert_span(source),
@ -32,7 +33,7 @@ impl JsonRenderer<'_> {
.links
.into_iter()
.filter_map(|clean::ItemLink { link, did, .. }| {
did.map(|did| (link, did.into()))
did.map(|did| (link, from_def_id(did)))
})
.collect(),
attrs: attrs
@ -40,7 +41,7 @@ impl JsonRenderer<'_> {
.iter()
.map(rustc_ast_pretty::pprust::attribute_to_string)
.collect(),
deprecation: deprecation.map(Into::into),
deprecation: deprecation.map(from_deprecation),
kind: item_type.into(),
inner: kind.into(),
}),
@ -74,19 +75,17 @@ impl JsonRenderer<'_> {
Inherited => Visibility::Default,
Restricted(did) if did.index == CRATE_DEF_INDEX => Visibility::Crate,
Restricted(did) => Visibility::Restricted {
parent: did.into(),
parent: from_def_id(did),
path: self.tcx.def_path(did).to_string_no_crate_verbose(),
},
}
}
}
impl From<rustc_attr::Deprecation> for Deprecation {
fn from(deprecation: rustc_attr::Deprecation) -> Self {
#[rustfmt::skip]
let rustc_attr::Deprecation { since, note, is_since_rustc_version: _, suggestion: _ } = deprecation;
Deprecation { since: since.map(|s| s.to_string()), note: note.map(|s| s.to_string()) }
}
crate fn from_deprecation(deprecation: rustc_attr::Deprecation) -> Deprecation {
#[rustfmt::skip]
let rustc_attr::Deprecation { since, note, is_since_rustc_version: _, suggestion: _ } = deprecation;
Deprecation { since: since.map(|s| s.to_string()), note: note.map(|s| s.to_string()) }
}
impl From<clean::GenericArgs> for GenericArgs {
@ -141,10 +140,8 @@ impl From<clean::TypeBindingKind> for TypeBindingKind {
}
}
impl From<DefId> for Id {
fn from(did: DefId) -> Self {
Id(format!("{}:{}", did.krate.as_u32(), u32::from(did.index)))
}
crate fn from_def_id(did: DefId) -> Id {
Id(format!("{}:{}", did.krate.as_u32(), u32::from(did.index)))
}
impl From<clean::ItemKind> for ItemEnum {
@ -199,7 +196,7 @@ impl From<clean::Struct> for Struct {
fn from(struct_: clean::Struct) -> Self {
let clean::Struct { struct_type, generics, fields, fields_stripped } = struct_;
Struct {
struct_type: struct_type.into(),
struct_type: from_ctor_kind(struct_type),
generics: generics.into(),
fields_stripped,
fields: ids(fields),
@ -221,13 +218,11 @@ impl From<clean::Union> for Struct {
}
}
impl From<CtorKind> for StructType {
fn from(struct_type: CtorKind) -> Self {
match struct_type {
CtorKind::Fictive => StructType::Plain,
CtorKind::Fn => StructType::Tuple,
CtorKind::Const => StructType::Unit,
}
crate fn from_ctor_kind(struct_type: CtorKind) -> StructType {
match struct_type {
CtorKind::Fictive => StructType::Plain,
CtorKind::Fn => StructType::Tuple,
CtorKind::Const => StructType::Unit,
}
}
@ -310,7 +305,7 @@ impl From<clean::GenericBound> for GenericBound {
GenericBound::TraitBound {
trait_: trait_.into(),
generic_params: generic_params.into_iter().map(Into::into).collect(),
modifier: modifier.into(),
modifier: from_trait_bound_modifier(modifier),
}
}
Outlives(lifetime) => GenericBound::Outlives(lifetime.0.to_string()),
@ -318,14 +313,12 @@ impl From<clean::GenericBound> for GenericBound {
}
}
impl From<rustc_hir::TraitBoundModifier> for TraitBoundModifier {
fn from(modifier: rustc_hir::TraitBoundModifier) -> Self {
use rustc_hir::TraitBoundModifier::*;
match modifier {
None => TraitBoundModifier::None,
Maybe => TraitBoundModifier::Maybe,
MaybeConst => TraitBoundModifier::MaybeConst,
}
crate fn from_trait_bound_modifier(modifier: rustc_hir::TraitBoundModifier) -> TraitBoundModifier {
use rustc_hir::TraitBoundModifier::*;
match modifier {
None => TraitBoundModifier::None,
Maybe => TraitBoundModifier::Maybe,
MaybeConst => TraitBoundModifier::MaybeConst,
}
}
@ -335,7 +328,7 @@ impl From<clean::Type> for Type {
match ty {
ResolvedPath { path, param_names, did, is_generic: _ } => Type::ResolvedPath {
name: path.whole_name(),
id: did.into(),
id: from_def_id(did),
args: path.segments.last().map(|args| Box::new(args.clone().args.into())),
param_names: param_names
.map(|v| v.into_iter().map(Into::into).collect())
@ -470,7 +463,7 @@ impl From<clean::VariantStruct> for Struct {
fn from(struct_: clean::VariantStruct) -> Self {
let clean::VariantStruct { struct_type, fields, fields_stripped } = struct_;
Struct {
struct_type: struct_type.into(),
struct_type: from_ctor_kind(struct_type),
generics: Default::default(),
fields_stripped,
fields: ids(fields),
@ -497,13 +490,13 @@ impl From<clean::Import> for Import {
Simple(s) => Import {
span: import.source.path.whole_name(),
name: s.to_string(),
id: import.source.did.map(Into::into),
id: import.source.did.map(from_def_id),
glob: false,
},
Glob => Import {
span: import.source.path.whole_name(),
name: import.source.path.last_name().to_string(),
id: import.source.did.map(Into::into),
id: import.source.did.map(from_def_id),
glob: true,
},
}
@ -513,20 +506,18 @@ impl From<clean::Import> for Import {
impl From<clean::ProcMacro> for ProcMacro {
fn from(mac: clean::ProcMacro) -> Self {
ProcMacro {
kind: mac.kind.into(),
kind: from_macro_kind(mac.kind),
helpers: mac.helpers.iter().map(|x| x.to_string()).collect(),
}
}
}
impl From<rustc_span::hygiene::MacroKind> for MacroKind {
fn from(kind: rustc_span::hygiene::MacroKind) -> Self {
use rustc_span::hygiene::MacroKind::*;
match kind {
Bang => MacroKind::Bang,
Attr => MacroKind::Attr,
Derive => MacroKind::Derive,
}
crate fn from_macro_kind(kind: rustc_span::hygiene::MacroKind) -> MacroKind {
use rustc_span::hygiene::MacroKind::*;
match kind {
Bang => MacroKind::Bang,
Attr => MacroKind::Attr,
Derive => MacroKind::Derive,
}
}
@ -599,5 +590,5 @@ impl From<ItemType> for ItemKind {
}
fn ids(items: impl IntoIterator<Item = clean::Item>) -> Vec<Id> {
items.into_iter().filter(|x| !x.is_stripped()).map(|i| i.def_id.into()).collect()
items.into_iter().filter(|x| !x.is_stripped()).map(|i| from_def_id(i.def_id)).collect()
}

View File

@ -5,7 +5,6 @@
//! docs for usage and details.
mod conversions;
pub mod types;
use std::cell::RefCell;
use std::fs::File;
@ -17,12 +16,15 @@ use rustc_middle::ty::TyCtxt;
use rustc_session::Session;
use rustc_span::edition::Edition;
use rustdoc_json_types as types;
use crate::clean;
use crate::config::{RenderInfo, RenderOptions};
use crate::error::Error;
use crate::formats::cache::Cache;
use crate::formats::FormatRenderer;
use crate::html::render::cache::ExternalLocation;
use crate::json::conversions::from_def_id;
#[derive(Clone)]
crate struct JsonRenderer<'tcx> {
@ -50,7 +52,7 @@ impl JsonRenderer<'_> {
.map(|i| {
let item = &i.impl_item;
self.item(item.clone()).unwrap();
item.def_id.into()
from_def_id(item.def_id)
})
.collect()
})
@ -68,7 +70,7 @@ impl JsonRenderer<'_> {
let item = &i.impl_item;
if item.def_id.is_local() {
self.item(item.clone()).unwrap();
Some(item.def_id.into())
Some(from_def_id(item.def_id))
} else {
None
}
@ -87,9 +89,9 @@ impl JsonRenderer<'_> {
if !id.is_local() {
trait_item.items.clone().into_iter().for_each(|i| self.item(i).unwrap());
Some((
id.into(),
from_def_id(id),
types::Item {
id: id.into(),
id: from_def_id(id),
crate_id: id.krate.as_u32(),
name: self
.cache
@ -163,7 +165,7 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
} else if let types::ItemEnum::EnumItem(ref mut e) = new_item.inner {
e.impls = self.get_impls(id)
}
let removed = self.index.borrow_mut().insert(id.into(), new_item.clone());
let removed = self.index.borrow_mut().insert(from_def_id(id), new_item.clone());
// FIXME(adotinthevoid): Currently, the index is duplicated. This is a sanity check
// to make sure the items are unique.
if let Some(old_item) = removed {
@ -203,11 +205,14 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
debug!("Done with crate");
let mut index = (*self.index).clone().into_inner();
index.extend(self.get_trait_items());
// This needs to be the default HashMap for compatibility with the public interface for
// rustdoc-json
#[allow(rustc::default_hash_types)]
let output = types::Crate {
root: types::Id(String::from("0:0")),
crate_version: krate.version.clone(),
includes_private: self.cache.document_private,
index,
index: index.into_iter().collect(),
paths: self
.cache
.paths
@ -216,7 +221,7 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
.chain(self.cache.external_paths.clone().into_iter())
.map(|(k, (path, kind))| {
(
k.into(),
from_def_id(k),
types::ItemSummary { crate_id: k.krate.as_u32(), path, kind: kind.into() },
)
})

View File

@ -0,0 +1,11 @@
[package]
name = "rustdoc-json-types"
version = "0.1.0"
authors = ["The Rust Project Developers"]
edition = "2018"
[lib]
path = "lib.rs"
[dependencies]
serde = { version = "1.0", features = ["derive"] }

View File

@ -0,0 +1,12 @@
# Rustdoc JSON Types
This crate exposes the Rustdoc JSON API as a set of types with serde implementations.
These types are part of the public interface of the rustdoc JSON output, and making them
their own crate allows them to be versioned and distributed without having to depend on
any rustc/rustdoc internals. This way, consumers can rely on this crate for both documentation
of the output, and as a way to read the output easily, and its versioning is intended to
follow semver guarantees about the version of the format. JSON format X will always be
compatible with rustdoc-json-types version N.
Currently, this crate is only used by rustdoc itself. Upon the stabilization of
rustdoc-json, it may be distributed separately for consumers of the API.

View File

@ -3,9 +3,9 @@
//! These types are the public API exposed through the `--output-format json` flag. The [`Crate`]
//! struct is the root of the JSON blob and all other items are contained within.
use std::collections::HashMap;
use std::path::PathBuf;
use rustc_data_structures::fx::FxHashMap;
use serde::{Deserialize, Serialize};
/// A `Crate` is the root of the emitted JSON blob. It contains all type/documentation information
@ -21,11 +21,11 @@ pub struct Crate {
pub includes_private: bool,
/// A collection of all items in the local crate as well as some external traits and their
/// items that are referenced locally.
pub index: FxHashMap<Id, Item>,
pub index: HashMap<Id, Item>,
/// Maps IDs to fully qualified paths and other info helpful for generating links.
pub paths: FxHashMap<Id, ItemSummary>,
pub paths: HashMap<Id, ItemSummary>,
/// Maps `crate_id` of items to a crate name and html_root_url if it exists.
pub external_crates: FxHashMap<u32, ExternalCrate>,
pub external_crates: HashMap<u32, ExternalCrate>,
/// A single version number to be used in the future when making backwards incompatible changes
/// to the JSON output.
pub format_version: u32,
@ -72,7 +72,7 @@ pub struct Item {
/// Some("") if there is some documentation but it is empty (EG `#[doc = ""]`).
pub docs: Option<String>,
/// 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
pub links: FxHashMap<String, Id>,
pub links: HashMap<String, Id>,
/// Stringified versions of the attributes on this item (e.g. `"#[inline]"`)
pub attrs: Vec<String>,
pub deprecation: Option<Deprecation>,