]>
Commit | Line | Data |
---|---|---|
1a4d82fc | 1 | // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT |
223e47cc LB |
2 | // file at the top-level directory of this distribution and at |
3 | // http://rust-lang.org/COPYRIGHT. | |
4 | // | |
5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |
8 | // option. This file may not be copied, modified, or distributed | |
9 | // except according to those terms. | |
10 | ||
1a4d82fc | 11 | use clean::*; |
54a0048b SL |
12 | |
13 | pub enum FoldItem { | |
14 | Retain(Item), | |
15 | Strip(Item), | |
16 | Erase, | |
17 | } | |
18 | ||
19 | impl FoldItem { | |
20 | pub fn fold(self) -> Option<Item> { | |
21 | match self { | |
22 | FoldItem::Erase => None, | |
23 | FoldItem::Retain(i) => Some(i), | |
24 | FoldItem::Strip(item@ Item { inner: StrippedItem(..), .. } ) => Some(item), | |
25 | FoldItem::Strip(mut i) => { | |
26 | i.inner = StrippedItem(box i.inner); | |
27 | Some(i) | |
28 | } | |
29 | } | |
30 | } | |
31 | } | |
1a4d82fc JJ |
32 | |
33 | pub trait DocFolder : Sized { | |
34 | fn fold_item(&mut self, item: Item) -> Option<Item> { | |
35 | self.fold_item_recur(item) | |
36 | } | |
37 | ||
38 | /// don't override! | |
54a0048b SL |
39 | fn fold_inner_recur(&mut self, inner: ItemEnum) -> ItemEnum { |
40 | match inner { | |
41 | StrippedItem(..) => unreachable!(), | |
1a4d82fc JJ |
42 | ModuleItem(i) => { |
43 | ModuleItem(self.fold_mod(i)) | |
44 | }, | |
54a0048b SL |
45 | StructItem(mut i) => { |
46 | let num_fields = i.fields.len(); | |
47 | i.fields = i.fields.into_iter().filter_map(|x| self.fold_item(x)).collect(); | |
48 | i.fields_stripped |= num_fields != i.fields.len() || | |
49 | i.fields.iter().any(|f| f.is_stripped()); | |
50 | StructItem(i) | |
51 | }, | |
1a4d82fc | 52 | EnumItem(mut i) => { |
54a0048b SL |
53 | let num_variants = i.variants.len(); |
54 | i.variants = i.variants.into_iter().filter_map(|x| self.fold_item(x)).collect(); | |
55 | i.variants_stripped |= num_variants != i.variants.len() || | |
56 | i.variants.iter().any(|f| f.is_stripped()); | |
1a4d82fc JJ |
57 | EnumItem(i) |
58 | }, | |
59 | TraitItem(mut i) => { | |
54a0048b | 60 | i.items = i.items.into_iter().filter_map(|x| self.fold_item(x)).collect(); |
1a4d82fc JJ |
61 | TraitItem(i) |
62 | }, | |
63 | ImplItem(mut i) => { | |
54a0048b | 64 | i.items = i.items.into_iter().filter_map(|x| self.fold_item(x)).collect(); |
1a4d82fc JJ |
65 | ImplItem(i) |
66 | }, | |
67 | VariantItem(i) => { | |
68 | let i2 = i.clone(); // this clone is small | |
69 | match i.kind { | |
70 | StructVariant(mut j) => { | |
54a0048b SL |
71 | let num_fields = j.fields.len(); |
72 | j.fields = j.fields.into_iter().filter_map(|x| self.fold_item(x)).collect(); | |
73 | j.fields_stripped |= num_fields != j.fields.len() || | |
74 | j.fields.iter().any(|f| f.is_stripped()); | |
1a4d82fc JJ |
75 | VariantItem(Variant {kind: StructVariant(j), ..i2}) |
76 | }, | |
77 | _ => VariantItem(i2) | |
78 | } | |
79 | }, | |
80 | x => x | |
54a0048b SL |
81 | } |
82 | } | |
83 | ||
84 | /// don't override! | |
85 | fn fold_item_recur(&mut self, item: Item) -> Option<Item> { | |
86 | let Item { attrs, name, source, visibility, def_id, inner, stability, deprecation } = item; | |
87 | ||
88 | let inner = match inner { | |
89 | StrippedItem(box i) => StrippedItem(box self.fold_inner_recur(i)), | |
90 | _ => self.fold_inner_recur(inner), | |
1a4d82fc JJ |
91 | }; |
92 | ||
93 | Some(Item { attrs: attrs, name: name, source: source, inner: inner, | |
9cc50fc6 SL |
94 | visibility: visibility, stability: stability, deprecation: deprecation, |
95 | def_id: def_id }) | |
1a4d82fc JJ |
96 | } |
97 | ||
98 | fn fold_mod(&mut self, m: Module) -> Module { | |
99 | Module { | |
100 | is_crate: m.is_crate, | |
101 | items: m.items.into_iter().filter_map(|i| self.fold_item(i)).collect() | |
223e47cc LB |
102 | } |
103 | } | |
223e47cc | 104 | |
1a4d82fc | 105 | fn fold_crate(&mut self, mut c: Crate) -> Crate { |
54a0048b SL |
106 | c.module = c.module.and_then(|module| self.fold_item(module)); |
107 | ||
108 | c.external_traits = c.external_traits.into_iter().map(|(k, mut v)| { | |
109 | v.items = v.items.into_iter().filter_map(|i| self.fold_item(i)).collect(); | |
9346a6ac AL |
110 | (k, v) |
111 | }).collect(); | |
54a0048b | 112 | c |
223e47cc LB |
113 | } |
114 | } |