Skip to content

rustdoc: Remove the json-{input, output} format #32773

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 9, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
@@ -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;

101 changes: 7 additions & 94 deletions src/librustdoc/lib.rs
Original file line number Diff line number Diff line change
@@ -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 }
}
37 changes: 18 additions & 19 deletions src/librustdoc/passes.rs
Original file line number Diff line number Diff line change
@@ -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 {
14 changes: 4 additions & 10 deletions src/librustdoc/plugins.rs
Original file line number Diff line number Diff line change
@@ -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
}
}

4 changes: 2 additions & 2 deletions src/librustdoc/test.rs
Original file line number Diff line number Diff line change
@@ -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,
4 changes: 0 additions & 4 deletions src/test/run-make/rustdoc-json/Makefile

This file was deleted.

25 changes: 0 additions & 25 deletions src/test/run-make/rustdoc-json/foo.rs

This file was deleted.