mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-15 08:23:26 +00:00
Auto merge of #32773 - mitaa:rdoc-ttfn-json, r=alexcrichton
rustdoc: Remove the json-{input, output} format (for reference #32698) fixes #25108 r? @alexcrichton
This commit is contained in:
commit
c22302f53f
@ -54,10 +54,6 @@ use doctree;
|
||||
use visit_ast;
|
||||
use html::item_type::ItemType;
|
||||
|
||||
/// A stable identifier to the particular version of JSON output.
|
||||
/// Increment this when the `Crate` and related structures change.
|
||||
pub const SCHEMA_VERSION: &'static str = "0.8.3";
|
||||
|
||||
mod inline;
|
||||
mod simplify;
|
||||
|
||||
|
@ -54,22 +54,16 @@ use std::cell::RefCell;
|
||||
use std::collections::HashMap;
|
||||
use std::default::Default;
|
||||
use std::env;
|
||||
use std::fs::File;
|
||||
use std::io::{self, Read, Write};
|
||||
use std::io::Read;
|
||||
use std::path::PathBuf;
|
||||
use std::process;
|
||||
use std::rc::Rc;
|
||||
use std::sync::mpsc::channel;
|
||||
|
||||
use externalfiles::ExternalHtml;
|
||||
use serialize::Decodable;
|
||||
use serialize::json::{self, Json};
|
||||
use rustc::session::search_paths::SearchPaths;
|
||||
use rustc::session::config::{ErrorOutputType, RustcOptGroup, nightly_options};
|
||||
|
||||
// reexported from `clean` so it can be easily updated with the mod itself
|
||||
pub use clean::SCHEMA_VERSION;
|
||||
|
||||
#[macro_use]
|
||||
pub mod externalfiles;
|
||||
|
||||
@ -127,7 +121,6 @@ thread_local!(pub static ANALYSISKEY: Rc<RefCell<Option<core::CrateAnalysis>>> =
|
||||
|
||||
struct Output {
|
||||
krate: clean::Crate,
|
||||
json_plugins: Vec<plugins::PluginJson>,
|
||||
passes: Vec<String>,
|
||||
}
|
||||
|
||||
@ -150,9 +143,9 @@ pub fn opts() -> Vec<RustcOptGroup> {
|
||||
stable(optflag("V", "version", "print rustdoc's version")),
|
||||
stable(optflag("v", "verbose", "use verbose output")),
|
||||
stable(optopt("r", "input-format", "the input type of the specified file",
|
||||
"[rust|json]")),
|
||||
"[rust]")),
|
||||
stable(optopt("w", "output-format", "the output type to write",
|
||||
"[html|json]")),
|
||||
"[html]")),
|
||||
stable(optopt("o", "output", "where to place the output", "PATH")),
|
||||
stable(optopt("", "crate-name", "specify the name of this crate", "NAME")),
|
||||
stable(optmulti("L", "library-path", "directory to add to crate search path",
|
||||
@ -311,7 +304,7 @@ pub fn main_args(args: &[String]) -> isize {
|
||||
return 1;
|
||||
}
|
||||
};
|
||||
let Output { krate, json_plugins, passes, } = out;
|
||||
let Output { krate, passes, } = out;
|
||||
info!("going to format");
|
||||
match matches.opt_str("w").as_ref().map(|s| &**s) {
|
||||
Some("html") | None => {
|
||||
@ -321,11 +314,6 @@ pub fn main_args(args: &[String]) -> isize {
|
||||
css_file_extension)
|
||||
.expect("failed to generate documentation")
|
||||
}
|
||||
Some("json") => {
|
||||
json_output(krate, json_plugins,
|
||||
output.unwrap_or(PathBuf::from("doc.json")))
|
||||
.expect("failed to write json")
|
||||
}
|
||||
Some(s) => {
|
||||
println!("unknown output format: {}", s);
|
||||
return 1;
|
||||
@ -342,14 +330,9 @@ fn acquire_input(input: &str,
|
||||
matches: &getopts::Matches) -> Result<Output, String> {
|
||||
match matches.opt_str("r").as_ref().map(|s| &**s) {
|
||||
Some("rust") => Ok(rust_input(input, externs, matches)),
|
||||
Some("json") => json_input(input),
|
||||
Some(s) => Err(format!("unknown input format: {}", s)),
|
||||
None => {
|
||||
if input.ends_with(".json") {
|
||||
json_input(input)
|
||||
} else {
|
||||
Ok(rust_input(input, externs, matches))
|
||||
}
|
||||
Ok(rust_input(input, externs, matches))
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -461,76 +444,6 @@ fn rust_input(cratefile: &str, externs: core::Externs, matches: &getopts::Matche
|
||||
|
||||
// Run everything!
|
||||
info!("Executing passes/plugins");
|
||||
let (krate, json) = pm.run_plugins(krate);
|
||||
Output { krate: krate, json_plugins: json, passes: passes }
|
||||
}
|
||||
|
||||
/// This input format purely deserializes the json output file. No passes are
|
||||
/// run over the deserialized output.
|
||||
fn json_input(input: &str) -> Result<Output, String> {
|
||||
let mut bytes = Vec::new();
|
||||
if let Err(e) = File::open(input).and_then(|mut f| f.read_to_end(&mut bytes)) {
|
||||
return Err(format!("couldn't open {}: {}", input, e))
|
||||
}
|
||||
match json::from_reader(&mut &bytes[..]) {
|
||||
Err(s) => Err(format!("{:?}", s)),
|
||||
Ok(Json::Object(obj)) => {
|
||||
let mut obj = obj;
|
||||
// Make sure the schema is what we expect
|
||||
match obj.remove(&"schema".to_string()) {
|
||||
Some(Json::String(version)) => {
|
||||
if version != SCHEMA_VERSION {
|
||||
return Err(format!(
|
||||
"sorry, but I only understand version {}",
|
||||
SCHEMA_VERSION))
|
||||
}
|
||||
}
|
||||
Some(..) => return Err("malformed json".to_string()),
|
||||
None => return Err("expected a schema version".to_string()),
|
||||
}
|
||||
let krate = match obj.remove(&"crate".to_string()) {
|
||||
Some(json) => {
|
||||
let mut d = json::Decoder::new(json);
|
||||
Decodable::decode(&mut d).unwrap()
|
||||
}
|
||||
None => return Err("malformed json".to_string()),
|
||||
};
|
||||
// FIXME: this should read from the "plugins" field, but currently
|
||||
// Json doesn't implement decodable...
|
||||
let plugin_output = Vec::new();
|
||||
Ok(Output { krate: krate, json_plugins: plugin_output, passes: Vec::new(), })
|
||||
}
|
||||
Ok(..) => {
|
||||
Err("malformed json input: expected an object at the \
|
||||
top".to_string())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Outputs the crate/plugin json as a giant json blob at the specified
|
||||
/// destination.
|
||||
fn json_output(krate: clean::Crate, res: Vec<plugins::PluginJson> ,
|
||||
dst: PathBuf) -> io::Result<()> {
|
||||
// {
|
||||
// "schema": version,
|
||||
// "crate": { parsed crate ... },
|
||||
// "plugins": { output of plugins ... }
|
||||
// }
|
||||
let mut json = std::collections::BTreeMap::new();
|
||||
json.insert("schema".to_string(), Json::String(SCHEMA_VERSION.to_string()));
|
||||
let plugins_json = res.into_iter()
|
||||
.filter_map(|opt| {
|
||||
opt.map(|(string, json)| (string.to_string(), json))
|
||||
}).collect();
|
||||
|
||||
// FIXME #8335: yuck, Rust -> str -> JSON round trip! No way to .encode
|
||||
// straight to the Rust JSON representation.
|
||||
let crate_json_str = format!("{}", json::as_json(&krate));
|
||||
let crate_json = json::from_str(&crate_json_str).expect("Rust generated JSON is invalid");
|
||||
|
||||
json.insert("crate".to_string(), crate_json);
|
||||
json.insert("plugins".to_string(), Json::Object(plugins_json));
|
||||
|
||||
let mut file = File::create(&dst)?;
|
||||
write!(&mut file, "{}", Json::Object(json))
|
||||
let krate = pm.run_plugins(krate);
|
||||
Output { krate: krate, passes: passes }
|
||||
}
|
||||
|
@ -54,7 +54,7 @@ pub fn strip_hidden(krate: clean::Crate) -> plugins::PluginResult {
|
||||
};
|
||||
|
||||
// strip any traits implemented on stripped items
|
||||
let krate = {
|
||||
{
|
||||
struct ImplStripper<'a> {
|
||||
stripped: &'a mut DefIdSet
|
||||
}
|
||||
@ -80,9 +80,7 @@ pub fn strip_hidden(krate: clean::Crate) -> plugins::PluginResult {
|
||||
}
|
||||
let mut stripper = ImplStripper{ stripped: &mut stripped };
|
||||
stripper.fold_crate(krate)
|
||||
};
|
||||
|
||||
(krate, None)
|
||||
}
|
||||
}
|
||||
|
||||
/// Strip private items from the point of view of a crate or externally from a
|
||||
@ -107,9 +105,8 @@ pub fn strip_private(mut krate: clean::Crate) -> plugins::PluginResult {
|
||||
// strip all private implementations of traits
|
||||
{
|
||||
let mut stripper = ImplStripper(&retained);
|
||||
krate = stripper.fold_crate(krate);
|
||||
stripper.fold_crate(krate)
|
||||
}
|
||||
(krate, None)
|
||||
}
|
||||
|
||||
struct Stripper<'a> {
|
||||
@ -192,17 +189,19 @@ impl<'a> fold::DocFolder for Stripper<'a> {
|
||||
self.fold_item_recur(i)
|
||||
};
|
||||
|
||||
i.and_then(|i| { match i.inner {
|
||||
// emptied modules/impls have no need to exist
|
||||
clean::ModuleItem(ref m)
|
||||
if m.items.is_empty() &&
|
||||
i.doc_value().is_none() => None,
|
||||
clean::ImplItem(ref i) if i.items.is_empty() => None,
|
||||
_ => {
|
||||
self.retained.insert(i.def_id);
|
||||
Some(i)
|
||||
i.and_then(|i| {
|
||||
match i.inner {
|
||||
// emptied modules/impls have no need to exist
|
||||
clean::ModuleItem(ref m)
|
||||
if m.items.is_empty() &&
|
||||
i.doc_value().is_none() => None,
|
||||
clean::ImplItem(ref i) if i.items.is_empty() => None,
|
||||
_ => {
|
||||
self.retained.insert(i.def_id);
|
||||
Some(i)
|
||||
}
|
||||
}
|
||||
}})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@ -234,7 +233,7 @@ impl fold::DocFolder for ImportStripper {
|
||||
}
|
||||
|
||||
pub fn strip_priv_imports(krate: clean::Crate) -> plugins::PluginResult {
|
||||
(ImportStripper.fold_crate(krate), None)
|
||||
ImportStripper.fold_crate(krate)
|
||||
}
|
||||
|
||||
pub fn unindent_comments(krate: clean::Crate) -> plugins::PluginResult {
|
||||
@ -258,7 +257,7 @@ pub fn unindent_comments(krate: clean::Crate) -> plugins::PluginResult {
|
||||
}
|
||||
let mut cleaner = CommentCleaner;
|
||||
let krate = cleaner.fold_crate(krate);
|
||||
(krate, None)
|
||||
krate
|
||||
}
|
||||
|
||||
pub fn collapse_docs(krate: clean::Crate) -> plugins::PluginResult {
|
||||
@ -287,7 +286,7 @@ pub fn collapse_docs(krate: clean::Crate) -> plugins::PluginResult {
|
||||
}
|
||||
let mut collapser = Collapser;
|
||||
let krate = collapser.fold_crate(krate);
|
||||
(krate, None)
|
||||
krate
|
||||
}
|
||||
|
||||
pub fn unindent(s: &str) -> String {
|
||||
|
@ -12,15 +12,13 @@
|
||||
|
||||
use clean;
|
||||
|
||||
use serialize::json;
|
||||
use std::mem;
|
||||
use std::string::String;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use rustc_back::dynamic_lib as dl;
|
||||
|
||||
pub type PluginJson = Option<(String, json::Json)>;
|
||||
pub type PluginResult = (clean::Crate, PluginJson);
|
||||
pub type PluginResult = clean::Crate;
|
||||
pub type PluginCallback = fn (clean::Crate) -> PluginResult;
|
||||
|
||||
/// Manages loading and running of plugins
|
||||
@ -65,15 +63,11 @@ impl PluginManager {
|
||||
self.callbacks.push(plugin);
|
||||
}
|
||||
/// Run all the loaded plugins over the crate, returning their results
|
||||
pub fn run_plugins(&self, krate: clean::Crate) -> (clean::Crate, Vec<PluginJson> ) {
|
||||
let mut out_json = Vec::new();
|
||||
let mut krate = krate;
|
||||
pub fn run_plugins(&self, mut krate: clean::Crate) -> clean::Crate {
|
||||
for &callback in &self.callbacks {
|
||||
let (c, res) = callback(krate);
|
||||
krate = c;
|
||||
out_json.push(res);
|
||||
krate = callback(krate);
|
||||
}
|
||||
(krate, out_json)
|
||||
krate
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -122,8 +122,8 @@ pub fn run(input: &str,
|
||||
if let Some(name) = crate_name {
|
||||
krate.name = name;
|
||||
}
|
||||
let (krate, _) = passes::collapse_docs(krate);
|
||||
let (krate, _) = passes::unindent_comments(krate);
|
||||
let krate = passes::collapse_docs(krate);
|
||||
let krate = passes::unindent_comments(krate);
|
||||
|
||||
let mut collector = Collector::new(krate.name.to_string(),
|
||||
cfgs,
|
||||
|
@ -1,4 +0,0 @@
|
||||
-include ../tools.mk
|
||||
all:
|
||||
$(HOST_RPATH_ENV) $(RUSTDOC) -w json -o $(TMPDIR)/doc.json foo.rs
|
||||
$(HOST_RPATH_ENV) $(RUSTDOC) -o $(TMPDIR)/doc $(TMPDIR)/doc.json
|
@ -1,25 +0,0 @@
|
||||
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
#![crate_name = "foo"]
|
||||
|
||||
//! Very docs
|
||||
|
||||
pub mod bar {
|
||||
|
||||
/// So correct
|
||||
pub mod baz {
|
||||
/// Much detail
|
||||
pub fn baz() { }
|
||||
}
|
||||
|
||||
/// *wow*
|
||||
pub trait Doge { fn dummy(&self) { } }
|
||||
}
|
Loading…
Reference in New Issue
Block a user