(index<- ) ./librustdoc/fold.rs
git branch: * master 5200215 auto merge of #14035 : alexcrichton/rust/experimental, r=huonw
modified: Wed Mar 26 11:50:03 2014
1 // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
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
11 use clean::*;
12 use std::iter::Extendable;
13 use std::mem::{replace, swap};
14
15 pub trait DocFolder {
16 fn fold_item(&mut self, item: Item) -> Option<Item> {
17 self.fold_item_recur(item)
18 }
19
20 /// don't override!
21 fn fold_item_recur(&mut self, item: Item) -> Option<Item> {
22 let Item { attrs, name, source, visibility, id, inner } = item;
23 let inner = inner;
24 let inner = match inner {
25 StructItem(mut i) => {
26 let mut foo = Vec::new(); swap(&mut foo, &mut i.fields);
27 let num_fields = foo.len();
28 i.fields.extend(foo.move_iter().filter_map(|x| self.fold_item(x)));
29 i.fields_stripped |= num_fields != i.fields.len();
30 StructItem(i)
31 },
32 ModuleItem(i) => {
33 ModuleItem(self.fold_mod(i))
34 },
35 EnumItem(mut i) => {
36 let mut foo = Vec::new(); swap(&mut foo, &mut i.variants);
37 let num_variants = foo.len();
38 i.variants.extend(foo.move_iter().filter_map(|x| self.fold_item(x)));
39 i.variants_stripped |= num_variants != i.variants.len();
40 EnumItem(i)
41 },
42 TraitItem(mut i) => {
43 fn vtrm<T: DocFolder>(this: &mut T, trm: TraitMethod) -> Option<TraitMethod> {
44 match trm {
45 Required(it) => {
46 match this.fold_item(it) {
47 Some(x) => return Some(Required(x)),
48 None => return None,
49 }
50 },
51 Provided(it) => {
52 match this.fold_item(it) {
53 Some(x) => return Some(Provided(x)),
54 None => return None,
55 }
56 },
57 }
58 }
59 let mut foo = Vec::new(); swap(&mut foo, &mut i.methods);
60 i.methods.extend(foo.move_iter().filter_map(|x| vtrm(self, x)));
61 TraitItem(i)
62 },
63 ImplItem(mut i) => {
64 let mut foo = Vec::new(); swap(&mut foo, &mut i.methods);
65 i.methods.extend(foo.move_iter().filter_map(|x| self.fold_item(x)));
66 ImplItem(i)
67 },
68 VariantItem(i) => {
69 let i2 = i.clone(); // this clone is small
70 match i.kind {
71 StructVariant(mut j) => {
72 let mut foo = Vec::new(); swap(&mut foo, &mut j.fields);
73 let num_fields = foo.len();
74 let c = |x| self.fold_item(x);
75 j.fields.extend(foo.move_iter().filter_map(c));
76 j.fields_stripped |= num_fields != j.fields.len();
77 VariantItem(Variant {kind: StructVariant(j), ..i2})
78 },
79 _ => VariantItem(i2)
80 }
81 },
82 x => x
83 };
84
85 Some(Item { attrs: attrs, name: name, source: source, inner: inner,
86 visibility: visibility, id: id })
87 }
88
89 fn fold_mod(&mut self, m: Module) -> Module {
90 Module {
91 is_crate: m.is_crate,
92 items: m.items.move_iter().filter_map(|i| self.fold_item(i)).collect()
93 }
94 }
95
96 fn fold_crate(&mut self, mut c: Crate) -> Crate {
97 c.module = match replace(&mut c.module, None) {
98 Some(module) => self.fold_item(module), None => None
99 };
100 return c;
101 }
102 }