Replace ToJson with serde
This commit is contained in:
parent
15babed8b7
commit
f1070b1525
8 changed files with 52 additions and 79 deletions
|
@ -1,6 +1,6 @@
|
||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
use std::ffi::OsStr;
|
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
|
use std::ffi::OsStr;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
@ -28,12 +28,12 @@ use crate::theme;
|
||||||
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
||||||
pub enum OutputFormat {
|
pub enum OutputFormat {
|
||||||
Json,
|
Json,
|
||||||
HTML,
|
Html,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl OutputFormat {
|
impl OutputFormat {
|
||||||
pub fn is_json(&self) -> bool {
|
pub fn is_json(&self) -> bool {
|
||||||
match *self {
|
match self {
|
||||||
OutputFormat::Json => true,
|
OutputFormat::Json => true,
|
||||||
_ => false,
|
_ => false,
|
||||||
}
|
}
|
||||||
|
@ -46,7 +46,7 @@ impl TryFrom<&str> for OutputFormat {
|
||||||
fn try_from(value: &str) -> Result<Self, Self::Error> {
|
fn try_from(value: &str) -> Result<Self, Self::Error> {
|
||||||
match value {
|
match value {
|
||||||
"json" => Ok(OutputFormat::Json),
|
"json" => Ok(OutputFormat::Json),
|
||||||
"html" => Ok(OutputFormat::HTML),
|
"html" => Ok(OutputFormat::Html),
|
||||||
_ => Err(format!("unknown output format `{}`", value)),
|
_ => Err(format!("unknown output format `{}`", value)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -498,6 +498,12 @@ impl Options {
|
||||||
diag.struct_err("json output format isn't supported for doc generation")
|
diag.struct_err("json output format isn't supported for doc generation")
|
||||||
.emit();
|
.emit();
|
||||||
return Err(1);
|
return Err(1);
|
||||||
|
} else if !o.is_json() && show_coverage {
|
||||||
|
diag.struct_err(
|
||||||
|
"html output format isn't supported for the --show-coverage option",
|
||||||
|
)
|
||||||
|
.emit();
|
||||||
|
return Err(1);
|
||||||
}
|
}
|
||||||
Some(o)
|
Some(o)
|
||||||
}
|
}
|
||||||
|
@ -505,7 +511,7 @@ impl Options {
|
||||||
diag.struct_err(&e).emit();
|
diag.struct_err(&e).emit();
|
||||||
return Err(1);
|
return Err(1);
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
None => None,
|
None => None,
|
||||||
};
|
};
|
||||||
let crate_name = matches.opt_str("crate-name");
|
let crate_name = matches.opt_str("crate-name");
|
||||||
|
|
|
@ -7,7 +7,8 @@ use crate::passes::Pass;
|
||||||
use rustc_ast::attr;
|
use rustc_ast::attr;
|
||||||
use rustc_span::symbol::sym;
|
use rustc_span::symbol::sym;
|
||||||
use rustc_span::FileName;
|
use rustc_span::FileName;
|
||||||
use serialize::json::{ToJson, Json};
|
use serde::Serialize;
|
||||||
|
use serde_json;
|
||||||
|
|
||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
use std::ops;
|
use std::ops;
|
||||||
|
@ -18,16 +19,16 @@ pub const CALCULATE_DOC_COVERAGE: Pass = Pass {
|
||||||
description: "counts the number of items with and without documentation",
|
description: "counts the number of items with and without documentation",
|
||||||
};
|
};
|
||||||
|
|
||||||
fn calculate_doc_coverage( krate: clean::Crate, ctx: &DocContext<'_>) -> clean::Crate {
|
fn calculate_doc_coverage(krate: clean::Crate, ctx: &DocContext<'_>) -> clean::Crate {
|
||||||
let mut calc = CoverageCalculator::new(ctx.renderinfo.borrow().output_format);
|
let mut calc = CoverageCalculator::new();
|
||||||
let krate = calc.fold_crate(krate);
|
let krate = calc.fold_crate(krate);
|
||||||
|
|
||||||
calc.print_results();
|
calc.print_results(ctx.renderinfo.borrow().output_format);
|
||||||
|
|
||||||
krate
|
krate
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default, Copy, Clone)]
|
#[derive(Default, Copy, Clone, Serialize)]
|
||||||
struct ItemCount {
|
struct ItemCount {
|
||||||
total: u64,
|
total: u64,
|
||||||
with_docs: u64,
|
with_docs: u64,
|
||||||
|
@ -68,68 +69,37 @@ impl ops::AddAssign for ItemCount {
|
||||||
|
|
||||||
struct CoverageCalculator {
|
struct CoverageCalculator {
|
||||||
items: BTreeMap<FileName, ItemCount>,
|
items: BTreeMap<FileName, ItemCount>,
|
||||||
output_format: Option<OutputFormat>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn limit_filename_len(filename: String) -> String {
|
fn limit_filename_len(filename: String) -> String {
|
||||||
// if a filename is too long, shorten it so we don't blow out the table
|
let nb_chars = filename.chars().count();
|
||||||
// FIXME(misdreavus): this needs to count graphemes, and probably also track
|
if nb_chars > 35 {
|
||||||
// double-wide characters...
|
"...".to_string()
|
||||||
if filename.len() > 35 {
|
+ &filename[filename.char_indices().nth(nb_chars - 32).map(|x| x.0).unwrap_or(0)..]
|
||||||
"...".to_string() + &filename[filename.len() - 32..]
|
|
||||||
} else {
|
} else {
|
||||||
filename
|
filename
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ToJson for CoverageCalculator {
|
|
||||||
fn to_json(&self) -> Json {
|
|
||||||
let mut total = ItemCount::default();
|
|
||||||
let mut entries = BTreeMap::default();
|
|
||||||
|
|
||||||
entries.insert("files".to_owned(), Json::Array(self.items
|
|
||||||
.iter()
|
|
||||||
.filter_map(|(file, &count)| {
|
|
||||||
count.percentage().map(|percent| {
|
|
||||||
(limit_filename_len(file.to_string()), count, percent)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
.map(|(name, count, percentage)| {
|
|
||||||
let mut fields = BTreeMap::default();
|
|
||||||
|
|
||||||
fields.insert("documented".to_owned(), Json::U64(count.with_docs));
|
|
||||||
fields.insert("total".to_owned(), Json::U64(count.total));
|
|
||||||
fields.insert("percentage".to_owned(), Json::F64(percentage));
|
|
||||||
|
|
||||||
total += count;
|
|
||||||
|
|
||||||
let mut obj = BTreeMap::default();
|
|
||||||
obj.insert(name, Json::Object(fields));
|
|
||||||
|
|
||||||
Json::Object(obj)
|
|
||||||
})
|
|
||||||
.collect::<Vec<_>>()));
|
|
||||||
let mut fields = BTreeMap::default();
|
|
||||||
fields.insert("documented".to_owned(), Json::U64(total.with_docs));
|
|
||||||
fields.insert("total".to_owned(), Json::U64(total.total));
|
|
||||||
fields.insert("percentage".to_owned(), Json::F64(total.percentage().unwrap_or(0.0)));
|
|
||||||
|
|
||||||
entries.insert("total".to_owned(), Json::Object(fields));
|
|
||||||
Json::Object(entries)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl CoverageCalculator {
|
impl CoverageCalculator {
|
||||||
fn new(output_format: Option<OutputFormat>) -> CoverageCalculator {
|
fn new() -> CoverageCalculator {
|
||||||
CoverageCalculator {
|
CoverageCalculator { items: Default::default() }
|
||||||
items: Default::default(),
|
|
||||||
output_format,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print_results(&self) {
|
fn to_json(&self) -> String {
|
||||||
if self.output_format.map(|o| o.is_json()).unwrap_or_else(|| false) {
|
serde_json::to_string(
|
||||||
println!("{}", self.to_json().pretty());
|
&self
|
||||||
|
.items
|
||||||
|
.iter()
|
||||||
|
.map(|(k, v)| (k.to_string(), v))
|
||||||
|
.collect::<BTreeMap<String, &ItemCount>>(),
|
||||||
|
)
|
||||||
|
.expect("failed to convert JSON data to string")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn print_results(&self, output_format: Option<OutputFormat>) {
|
||||||
|
if output_format.map(|o| o.is_json()).unwrap_or_else(|| false) {
|
||||||
|
println!("{}", self.to_json());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let mut total = ItemCount::default();
|
let mut total = ItemCount::default();
|
||||||
|
@ -154,15 +124,7 @@ impl CoverageCalculator {
|
||||||
|
|
||||||
for (file, &count) in &self.items {
|
for (file, &count) in &self.items {
|
||||||
if let Some(percentage) = count.percentage() {
|
if let Some(percentage) = count.percentage() {
|
||||||
let mut name = file.to_string();
|
print_table_record(&limit_filename_len(file.to_string()), count, percentage);
|
||||||
// if a filename is too long, shorten it so we don't blow out the table
|
|
||||||
// FIXME(misdreavus): this needs to count graphemes, and probably also track
|
|
||||||
// double-wide characters...
|
|
||||||
if name.len() > 35 {
|
|
||||||
name = "...".to_string() + &name[name.len() - 32..];
|
|
||||||
}
|
|
||||||
|
|
||||||
print_table_record(&name, count, percentage);
|
|
||||||
|
|
||||||
total += count;
|
total += count;
|
||||||
}
|
}
|
||||||
|
|
4
src/test/rustdoc-ui/coverage/html.rs
Normal file
4
src/test/rustdoc-ui/coverage/html.rs
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
// compile-flags:-Z unstable-options --output-format html --show-coverage
|
||||||
|
|
||||||
|
/// Foo
|
||||||
|
pub struct Xo;
|
2
src/test/rustdoc-ui/coverage/html.stderr
Normal file
2
src/test/rustdoc-ui/coverage/html.stderr
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
error: html output format isn't supported for the --show-coverage option
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
// build-pass
|
// build-pass
|
||||||
// compile-flags:-Z unstable-options --show-coverage
|
// compile-flags:-Z unstable-options --output-format json --show-coverage
|
||||||
|
|
||||||
pub mod foo {
|
pub mod foo {
|
||||||
/// Hello!
|
/// Hello!
|
||||||
|
|
|
@ -1,7 +1 @@
|
||||||
+-------------------------------------+------------+------------+------------+
|
{"$DIR/json.rs":{"total":13,"with_docs":7}}
|
||||||
| File | Documented | Total | Percentage |
|
|
||||||
+-------------------------------------+------------+------------+------------+
|
|
||||||
| ...test/rustdoc-ui/coverage/json.rs | 7 | 13 | 53.8% |
|
|
||||||
+-------------------------------------+------------+------------+------------+
|
|
||||||
| Total | 7 | 13 | 53.8% |
|
|
||||||
+-------------------------------------+------------+------------+------------+
|
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
// compile-flags:-Z unstable-options --output-format
|
// compile-flags:-Z unstable-options --output-format
|
||||||
// should-fail
|
|
||||||
|
|
||||||
/// toudoum!
|
/// toudoum!
|
||||||
pub struct SomeStruct;
|
pub struct SomeStruct;
|
|
@ -0,0 +1,6 @@
|
||||||
|
warning: the 'output-format' flag is considered deprecated
|
||||||
|
|
|
||||||
|
= warning: see issue #44136 <https://github.com/rust-lang/rust/issues/44136> for more information
|
||||||
|
|
||||||
|
error: too many file operands
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue