Skip to content

Commit c6ed447

Browse files
committed
rustdoc: Begin support for structs
1 parent bd4365f commit c6ed447

File tree

6 files changed

+159
-20
lines changed

6 files changed

+159
-20
lines changed

src/rustdoc/doc.rs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ enum ItemTag {
9898
EnumTag(EnumDoc),
9999
TraitTag(TraitDoc),
100100
ImplTag(ImplDoc),
101-
TyTag(TyDoc)
101+
TyTag(TyDoc),
102+
StructTag(StructDoc)
102103
}
103104

104105
impl ItemTag : cmp::Eq {
@@ -152,6 +153,12 @@ impl ItemTag : cmp::Eq {
152153
_ => false
153154
}
154155
}
156+
StructTag(e0a) => {
157+
match other {
158+
StructTag(e0b) => e0a == e0b,
159+
_ => false
160+
}
161+
}
155162
}
156163
}
157164
pure fn ne(&&other: ItemTag) -> bool { !self.eq(other) }
@@ -315,6 +322,21 @@ impl ImplDoc : cmp::Eq {
315322

316323
type TyDoc = SimpleItemDoc;
317324

325+
type StructDoc = {
326+
item: ItemDoc,
327+
fields: ~[~str],
328+
sig: Option<~str>
329+
};
330+
331+
impl StructDoc : cmp::Eq {
332+
pure fn eq(&&other: StructDoc) -> bool {
333+
return self.item == other.item
334+
&& self.fields == other.fields
335+
&& self.sig == other.sig;
336+
}
337+
pure fn ne(&&other: StructDoc) -> bool { !self.eq(other) }
338+
}
339+
318340
type Index = {
319341
entries: ~[IndexEntry]
320342
};
@@ -442,6 +464,15 @@ impl ModDoc {
442464
}
443465
}
444466
}
467+
468+
fn structs() -> ~[StructDoc] {
469+
do vec::filter_map(self.items) |itemtag| {
470+
match itemtag {
471+
StructTag(StructDoc) => Some(StructDoc),
472+
_ => None
473+
}
474+
}
475+
}
445476
}
446477

447478
trait PageUtils {
@@ -544,7 +575,8 @@ impl ItemTag: Item {
544575
doc::EnumTag(doc) => doc.item,
545576
doc::TraitTag(doc) => doc.item,
546577
doc::ImplTag(doc) => doc.item,
547-
doc::TyTag(doc) => doc.item
578+
doc::TyTag(doc) => doc.item,
579+
doc::StructTag(doc) => doc.item
548580
}
549581
}
550582
}
@@ -573,6 +605,10 @@ impl ImplDoc: Item {
573605
pure fn item() -> ItemDoc { self.item }
574606
}
575607

608+
impl StructDoc: Item {
609+
pure fn item() -> ItemDoc { self.item }
610+
}
611+
576612
trait ItemUtils {
577613
pure fn id() -> AstId;
578614
pure fn name() -> ~str;

src/rustdoc/extract.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ fn moddoc_from_mod(
119119
tydoc_from_ty(ItemDoc)
120120
))
121121
}
122+
ast::item_class(def, _) => {
123+
Some(doc::StructTag(
124+
structdoc_from_struct(ItemDoc, def)
125+
))
126+
}
122127
_ => None
123128
}
124129
},
@@ -293,6 +298,34 @@ fn should_extract_tys() {
293298
assert doc.cratemod().types()[0].name() == ~"a";
294299
}
295300

301+
fn structdoc_from_struct(
302+
itemdoc: doc::ItemDoc,
303+
struct_def: @ast::struct_def
304+
) -> doc::StructDoc {
305+
{
306+
item: itemdoc,
307+
fields: do struct_def.fields.map |field| {
308+
match field.node.kind {
309+
ast::named_field(ident, _, _) => to_str(ident),
310+
ast::unnamed_field => fail ~"what is an unnamed struct field?"
311+
}
312+
},
313+
sig: None
314+
}
315+
}
316+
317+
#[test]
318+
fn should_extract_structs() {
319+
let doc = test::mk_doc(~"struct Foo { field: () }");
320+
assert doc.cratemod().structs()[0].name() == ~"Foo";
321+
}
322+
323+
#[test]
324+
fn should_extract_struct_fields() {
325+
let doc = test::mk_doc(~"struct Foo { field: () }");
326+
assert doc.cratemod().structs()[0].fields[0] == ~"field";
327+
}
328+
296329
#[cfg(test)]
297330
mod test {
298331

src/rustdoc/fold.rs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export default_seq_fold_enum;
1111
export default_seq_fold_trait;
1212
export default_seq_fold_impl;
1313
export default_seq_fold_type;
14+
export default_seq_fold_struct;
1415
export default_par_fold;
1516
export default_par_fold_mod;
1617
export default_par_fold_nmod;
@@ -31,6 +32,8 @@ type FoldEnum<T> = fn~(fold: Fold<T>, doc: doc::EnumDoc) -> doc::EnumDoc;
3132
type FoldTrait<T> = fn~(fold: Fold<T>, doc: doc::TraitDoc) -> doc::TraitDoc;
3233
type FoldImpl<T> = fn~(fold: Fold<T>, doc: doc::ImplDoc) -> doc::ImplDoc;
3334
type FoldType<T> = fn~(fold: Fold<T>, doc: doc::TyDoc) -> doc::TyDoc;
35+
type FoldStruct<T> = fn~(fold: Fold<T>,
36+
doc: doc::StructDoc) -> doc::StructDoc;
3437

3538
type Fold_<T> = {
3639
ctxt: T,
@@ -44,7 +47,8 @@ type Fold_<T> = {
4447
fold_enum: FoldEnum<T>,
4548
fold_trait: FoldTrait<T>,
4649
fold_impl: FoldImpl<T>,
47-
fold_type: FoldType<T>
50+
fold_type: FoldType<T>,
51+
fold_struct: FoldStruct<T>
4852
};
4953

5054

@@ -62,7 +66,8 @@ fn mk_fold<T:Copy>(
6266
+fold_enum: FoldEnum<T>,
6367
+fold_trait: FoldTrait<T>,
6468
+fold_impl: FoldImpl<T>,
65-
+fold_type: FoldType<T>
69+
+fold_type: FoldType<T>,
70+
+fold_struct: FoldStruct<T>
6671
) -> Fold<T> {
6772
Fold({
6873
ctxt: ctxt,
@@ -76,7 +81,8 @@ fn mk_fold<T:Copy>(
7681
fold_enum: fold_enum,
7782
fold_trait: fold_trait,
7883
fold_impl: fold_impl,
79-
fold_type: fold_type
84+
fold_type: fold_type,
85+
fold_struct: fold_struct,
8086
})
8187
}
8288

@@ -93,7 +99,8 @@ fn default_any_fold<T:Send Copy>(ctxt: T) -> Fold<T> {
9399
|f, d| default_seq_fold_enum(f, d),
94100
|f, d| default_seq_fold_trait(f, d),
95101
|f, d| default_seq_fold_impl(f, d),
96-
|f, d| default_seq_fold_type(f, d)
102+
|f, d| default_seq_fold_type(f, d),
103+
|f, d| default_seq_fold_struct(f, d)
97104
)
98105
}
99106

@@ -110,7 +117,8 @@ fn default_seq_fold<T:Copy>(ctxt: T) -> Fold<T> {
110117
|f, d| default_seq_fold_enum(f, d),
111118
|f, d| default_seq_fold_trait(f, d),
112119
|f, d| default_seq_fold_impl(f, d),
113-
|f, d| default_seq_fold_type(f, d)
120+
|f, d| default_seq_fold_type(f, d),
121+
|f, d| default_seq_fold_struct(f, d)
114122
)
115123
}
116124

@@ -127,7 +135,8 @@ fn default_par_fold<T:Send Copy>(ctxt: T) -> Fold<T> {
127135
|f, d| default_seq_fold_enum(f, d),
128136
|f, d| default_seq_fold_trait(f, d),
129137
|f, d| default_seq_fold_impl(f, d),
130-
|f, d| default_seq_fold_type(f, d)
138+
|f, d| default_seq_fold_type(f, d),
139+
|f, d| default_seq_fold_struct(f, d)
131140
)
132141
}
133142

@@ -267,6 +276,9 @@ fn fold_ItemTag<T>(fold: Fold<T>, doc: doc::ItemTag) -> doc::ItemTag {
267276
doc::TyTag(TyDoc) => {
268277
doc::TyTag(fold.fold_type(fold, TyDoc))
269278
}
279+
doc::StructTag(StructDoc) => {
280+
doc::StructTag(fold.fold_struct(fold, StructDoc))
281+
}
270282
}
271283
}
272284

@@ -330,6 +342,16 @@ fn default_seq_fold_type<T>(
330342
}
331343
}
332344

345+
fn default_seq_fold_struct<T>(
346+
fold: Fold<T>,
347+
doc: doc::StructDoc
348+
) -> doc::StructDoc {
349+
{
350+
item: fold.fold_item(fold, doc.item),
351+
.. doc
352+
}
353+
}
354+
333355
#[test]
334356
fn default_fold_should_produce_same_doc() {
335357
let source = ~"mod a { fn b() { } mod c { fn d() { } } }";

src/rustdoc/markdown_pass.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,9 @@ fn header_kind(doc: doc::ItemTag) -> ~str {
210210
doc::TyTag(_) => {
211211
~"Type"
212212
}
213+
doc::StructTag(_) => {
214+
~"Struct"
215+
}
213216
}
214217
}
215218

@@ -370,7 +373,8 @@ fn write_item_(ctxt: Ctxt, doc: doc::ItemTag, write_header: bool) {
370373
doc::EnumTag(EnumDoc) => write_enum(ctxt, EnumDoc),
371374
doc::TraitTag(TraitDoc) => write_trait(ctxt, TraitDoc),
372375
doc::ImplTag(ImplDoc) => write_impl(ctxt, ImplDoc),
373-
doc::TyTag(TyDoc) => write_type(ctxt, TyDoc)
376+
doc::TyTag(TyDoc) => write_type(ctxt, TyDoc),
377+
doc::StructTag(StructDoc) => write_struct(ctxt, StructDoc),
374378
}
375379
}
376380

@@ -775,6 +779,20 @@ fn should_write_type_signature() {
775779
assert str::contains(markdown, ~"\n\n type t = int\n\n");
776780
}
777781

782+
fn write_struct(
783+
ctxt: Ctxt,
784+
doc: doc::StructDoc
785+
) {
786+
write_sig(ctxt, doc.sig);
787+
write_common(ctxt, doc.desc(), doc.sections());
788+
}
789+
790+
#[test]
791+
fn should_write_struct_header() {
792+
let markdown = test::render(~"struct S { field: () }");
793+
assert str::contains(markdown, ~"## Struct `S`\n\n");
794+
}
795+
778796
#[cfg(test)]
779797
mod test {
780798
fn render(source: ~str) -> ~str {

src/rustdoc/sort_item_type_pass.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ fn mk_pass() -> Pass {
1111
doc::ConstTag(_) => 0,
1212
doc::TyTag(_) => 1,
1313
doc::EnumTag(_) => 2,
14-
doc::TraitTag(_) => 3,
15-
doc::ImplTag(_) => 4,
16-
doc::FnTag(_) => 5,
17-
doc::ModTag(_) => 6,
18-
doc::NmodTag(_) => 7
14+
doc::StructTag(_) => 3,
15+
doc::TraitTag(_) => 4,
16+
doc::ImplTag(_) => 5,
17+
doc::FnTag(_) => 6,
18+
doc::ModTag(_) => 7,
19+
doc::NmodTag(_) => 8
1920
}
2021
}
2122

@@ -35,17 +36,19 @@ fn test() {
3536
enum ienum { ivar } \
3637
trait itrait { fn a(); } \
3738
impl int { fn a() { } } \
38-
type itype = int;";
39+
type itype = int; \
40+
struct istruct { f: () }";
3941
do astsrv::from_str(source) |srv| {
4042
let doc = extract::from_srv(srv, ~"");
4143
let doc = mk_pass().f(srv, doc);
4244
assert doc.cratemod().items[0].name() == ~"iconst";
4345
assert doc.cratemod().items[1].name() == ~"itype";
4446
assert doc.cratemod().items[2].name() == ~"ienum";
45-
assert doc.cratemod().items[3].name() == ~"itrait";
46-
assert doc.cratemod().items[4].name() == ~"__extensions__";
47-
assert doc.cratemod().items[5].name() == ~"ifn";
48-
assert doc.cratemod().items[6].name() == ~"imod";
49-
assert doc.cratemod().items[7].name() == ~"inmod";
47+
assert doc.cratemod().items[3].name() == ~"istruct";
48+
assert doc.cratemod().items[4].name() == ~"itrait";
49+
assert doc.cratemod().items[5].name() == ~"__extensions__";
50+
assert doc.cratemod().items[6].name() == ~"ifn";
51+
assert doc.cratemod().items[7].name() == ~"imod";
52+
assert doc.cratemod().items[8].name() == ~"inmod";
5053
}
5154
}

src/rustdoc/tystr_pass.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ fn run(
2727
fold_trait: fold_trait,
2828
fold_impl: fold_impl,
2929
fold_type: fold_type,
30+
fold_struct: fold_struct,
3031
.. *fold::default_any_fold(srv)
3132
});
3233
fold.fold_doc(fold, doc)
@@ -322,6 +323,32 @@ fn should_add_type_signatures() {
322323
assert doc.cratemod().types()[0].sig == Some(~"type t<T> = int");
323324
}
324325

326+
fn fold_struct(
327+
fold: fold::Fold<astsrv::Srv>,
328+
doc: doc::StructDoc
329+
) -> doc::StructDoc {
330+
let srv = fold.ctxt;
331+
332+
{
333+
sig: do astsrv::exec(srv) |ctxt| {
334+
match ctxt.ast_map.get(doc.id()) {
335+
ast_map::node_item(item, _) => {
336+
Some(pprust::item_to_str(item,
337+
extract::interner()))
338+
}
339+
_ => fail ~"not an item"
340+
}
341+
},
342+
.. doc
343+
}
344+
}
345+
346+
#[test]
347+
fn should_add_struct_defs() {
348+
let doc = test::mk_doc(~"struct S { field: () }");
349+
assert doc.cratemod().structs()[0].sig.get().contains("struct S {");
350+
}
351+
325352
#[cfg(test)]
326353
mod test {
327354
fn mk_doc(source: ~str) -> doc::Doc {

0 commit comments

Comments
 (0)