Make Rustdoc strip private fields

In addition, the renderer will add comments to structs and enums saying
that fields or variants have been stripped.
This commit is contained in:
Steven Fackler 2013-10-13 20:37:43 -07:00
parent 009c3d8bae
commit d6d31d788d
5 changed files with 27 additions and 9 deletions

View File

@ -685,6 +685,7 @@ pub struct Struct {
struct_type: doctree::StructType,
generics: Generics,
fields: ~[Item],
fields_stripped: bool,
}
impl Clean<Item> for doctree::Struct {
@ -699,6 +700,7 @@ impl Clean<Item> for doctree::Struct {
struct_type: self.struct_type,
generics: self.generics.clean(),
fields: self.fields.clean(),
fields_stripped: false,
}),
}
}
@ -711,6 +713,7 @@ impl Clean<Item> for doctree::Struct {
pub struct VariantStruct {
struct_type: doctree::StructType,
fields: ~[Item],
fields_stripped: bool,
}
impl Clean<VariantStruct> for syntax::ast::struct_def {
@ -718,6 +721,7 @@ impl Clean<VariantStruct> for syntax::ast::struct_def {
VariantStruct {
struct_type: doctree::struct_type_from_def(self),
fields: self.fields.clean(),
fields_stripped: false,
}
}
}
@ -726,6 +730,7 @@ impl Clean<VariantStruct> for syntax::ast::struct_def {
pub struct Enum {
variants: ~[Item],
generics: Generics,
variants_stripped: bool,
}
impl Clean<Item> for doctree::Enum {
@ -739,6 +744,7 @@ impl Clean<Item> for doctree::Enum {
inner: EnumItem(Enum {
variants: self.variants.clean(),
generics: self.generics.clean(),
variants_stripped: false,
}),
}
}

View File

@ -27,7 +27,9 @@ pub trait DocFolder {
StructItem(i) => {
let mut i = i;
let mut foo = ~[]; swap(&mut foo, &mut i.fields);
let num_fields = foo.len();
i.fields.extend(&mut foo.move_iter().filter_map(|x| self.fold_item(x)));
i.fields_stripped |= num_fields != i.fields.len();
StructItem(i)
},
ModuleItem(i) => {
@ -36,7 +38,9 @@ pub trait DocFolder {
EnumItem(i) => {
let mut i = i;
let mut foo = ~[]; swap(&mut foo, &mut i.variants);
let num_variants = foo.len();
i.variants.extend(&mut foo.move_iter().filter_map(|x| self.fold_item(x)));
i.variants_stripped |= num_variants != i.variants.len();
EnumItem(i)
},
TraitItem(i) => {
@ -73,7 +77,9 @@ pub trait DocFolder {
StructVariant(j) => {
let mut j = j;
let mut foo = ~[]; swap(&mut foo, &mut j.fields);
let num_fields = foo.len();
j.fields.extend(&mut foo.move_iter().filter_map(c));
j.fields_stripped |= num_fields != j.fields.len();
VariantItem(Variant {kind: StructVariant(j), ..i2})
},
_ => VariantItem(i2)

View File

@ -1265,7 +1265,8 @@ fn render_method(w: &mut io::Writer, meth: &clean::Item, withlink: bool) {
fn item_struct(w: &mut io::Writer, it: &clean::Item, s: &clean::Struct) {
write!(w, "<pre class='struct'>");
render_struct(w, it, Some(&s.generics), s.struct_type, s.fields, "", true);
render_struct(w, it, Some(&s.generics), s.struct_type, s.fields,
s.fields_stripped, "", true);
write!(w, "</pre>");
document(w, it);
@ -1312,7 +1313,7 @@ fn item_enum(w: &mut io::Writer, it: &clean::Item, e: &clean::Enum) {
}
clean::StructVariant(ref s) => {
render_struct(w, v, None, s.struct_type, s.fields,
" ", false);
s.fields_stripped, " ", false);
}
}
}
@ -1320,6 +1321,10 @@ fn item_enum(w: &mut io::Writer, it: &clean::Item, e: &clean::Enum) {
}
write!(w, ",\n");
}
if e.variants_stripped {
write!(w, " // some variants omitted\n");
}
write!(w, "\\}");
}
write!(w, "</pre>");
@ -1343,6 +1348,7 @@ fn render_struct(w: &mut io::Writer, it: &clean::Item,
g: Option<&clean::Generics>,
ty: doctree::StructType,
fields: &[clean::Item],
fields_stripped: bool,
tab: &str,
structhead: bool) {
write!(w, "{}{}{}",
@ -1368,6 +1374,10 @@ fn render_struct(w: &mut io::Writer, it: &clean::Item,
_ => unreachable!()
}
}
if fields_stripped {
write!(w, " // some fields omitted\n{}", tab);
}
write!(w, "\\}");
}
doctree::Tuple | doctree::Newtype => {

View File

@ -70,17 +70,13 @@ pub fn strip_private(mut crate: clean::Crate) -> plugins::PluginResult {
}
}
// These are public-by-default (if the enum was public)
clean::VariantItem(*) => {
// These are public-by-default (if the enum/struct was public)
clean::VariantItem(*) | clean::StructFieldItem(*) => {
if i.visibility == Some(ast::private) {
return None;
}
}
// We show these regardless of whether they're public/private
// because it's useful to see sometimes
clean::StructFieldItem(*) => {}
// handled below
clean::ModuleItem(*) => {}

View File

@ -48,7 +48,7 @@ pub mod passes;
pub mod plugins;
pub mod visit_ast;
pub static SCHEMA_VERSION: &'static str = "0.8.0";
pub static SCHEMA_VERSION: &'static str = "0.8.1";
type Pass = (&'static str, // name
extern fn(clean::Crate) -> plugins::PluginResult, // fn