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;
20 use syntax::attr::{Deprecated, Experimental, Unstable, Stable, Frozen, Locked};
21 use syntax::ast::Public;
23 use clean::{Crate, Item, ModuleItem, Module, EnumItem, Enum};
24 use clean::{ImplItem, Impl, Trait, TraitItem, TraitMethod, ProvidedMethod, RequiredMethod};
25 use clean::{TypeTraitItem, ViewItemItem, PrimitiveItem, Stability};
27 use html::render::cache;
29 #[deriving(Zero, RustcEncodable, RustcDecodable, PartialEq, Eq)]
30 /// The counts for each stability level.
34 pub experimental: uint,
40 /// No stability level, inherited or otherwise.
47 fn add(self, other: Counts) -> Counts {
49 deprecated: self.deprecated + other.deprecated,
50 experimental: self.experimental + other.experimental,
51 unstable: self.unstable + other.unstable,
52 stable: self.stable + other.stable,
53 frozen: self.frozen + other.frozen,
54 locked: self.locked + other.locked,
55 unmarked: self.unmarked + other.unmarked,
73 pub fn total(&self) -> uint {
74 self.deprecated + self.experimental + self.unstable + self.stable +
75 self.frozen + self.locked + self.unmarked
79 #[deriving(RustcEncodable, RustcDecodable, PartialEq, Eq)]
80 /// A summarized module, which includes total counts and summarized children
82 pub struct ModuleSummary {
85 pub submodules: Vec<ModuleSummary>,
88 impl PartialOrd for ModuleSummary {
89 fn partial_cmp(&self, other: &ModuleSummary) -> Option<Ordering> {
90 self.name.partial_cmp(&other.name)
94 impl Ord for ModuleSummary {
95 fn cmp(&self, other: &ModuleSummary) -> Ordering {
96 self.name.cmp(&other.name)
100 // is the item considered publically visible?
101 fn visible(item: &Item) -> bool {
104 _ => item.visibility == Some(Public)
108 fn count_stability(stab: Option<&Stability>) -> Counts {
110 None => Counts { unmarked: 1, .. Counts::zero() },
111 Some(ref stab) => match stab.level {
112 Deprecated => Counts { deprecated: 1, .. Counts::zero() },
113 Experimental => Counts { experimental: 1, .. Counts::zero() },
114 Unstable => Counts { unstable: 1, .. Counts::zero() },
115 Stable => Counts { stable: 1, .. Counts::zero() },
116 Frozen => Counts { frozen: 1, .. Counts::zero() },
117 Locked => Counts { locked: 1, .. Counts::zero() },
122 fn summarize_methods(item: &Item) -> Counts {
123 match cache().impls.get(&item.def_id) {
126 let count = count_stability(i.stability.as_ref());
127 if i.impl_.trait_.is_none() {
128 count + i.impl_.items.iter()
129 .map(|ti| summarize_item(ti).0)
130 .fold(Counts::zero(), |acc, c| acc + c)
134 }).fold(Counts::zero(), |acc, c| acc + c)
143 // Produce the summary for an arbitrary item. If the item is a module, include a
144 // module summary. The counts for items with nested items (e.g. modules, traits,
145 // impls) include all children counts.
146 fn summarize_item(item: &Item) -> (Counts, Option<ModuleSummary>) {
147 let item_counts = count_stability(item.stability.as_ref()) + summarize_methods(item);
149 // Count this item's children, if any. Note that a trait impl is
150 // considered to have no children.
152 // Require explicit `pub` to be visible
153 ImplItem(Impl { items: ref subitems, trait_: None, .. }) => {
154 let subcounts = subitems.iter().filter(|i| visible(*i))
157 .fold(Counts::zero(), |acc, x| acc + x);
160 // `pub` automatically
161 EnumItem(Enum { variants: ref subitems, .. }) => {
162 let subcounts = subitems.iter().map(summarize_item)
164 .fold(Counts::zero(), |acc, x| acc + x);
165 (item_counts + subcounts, None)
168 items: ref trait_items,
171 fn extract_item<'a>(trait_item: &'a TraitMethod) -> &'a Item {
173 ProvidedMethod(ref item) |
174 RequiredMethod(ref item) |
175 TypeTraitItem(ref item) => item
178 let subcounts = trait_items.iter()
182 .fold(Counts::zero(), |acc, x| acc + x);
183 (item_counts + subcounts, None)
185 ModuleItem(Module { ref items, .. }) => {
186 let mut counts = item_counts;
187 let mut submodules = Vec::new();
189 for (subcounts, submodule) in items.iter().filter(|i| visible(*i))
190 .map(summarize_item) {
191 counts = counts + subcounts;
192 submodule.map(|m| submodules.push(m));
196 (counts, Some(ModuleSummary {
197 name: item.name.as_ref().map_or("".to_string(), |n| n.clone()),
199 submodules: submodules,
202 // no stability information for the following items:
203 ViewItemItem(_) | PrimitiveItem(_) => (Counts::zero(), None),
204 _ => (item_counts, None)
208 /// Summarizes the stability levels in a crate.
209 pub fn build(krate: &Crate) -> ModuleSummary {
211 None => ModuleSummary {
212 name: krate.name.clone(),
213 counts: Counts::zero(),
214 submodules: Vec::new(),
216 Some(ref item) => ModuleSummary {
217 name: krate.name.clone(), .. summarize_item(item).1.unwrap()