1 // Copyright 2014 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.
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.
11 //! This module crawls a `clean::Crate` and produces a summarization of the
12 //! stability levels within the crate. The summary contains the module
13 //! hierarchy, with item counts for every stability level per module. A parent
14 //! module's count includes its children's.
16 use std::cmp::Ordering;
19 use syntax::attr::{Deprecated, Experimental, Unstable, Stable, Frozen, Locked};
20 use syntax::ast::Public;
22 use clean::{Crate, Item, ModuleItem, Module, EnumItem, Enum};
23 use clean::{ImplItem, Impl, Trait, TraitItem, TraitMethod, ProvidedMethod, RequiredMethod};
24 use clean::{TypeTraitItem, ViewItemItem, PrimitiveItem, Stability};
26 use html::render::cache;
28 #[derive(RustcEncodable, RustcDecodable, PartialEq, Eq)]
29 /// The counts for each stability level.
33 pub experimental: uint,
39 /// No stability level, inherited or otherwise.
46 fn add(self, other: Counts) -> Counts {
48 deprecated: self.deprecated + other.deprecated,
49 experimental: self.experimental + other.experimental,
50 unstable: self.unstable + other.unstable,
51 stable: self.stable + other.stable,
52 frozen: self.frozen + other.frozen,
53 locked: self.locked + other.locked,
54 unmarked: self.unmarked + other.unmarked,
72 pub fn total(&self) -> uint {
73 self.deprecated + self.experimental + self.unstable + self.stable +
74 self.frozen + self.locked + self.unmarked
78 #[derive(RustcEncodable, RustcDecodable, PartialEq, Eq)]
79 /// A summarized module, which includes total counts and summarized children
81 pub struct ModuleSummary {
84 pub submodules: Vec<ModuleSummary>,
87 impl PartialOrd for ModuleSummary {
88 fn partial_cmp(&self, other: &ModuleSummary) -> Option<Ordering> {
89 self.name.partial_cmp(&other.name)
93 impl Ord for ModuleSummary {
94 fn cmp(&self, other: &ModuleSummary) -> Ordering {
95 self.name.cmp(&other.name)
99 // is the item considered publically visible?
100 fn visible(item: &Item) -> bool {
103 _ => item.visibility == Some(Public)
107 fn count_stability(stab: Option<&Stability>) -> Counts {
109 None => Counts { unmarked: 1, .. Counts::zero() },
110 Some(ref stab) => match stab.level {
111 Deprecated => Counts { deprecated: 1, .. Counts::zero() },
112 Experimental => Counts { experimental: 1, .. Counts::zero() },
113 Unstable => Counts { unstable: 1, .. Counts::zero() },
114 Stable => Counts { stable: 1, .. Counts::zero() },
115 Frozen => Counts { frozen: 1, .. Counts::zero() },
116 Locked => Counts { locked: 1, .. Counts::zero() },
121 fn summarize_methods(item: &Item) -> Counts {
122 match cache().impls.get(&item.def_id) {
125 let count = count_stability(i.stability.as_ref());
126 if i.impl_.trait_.is_none() {
127 count + i.impl_.items.iter()
128 .map(|ti| summarize_item(ti).0)
129 .fold(Counts::zero(), |acc, c| acc + c)
133 }).fold(Counts::zero(), |acc, c| acc + c)
142 // Produce the summary for an arbitrary item. If the item is a module, include a
143 // module summary. The counts for items with nested items (e.g. modules, traits,
144 // impls) include all children counts.
145 fn summarize_item(item: &Item) -> (Counts, Option<ModuleSummary>) {
146 let item_counts = count_stability(item.stability.as_ref()) + summarize_methods(item);
148 // Count this item's children, if any. Note that a trait impl is
149 // considered to have no children.
151 // Require explicit `pub` to be visible
152 ImplItem(Impl { items: ref subitems, trait_: None, .. }) => {
153 let subcounts = subitems.iter().filter(|i| visible(*i))
156 .fold(Counts::zero(), |acc, x| acc + x);
159 // `pub` automatically
160 EnumItem(Enum { variants: ref subitems, .. }) => {
161 let subcounts = subitems.iter().map(summarize_item)
163 .fold(Counts::zero(), |acc, x| acc + x);
164 (item_counts + subcounts, None)
167 items: ref trait_items,
170 fn extract_item<'a>(trait_item: &'a TraitMethod) -> &'a Item {
172 ProvidedMethod(ref item) |
173 RequiredMethod(ref item) |
174 TypeTraitItem(ref item) => item
177 let subcounts = trait_items.iter()
181 .fold(Counts::zero(), |acc, x| acc + x);
182 (item_counts + subcounts, None)
184 ModuleItem(Module { ref items, .. }) => {
185 let mut counts = item_counts;
186 let mut submodules = Vec::new();
188 for (subcounts, submodule) in items.iter().filter(|i| visible(*i))
189 .map(summarize_item) {
190 counts = counts + subcounts;
191 submodule.map(|m| submodules.push(m));
195 (counts, Some(ModuleSummary {
196 name: item.name.as_ref().map_or("".to_string(), |n| n.clone()),
198 submodules: submodules,
201 // no stability information for the following items:
202 ViewItemItem(_) | PrimitiveItem(_) => (Counts::zero(), None),
203 _ => (item_counts, None)
207 /// Summarizes the stability levels in a crate.
208 pub fn build(krate: &Crate) -> ModuleSummary {
210 None => ModuleSummary {
211 name: krate.name.clone(),
212 counts: Counts::zero(),
213 submodules: Vec::new(),
215 Some(ref item) => ModuleSummary {
216 name: krate.name.clone(), .. summarize_item(item).1.unwrap()