2019-02-23 16:40:07 +09:00
|
|
|
use crate::clean::*;
|
2013-09-21 23:25:48 -07:00
|
|
|
|
2018-07-22 15:01:09 -06:00
|
|
|
pub struct StripItem(pub Item);
|
2016-03-31 18:15:54 +02:00
|
|
|
|
2018-07-22 15:01:09 -06:00
|
|
|
impl StripItem {
|
|
|
|
pub fn strip(self) -> Option<Item> {
|
|
|
|
match self.0 {
|
|
|
|
Item { inner: StrippedItem(..), .. } => Some(self.0),
|
|
|
|
mut i => {
|
2016-03-31 18:15:54 +02:00
|
|
|
i.inner = StrippedItem(box i.inner);
|
|
|
|
Some(i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-18 15:27:41 -05:00
|
|
|
pub trait DocFolder : Sized {
|
2013-09-21 23:25:48 -07:00
|
|
|
fn fold_item(&mut self, item: Item) -> Option<Item> {
|
|
|
|
self.fold_item_recur(item)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// don't override!
|
2016-03-31 18:15:54 +02:00
|
|
|
fn fold_inner_recur(&mut self, inner: ItemEnum) -> ItemEnum {
|
|
|
|
match inner {
|
|
|
|
StrippedItem(..) => unreachable!(),
|
|
|
|
ModuleItem(i) => {
|
|
|
|
ModuleItem(self.fold_mod(i))
|
|
|
|
},
|
2013-12-16 21:30:02 -08:00
|
|
|
StructItem(mut i) => {
|
2016-02-28 16:38:51 +01:00
|
|
|
let num_fields = i.fields.len();
|
|
|
|
i.fields = i.fields.into_iter().filter_map(|x| self.fold_item(x)).collect();
|
2016-03-31 18:15:54 +02:00
|
|
|
i.fields_stripped |= num_fields != i.fields.len() ||
|
|
|
|
i.fields.iter().any(|f| f.is_stripped());
|
2013-09-21 23:25:48 -07:00
|
|
|
StructItem(i)
|
|
|
|
},
|
2016-08-10 21:00:17 +03:00
|
|
|
UnionItem(mut i) => {
|
|
|
|
let num_fields = i.fields.len();
|
|
|
|
i.fields = i.fields.into_iter().filter_map(|x| self.fold_item(x)).collect();
|
|
|
|
i.fields_stripped |= num_fields != i.fields.len() ||
|
|
|
|
i.fields.iter().any(|f| f.is_stripped());
|
|
|
|
UnionItem(i)
|
|
|
|
},
|
2013-12-16 21:30:02 -08:00
|
|
|
EnumItem(mut i) => {
|
2016-02-28 16:38:51 +01:00
|
|
|
let num_variants = i.variants.len();
|
|
|
|
i.variants = i.variants.into_iter().filter_map(|x| self.fold_item(x)).collect();
|
2016-03-31 18:15:54 +02:00
|
|
|
i.variants_stripped |= num_variants != i.variants.len() ||
|
|
|
|
i.variants.iter().any(|f| f.is_stripped());
|
2013-09-21 23:25:48 -07:00
|
|
|
EnumItem(i)
|
|
|
|
},
|
2013-12-16 21:30:02 -08:00
|
|
|
TraitItem(mut i) => {
|
2016-02-28 16:38:51 +01:00
|
|
|
i.items = i.items.into_iter().filter_map(|x| self.fold_item(x)).collect();
|
2013-09-21 23:25:48 -07:00
|
|
|
TraitItem(i)
|
|
|
|
},
|
2013-12-16 21:30:02 -08:00
|
|
|
ImplItem(mut i) => {
|
2016-02-28 16:38:51 +01:00
|
|
|
i.items = i.items.into_iter().filter_map(|x| self.fold_item(x)).collect();
|
2013-09-21 23:25:48 -07:00
|
|
|
ImplItem(i)
|
|
|
|
},
|
|
|
|
VariantItem(i) => {
|
|
|
|
let i2 = i.clone(); // this clone is small
|
|
|
|
match i.kind {
|
2016-10-02 20:07:18 -04:00
|
|
|
VariantKind::Struct(mut j) => {
|
2016-02-28 16:38:51 +01:00
|
|
|
let num_fields = j.fields.len();
|
|
|
|
j.fields = j.fields.into_iter().filter_map(|x| self.fold_item(x)).collect();
|
2016-03-31 18:15:54 +02:00
|
|
|
j.fields_stripped |= num_fields != j.fields.len() ||
|
|
|
|
j.fields.iter().any(|f| f.is_stripped());
|
2016-10-02 20:07:18 -04:00
|
|
|
VariantItem(Variant {kind: VariantKind::Struct(j), ..i2})
|
2013-09-21 23:25:48 -07:00
|
|
|
},
|
|
|
|
_ => VariantItem(i2)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
x => x
|
2016-03-31 18:15:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// don't override!
|
|
|
|
fn fold_item_recur(&mut self, item: Item) -> Option<Item> {
|
2018-06-27 20:50:59 +01:00
|
|
|
let Item {
|
|
|
|
attrs,
|
|
|
|
name,
|
|
|
|
source,
|
|
|
|
visibility,
|
|
|
|
def_id,
|
|
|
|
inner,
|
|
|
|
stability,
|
|
|
|
deprecation,
|
|
|
|
} = item;
|
2016-03-31 18:15:54 +02:00
|
|
|
|
|
|
|
let inner = match inner {
|
|
|
|
StrippedItem(box i) => StrippedItem(box self.fold_inner_recur(i)),
|
|
|
|
_ => self.fold_inner_recur(inner),
|
2013-09-21 23:25:48 -07:00
|
|
|
};
|
|
|
|
|
2017-08-06 22:54:09 -07:00
|
|
|
Some(Item { attrs, name, source, inner, visibility,
|
|
|
|
stability, deprecation, def_id })
|
2013-09-21 23:25:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_mod(&mut self, m: Module) -> Module {
|
2014-02-28 22:33:45 +01:00
|
|
|
Module {
|
|
|
|
is_crate: m.is_crate,
|
2014-09-14 20:27:36 -07:00
|
|
|
items: m.items.into_iter().filter_map(|i| self.fold_item(i)).collect()
|
2014-02-28 22:33:45 +01:00
|
|
|
}
|
2013-09-21 23:25:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_crate(&mut self, mut c: Crate) -> Crate {
|
2018-06-01 10:20:00 -07:00
|
|
|
c.module = c.module.take().and_then(|module| self.fold_item(module));
|
2016-03-31 18:15:54 +02:00
|
|
|
|
2018-09-01 21:20:39 -05:00
|
|
|
{
|
2019-08-10 15:20:21 -04:00
|
|
|
let mut guard = c.external_traits.borrow_mut();
|
|
|
|
let external_traits = std::mem::replace(&mut *guard, Default::default());
|
|
|
|
*guard = external_traits.into_iter().map(|(k, mut v)| {
|
2018-09-01 21:20:39 -05:00
|
|
|
v.items = v.items.into_iter().filter_map(|i| self.fold_item(i)).collect();
|
|
|
|
(k, v)
|
2019-08-10 15:20:21 -04:00
|
|
|
}).collect();
|
2018-09-01 21:20:39 -05:00
|
|
|
}
|
2016-02-28 16:38:51 +01:00
|
|
|
c
|
2012-09-19 14:37:43 -07:00
|
|
|
}
|
|
|
|
}
|