Skip to content

Commit a3bb858

Browse files
committed
rustdoc: Refactored various uses of ItemType.
In particular, ItemType variants are no longer reexported. Since we already do namespace them via `item_type` mod, it's fine.
1 parent 131d4ed commit a3bb858

File tree

3 files changed

+129
-120
lines changed

3 files changed

+129
-120
lines changed

src/librustdoc/html/format.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ use syntax::ast_util;
2323

2424
use clean;
2525
use stability_summary::ModuleSummary;
26-
use html::item_type;
2726
use html::item_type::ItemType;
2827
use html::render;
2928
use html::render::{cache, CURRENT_LOCATION_KEY};
@@ -283,7 +282,7 @@ fn path(w: &mut fmt::Formatter, path: &clean::Path, print_all: bool,
283282
url.push_str("/");
284283
}
285284
match shortty {
286-
item_type::Module => {
285+
ItemType::Module => {
287286
url.push_str(fqp.last().unwrap().as_slice());
288287
url.push_str("/index.html");
289288
}

src/librustdoc/html/item_type.rs

+56-44
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
// except according to those terms.
1010

1111
//! Item types.
12-
pub use self::ItemType::*;
1312
1413
use std::fmt;
1514
use clean;
@@ -44,27 +43,64 @@ pub enum ItemType {
4443
}
4544

4645
impl ItemType {
46+
pub fn from_item(item: &clean::Item) -> ItemType {
47+
match item.inner {
48+
clean::ModuleItem(..) => ItemType::Module,
49+
clean::StructItem(..) => ItemType::Struct,
50+
clean::EnumItem(..) => ItemType::Enum,
51+
clean::FunctionItem(..) => ItemType::Function,
52+
clean::TypedefItem(..) => ItemType::Typedef,
53+
clean::StaticItem(..) => ItemType::Static,
54+
clean::ConstantItem(..) => ItemType::Constant,
55+
clean::TraitItem(..) => ItemType::Trait,
56+
clean::ImplItem(..) => ItemType::Impl,
57+
clean::ViewItemItem(..) => ItemType::ViewItem,
58+
clean::TyMethodItem(..) => ItemType::TyMethod,
59+
clean::MethodItem(..) => ItemType::Method,
60+
clean::StructFieldItem(..) => ItemType::StructField,
61+
clean::VariantItem(..) => ItemType::Variant,
62+
clean::ForeignFunctionItem(..) => ItemType::ForeignFunction,
63+
clean::ForeignStaticItem(..) => ItemType::ForeignStatic,
64+
clean::MacroItem(..) => ItemType::Macro,
65+
clean::PrimitiveItem(..) => ItemType::Primitive,
66+
clean::AssociatedTypeItem(..) => ItemType::AssociatedType,
67+
}
68+
}
69+
70+
pub fn from_type_kind(kind: clean::TypeKind) -> ItemType {
71+
match kind {
72+
clean::TypeStruct => ItemType::Struct,
73+
clean::TypeEnum => ItemType::Enum,
74+
clean::TypeFunction => ItemType::Function,
75+
clean::TypeTrait => ItemType::Trait,
76+
clean::TypeModule => ItemType::Module,
77+
clean::TypeStatic => ItemType::Static,
78+
clean::TypeVariant => ItemType::Variant,
79+
clean::TypeTypedef => ItemType::Typedef,
80+
}
81+
}
82+
4783
pub fn to_static_str(&self) -> &'static str {
4884
match *self {
49-
Module => "mod",
50-
Struct => "struct",
51-
Enum => "enum",
52-
Function => "fn",
53-
Typedef => "type",
54-
Static => "static",
55-
Trait => "trait",
56-
Impl => "impl",
57-
ViewItem => "viewitem",
58-
TyMethod => "tymethod",
59-
Method => "method",
60-
StructField => "structfield",
61-
Variant => "variant",
62-
ForeignFunction => "ffi",
63-
ForeignStatic => "ffs",
64-
Macro => "macro",
65-
Primitive => "primitive",
66-
AssociatedType => "associatedtype",
67-
Constant => "constant",
85+
ItemType::Module => "mod",
86+
ItemType::Struct => "struct",
87+
ItemType::Enum => "enum",
88+
ItemType::Function => "fn",
89+
ItemType::Typedef => "type",
90+
ItemType::Static => "static",
91+
ItemType::Trait => "trait",
92+
ItemType::Impl => "impl",
93+
ItemType::ViewItem => "viewitem",
94+
ItemType::TyMethod => "tymethod",
95+
ItemType::Method => "method",
96+
ItemType::StructField => "structfield",
97+
ItemType::Variant => "variant",
98+
ItemType::ForeignFunction => "ffi",
99+
ItemType::ForeignStatic => "ffs",
100+
ItemType::Macro => "macro",
101+
ItemType::Primitive => "primitive",
102+
ItemType::AssociatedType => "associatedtype",
103+
ItemType::Constant => "constant",
68104
}
69105
}
70106
}
@@ -75,27 +111,3 @@ impl fmt::Show for ItemType {
75111
}
76112
}
77113

78-
pub fn shortty(item: &clean::Item) -> ItemType {
79-
match item.inner {
80-
clean::ModuleItem(..) => Module,
81-
clean::StructItem(..) => Struct,
82-
clean::EnumItem(..) => Enum,
83-
clean::FunctionItem(..) => Function,
84-
clean::TypedefItem(..) => Typedef,
85-
clean::StaticItem(..) => Static,
86-
clean::ConstantItem(..) => Constant,
87-
clean::TraitItem(..) => Trait,
88-
clean::ImplItem(..) => Impl,
89-
clean::ViewItemItem(..) => ViewItem,
90-
clean::TyMethodItem(..) => TyMethod,
91-
clean::MethodItem(..) => Method,
92-
clean::StructFieldItem(..) => StructField,
93-
clean::VariantItem(..) => Variant,
94-
clean::ForeignFunctionItem(..) => ForeignFunction,
95-
clean::ForeignStaticItem(..) => ForeignStatic,
96-
clean::MacroItem(..) => Macro,
97-
clean::PrimitiveItem(..) => Primitive,
98-
clean::AssociatedTypeItem(..) => AssociatedType,
99-
}
100-
}
101-

src/librustdoc/html/render.rs

+72-74
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ use fold::DocFolder;
6161
use html::format::{VisSpace, Method, FnStyleSpace, MutableSpace, Stability};
6262
use html::format::{ConciseStability, TyParamBounds, WhereClause};
6363
use html::highlight;
64-
use html::item_type::{ItemType, shortty};
65-
use html::item_type;
64+
use html::item_type::ItemType;
6665
use html::layout;
6766
use html::markdown::Markdown;
6867
use html::markdown;
@@ -314,19 +313,8 @@ pub fn run(mut krate: clean::Crate,
314313
let paths: HashMap<ast::DefId, (Vec<String>, ItemType)> =
315314
analysis.as_ref().map(|a| {
316315
let paths = a.external_paths.borrow_mut().take().unwrap();
317-
paths.into_iter().map(|(k, (v, t))| {
318-
(k, (v, match t {
319-
clean::TypeStruct => item_type::Struct,
320-
clean::TypeEnum => item_type::Enum,
321-
clean::TypeFunction => item_type::Function,
322-
clean::TypeTrait => item_type::Trait,
323-
clean::TypeModule => item_type::Module,
324-
clean::TypeStatic => item_type::Static,
325-
clean::TypeVariant => item_type::Variant,
326-
clean::TypeTypedef => item_type::Typedef,
327-
}))
328-
}).collect()
329-
}).unwrap_or(HashMap::new());
316+
paths.into_iter().map(|(k, (v, t))| (k, (v, ItemType::from_type_kind(t)))).collect()
317+
}).unwrap_or(HashMap::new());
330318
let mut cache = Cache {
331319
impls: HashMap::new(),
332320
external_paths: paths.iter().map(|(&k, v)| (k, v.ref0().clone()))
@@ -359,7 +347,7 @@ pub fn run(mut krate: clean::Crate,
359347
for &(n, ref e) in krate.externs.iter() {
360348
cache.extern_locations.insert(n, extern_location(e, &cx.dst));
361349
let did = ast::DefId { krate: n, node: ast::CRATE_NODE_ID };
362-
cache.paths.insert(did, (vec![e.name.to_string()], item_type::Module));
350+
cache.paths.insert(did, (vec![e.name.to_string()], ItemType::Module));
363351
}
364352

365353
// Cache where all known primitives have their documentation located.
@@ -642,6 +630,11 @@ fn mkdir(path: &Path) -> io::IoResult<()> {
642630
}
643631
}
644632

633+
/// Returns a documentation-level item type from the item.
634+
fn shortty(item: &clean::Item) -> ItemType {
635+
ItemType::from_item(item)
636+
}
637+
645638
/// Takes a path to a source file and cleans the path to it. This canonicalizes
646639
/// things like ".." to components which preserve the "top down" hierarchy of a
647640
/// static HTML tree.
@@ -855,13 +848,13 @@ impl DocFolder for Cache {
855848
let last = self.parent_stack.last().unwrap();
856849
let did = *last;
857850
let path = match self.paths.get(&did) {
858-
Some(&(_, item_type::Trait)) =>
851+
Some(&(_, ItemType::Trait)) =>
859852
Some(self.stack[..self.stack.len() - 1]),
860853
// The current stack not necessarily has correlation for
861854
// where the type was defined. On the other hand,
862855
// `paths` always has the right information if present.
863-
Some(&(ref fqp, item_type::Struct)) |
864-
Some(&(ref fqp, item_type::Enum)) =>
856+
Some(&(ref fqp, ItemType::Struct)) |
857+
Some(&(ref fqp, ItemType::Enum)) =>
865858
Some(fqp[..fqp.len() - 1]),
866859
Some(..) => Some(self.stack.as_slice()),
867860
None => None
@@ -929,7 +922,7 @@ impl DocFolder for Cache {
929922
clean::VariantItem(..) if !self.privmod => {
930923
let mut stack = self.stack.clone();
931924
stack.pop();
932-
self.paths.insert(item.def_id, (stack, item_type::Enum));
925+
self.paths.insert(item.def_id, (stack, ItemType::Enum));
933926
}
934927

935928
clean::PrimitiveItem(..) if item.visibility.is_some() => {
@@ -1491,45 +1484,50 @@ fn item_module(w: &mut fmt::Formatter, cx: &Context,
14911484
!cx.ignore_private_item(&items[*i])
14921485
}).collect::<Vec<uint>>();
14931486

1487+
// the order of item types in the listing
1488+
fn reorder(ty: ItemType) -> u8 {
1489+
match ty {
1490+
ItemType::ViewItem => 0,
1491+
ItemType::Primitive => 1,
1492+
ItemType::Module => 2,
1493+
ItemType::Macro => 3,
1494+
ItemType::Struct => 4,
1495+
ItemType::Enum => 5,
1496+
ItemType::Constant => 6,
1497+
ItemType::Static => 7,
1498+
ItemType::ForeignFunction => 8,
1499+
ItemType::ForeignStatic => 9,
1500+
ItemType::Trait => 10,
1501+
ItemType::Function => 11,
1502+
ItemType::Typedef => 12,
1503+
_ => 13 + ty as u8,
1504+
}
1505+
}
1506+
14941507
fn cmp(i1: &clean::Item, i2: &clean::Item, idx1: uint, idx2: uint) -> Ordering {
1495-
if shortty(i1) == shortty(i2) {
1508+
let ty1 = shortty(i1);
1509+
let ty2 = shortty(i2);
1510+
if ty1 == ty2 {
14961511
return i1.name.cmp(&i2.name);
14971512
}
1498-
match (&i1.inner, &i2.inner) {
1499-
(&clean::ViewItemItem(ref a), &clean::ViewItemItem(ref b)) => {
1500-
match (&a.inner, &b.inner) {
1501-
(&clean::ExternCrate(..), _) => Less,
1502-
(_, &clean::ExternCrate(..)) => Greater,
1503-
_ => idx1.cmp(&idx2),
1513+
1514+
let tycmp = reorder(ty1).cmp(&reorder(ty2));
1515+
if let Equal = tycmp {
1516+
// for reexports, `extern crate` takes precedence.
1517+
match (&i1.inner, &i2.inner) {
1518+
(&clean::ViewItemItem(ref a), &clean::ViewItemItem(ref b)) => {
1519+
match (&a.inner, &b.inner) {
1520+
(&clean::ExternCrate(..), _) => return Less,
1521+
(_, &clean::ExternCrate(..)) => return Greater,
1522+
_ => {}
1523+
}
15041524
}
1525+
(_, _) => {}
15051526
}
1506-
(&clean::ViewItemItem(..), _) => Less,
1507-
(_, &clean::ViewItemItem(..)) => Greater,
1508-
(&clean::PrimitiveItem(..), _) => Less,
1509-
(_, &clean::PrimitiveItem(..)) => Greater,
1510-
(&clean::ModuleItem(..), _) => Less,
1511-
(_, &clean::ModuleItem(..)) => Greater,
1512-
(&clean::MacroItem(..), _) => Less,
1513-
(_, &clean::MacroItem(..)) => Greater,
1514-
(&clean::StructItem(..), _) => Less,
1515-
(_, &clean::StructItem(..)) => Greater,
1516-
(&clean::EnumItem(..), _) => Less,
1517-
(_, &clean::EnumItem(..)) => Greater,
1518-
(&clean::ConstantItem(..), _) => Less,
1519-
(_, &clean::ConstantItem(..)) => Greater,
1520-
(&clean::StaticItem(..), _) => Less,
1521-
(_, &clean::StaticItem(..)) => Greater,
1522-
(&clean::ForeignFunctionItem(..), _) => Less,
1523-
(_, &clean::ForeignFunctionItem(..)) => Greater,
1524-
(&clean::ForeignStaticItem(..), _) => Less,
1525-
(_, &clean::ForeignStaticItem(..)) => Greater,
1526-
(&clean::TraitItem(..), _) => Less,
1527-
(_, &clean::TraitItem(..)) => Greater,
1528-
(&clean::FunctionItem(..), _) => Less,
1529-
(_, &clean::FunctionItem(..)) => Greater,
1530-
(&clean::TypedefItem(..), _) => Less,
1531-
(_, &clean::TypedefItem(..)) => Greater,
1532-
_ => idx1.cmp(&idx2),
1527+
1528+
idx1.cmp(&idx2)
1529+
} else {
1530+
tycmp
15331531
}
15341532
}
15351533

@@ -1546,26 +1544,26 @@ fn item_module(w: &mut fmt::Formatter, cx: &Context,
15461544
try!(write!(w, "</table>"));
15471545
}
15481546
curty = myty;
1549-
let (short, name) = match myitem.inner {
1550-
clean::ModuleItem(..) => ("modules", "Modules"),
1551-
clean::StructItem(..) => ("structs", "Structs"),
1552-
clean::EnumItem(..) => ("enums", "Enums"),
1553-
clean::FunctionItem(..) => ("functions", "Functions"),
1554-
clean::TypedefItem(..) => ("types", "Type Definitions"),
1555-
clean::StaticItem(..) => ("statics", "Statics"),
1556-
clean::ConstantItem(..) => ("constants", "Constants"),
1557-
clean::TraitItem(..) => ("traits", "Traits"),
1558-
clean::ImplItem(..) => ("impls", "Implementations"),
1559-
clean::ViewItemItem(..) => ("reexports", "Reexports"),
1560-
clean::TyMethodItem(..) => ("tymethods", "Type Methods"),
1561-
clean::MethodItem(..) => ("methods", "Methods"),
1562-
clean::StructFieldItem(..) => ("fields", "Struct Fields"),
1563-
clean::VariantItem(..) => ("variants", "Variants"),
1564-
clean::ForeignFunctionItem(..) => ("ffi-fns", "Foreign Functions"),
1565-
clean::ForeignStaticItem(..) => ("ffi-statics", "Foreign Statics"),
1566-
clean::MacroItem(..) => ("macros", "Macros"),
1567-
clean::PrimitiveItem(..) => ("primitives", "Primitive Types"),
1568-
clean::AssociatedTypeItem(..) => ("associated-types", "Associated Types"),
1547+
let (short, name) = match myty.unwrap() {
1548+
ItemType::Module => ("modules", "Modules"),
1549+
ItemType::Struct => ("structs", "Structs"),
1550+
ItemType::Enum => ("enums", "Enums"),
1551+
ItemType::Function => ("functions", "Functions"),
1552+
ItemType::Typedef => ("types", "Type Definitions"),
1553+
ItemType::Static => ("statics", "Statics"),
1554+
ItemType::Constant => ("constants", "Constants"),
1555+
ItemType::Trait => ("traits", "Traits"),
1556+
ItemType::Impl => ("impls", "Implementations"),
1557+
ItemType::ViewItem => ("reexports", "Reexports"),
1558+
ItemType::TyMethod => ("tymethods", "Type Methods"),
1559+
ItemType::Method => ("methods", "Methods"),
1560+
ItemType::StructField => ("fields", "Struct Fields"),
1561+
ItemType::Variant => ("variants", "Variants"),
1562+
ItemType::ForeignFunction => ("ffi-fns", "Foreign Functions"),
1563+
ItemType::ForeignStatic => ("ffi-statics", "Foreign Statics"),
1564+
ItemType::Macro => ("macros", "Macros"),
1565+
ItemType::Primitive => ("primitives", "Primitive Types"),
1566+
ItemType::AssociatedType => ("associated-types", "Associated Types"),
15691567
};
15701568
try!(write!(w,
15711569
"<h2 id='{id}' class='section-header'>\

0 commit comments

Comments
 (0)