fold.rs 4.4 KB
Newer Older
1
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2 3 4 5 6 7 8 9 10
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

11
use clean::*;
G
gamazeps 已提交
12
use std::iter::Extend;
13
use std::mem::{replace, swap};
14 15 16 17 18 19 20 21

pub trait DocFolder {
    fn fold_item(&mut self, item: Item) -> Option<Item> {
        self.fold_item_recur(item)
    }

    /// don't override!
    fn fold_item_recur(&mut self, item: Item) -> Option<Item> {
22
        let Item { attrs, name, source, visibility, def_id, inner, stability } = item;
23 24
        let inner = inner;
        let inner = match inner {
25
            StructItem(mut i) => {
26
                let mut foo = Vec::new(); swap(&mut foo, &mut i.fields);
S
Steven Fackler 已提交
27
                let num_fields = foo.len();
A
Aaron Turon 已提交
28
                i.fields.extend(foo.into_iter().filter_map(|x| self.fold_item(x)));
S
Steven Fackler 已提交
29
                i.fields_stripped |= num_fields != i.fields.len();
30 31 32 33 34
                StructItem(i)
            },
            ModuleItem(i) => {
                ModuleItem(self.fold_mod(i))
            },
35
            EnumItem(mut i) => {
36
                let mut foo = Vec::new(); swap(&mut foo, &mut i.variants);
S
Steven Fackler 已提交
37
                let num_variants = foo.len();
A
Aaron Turon 已提交
38
                i.variants.extend(foo.into_iter().filter_map(|x| self.fold_item(x)));
S
Steven Fackler 已提交
39
                i.variants_stripped |= num_variants != i.variants.len();
40 41
                EnumItem(i)
            },
42
            TraitItem(mut i) => {
43 44
                fn vtrm<T: DocFolder>(this: &mut T, trm: TraitMethod)
                        -> Option<TraitMethod> {
45
                    match trm {
46
                        RequiredMethod(it) => {
47
                            match this.fold_item(it) {
48
                                Some(x) => return Some(RequiredMethod(x)),
49 50 51
                                None => return None,
                            }
                        },
52
                        ProvidedMethod(it) => {
53
                            match this.fold_item(it) {
54
                                Some(x) => return Some(ProvidedMethod(x)),
55 56 57
                                None => return None,
                            }
                        },
58 59 60 61 62 63
                        TypeTraitItem(it) => {
                            match this.fold_item(it) {
                                Some(x) => return Some(TypeTraitItem(x)),
                                None => return None,
                            }
                        }
64 65
                    }
                }
66
                let mut foo = Vec::new(); swap(&mut foo, &mut i.items);
A
Aaron Turon 已提交
67
                i.items.extend(foo.into_iter().filter_map(|x| vtrm(self, x)));
68 69
                TraitItem(i)
            },
70
            ImplItem(mut i) => {
71
                let mut foo = Vec::new(); swap(&mut foo, &mut i.items);
A
Aaron Turon 已提交
72
                i.items.extend(foo.into_iter()
73
                                  .filter_map(|x| self.fold_item(x)));
74 75 76 77 78
                ImplItem(i)
            },
            VariantItem(i) => {
                let i2 = i.clone(); // this clone is small
                match i.kind {
79
                    StructVariant(mut j) => {
80
                        let mut foo = Vec::new(); swap(&mut foo, &mut j.fields);
S
Steven Fackler 已提交
81
                        let num_fields = foo.len();
J
Jorge Aparicio 已提交
82
                        j.fields.extend(foo.into_iter().filter_map(|x| self.fold_item(x)));
S
Steven Fackler 已提交
83
                        j.fields_stripped |= num_fields != j.fields.len();
84 85 86 87 88 89 90 91 92
                        VariantItem(Variant {kind: StructVariant(j), ..i2})
                    },
                    _ => VariantItem(i2)
                }
            },
            x => x
        };

        Some(Item { attrs: attrs, name: name, source: source, inner: inner,
93
                    visibility: visibility, stability: stability, def_id: def_id })
94 95 96
    }

    fn fold_mod(&mut self, m: Module) -> Module {
97 98
        Module {
            is_crate: m.is_crate,
A
Aaron Turon 已提交
99
            items: m.items.into_iter().filter_map(|i| self.fold_item(i)).collect()
100
        }
101 102 103
    }

    fn fold_crate(&mut self, mut c: Crate) -> Crate {
104
        c.module = match replace(&mut c.module, None) {
105 106 107
            Some(module) => self.fold_item(module), None => None
        };
        return c;
108 109
    }
}