2012-07-04 21:53:12 +00:00
|
|
|
//! Generic pass for performing an operation on all descriptions
|
2012-01-25 05:16:01 +00:00
|
|
|
|
2012-09-05 17:41:47 +00:00
|
|
|
use doc::item_utils;
|
2012-07-11 22:00:40 +00:00
|
|
|
|
2012-01-25 05:16:01 +00:00
|
|
|
export mk_pass;
|
|
|
|
|
2012-07-14 05:57:48 +00:00
|
|
|
fn mk_pass(name: ~str, +op: fn~(~str) -> ~str) -> pass {
|
2012-02-28 02:07:16 +00:00
|
|
|
{
|
2012-02-28 02:11:12 +00:00
|
|
|
name: name,
|
2012-03-03 02:33:25 +00:00
|
|
|
f: fn~(srv: astsrv::srv, doc: doc::doc) -> doc::doc {
|
2012-02-28 02:07:16 +00:00
|
|
|
run(srv, doc, op)
|
|
|
|
}
|
2012-01-25 05:16:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-14 05:57:48 +00:00
|
|
|
type op = fn~(~str) -> ~str;
|
2012-01-25 05:16:01 +00:00
|
|
|
|
2012-08-01 20:35:33 +00:00
|
|
|
#[allow(non_implicitly_copyable_typarams)]
|
2012-01-25 05:16:01 +00:00
|
|
|
fn run(
|
|
|
|
_srv: astsrv::srv,
|
2012-03-03 02:33:25 +00:00
|
|
|
doc: doc::doc,
|
2012-01-25 05:16:01 +00:00
|
|
|
op: op
|
2012-03-03 02:33:25 +00:00
|
|
|
) -> doc::doc {
|
2012-01-25 05:16:01 +00:00
|
|
|
let fold = fold::fold({
|
2012-02-17 23:59:57 +00:00
|
|
|
fold_item: fold_item,
|
2012-01-27 06:27:13 +00:00
|
|
|
fold_enum: fold_enum,
|
2012-07-03 23:30:42 +00:00
|
|
|
fold_trait: fold_trait,
|
2012-09-04 20:29:32 +00:00
|
|
|
fold_impl: fold_impl,
|
|
|
|
.. *fold::default_any_fold(op)
|
2012-01-25 05:16:01 +00:00
|
|
|
});
|
2012-03-03 02:33:25 +00:00
|
|
|
fold.fold_doc(fold, doc)
|
2012-01-25 05:16:01 +00:00
|
|
|
}
|
|
|
|
|
2012-08-20 19:23:37 +00:00
|
|
|
fn maybe_apply_op(op: op, s: Option<~str>) -> Option<~str> {
|
2012-06-30 23:19:07 +00:00
|
|
|
option::map(s, |s| op(s) )
|
2012-01-25 05:16:01 +00:00
|
|
|
}
|
|
|
|
|
2012-02-17 23:59:57 +00:00
|
|
|
fn fold_item(fold: fold::fold<op>, doc: doc::itemdoc) -> doc::itemdoc {
|
|
|
|
let doc = fold::default_seq_fold_item(fold, doc);
|
2012-01-25 05:16:01 +00:00
|
|
|
|
2012-01-30 21:05:25 +00:00
|
|
|
{
|
2012-02-17 23:59:57 +00:00
|
|
|
brief: maybe_apply_op(fold.ctxt, doc.brief),
|
2012-03-09 19:47:31 +00:00
|
|
|
desc: maybe_apply_op(fold.ctxt, doc.desc),
|
2012-09-04 20:29:32 +00:00
|
|
|
sections: apply_to_sections(fold.ctxt, doc.sections),
|
|
|
|
.. doc
|
2012-01-25 05:16:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-29 23:26:56 +00:00
|
|
|
fn apply_to_sections(op: op, sections: ~[doc::section]) -> ~[doc::section] {
|
2012-07-13 17:36:35 +00:00
|
|
|
par::map(sections, |section, copy op| {
|
2012-06-30 23:19:07 +00:00
|
|
|
header: op(section.header),
|
|
|
|
body: op(section.body)
|
2012-06-26 20:55:56 +00:00
|
|
|
})
|
2012-03-09 19:47:31 +00:00
|
|
|
}
|
|
|
|
|
2012-01-26 02:51:46 +00:00
|
|
|
fn fold_enum(fold: fold::fold<op>, doc: doc::enumdoc) -> doc::enumdoc {
|
2012-02-17 23:59:57 +00:00
|
|
|
let doc = fold::default_seq_fold_enum(fold, doc);
|
|
|
|
|
2012-01-30 21:05:25 +00:00
|
|
|
{
|
2012-07-13 17:36:35 +00:00
|
|
|
variants: do par::map(doc.variants) |variant, copy fold| {
|
2012-01-30 21:05:25 +00:00
|
|
|
{
|
2012-09-04 20:29:32 +00:00
|
|
|
desc: maybe_apply_op(fold.ctxt, variant.desc),
|
|
|
|
.. variant
|
2012-01-26 02:51:46 +00:00
|
|
|
}
|
2012-09-04 20:29:32 +00:00
|
|
|
},
|
|
|
|
.. doc
|
2012-01-26 02:51:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-03 23:30:42 +00:00
|
|
|
fn fold_trait(fold: fold::fold<op>, doc: doc::traitdoc) -> doc::traitdoc {
|
|
|
|
let doc = fold::default_seq_fold_trait(fold, doc);
|
2012-02-17 23:59:57 +00:00
|
|
|
|
2012-01-31 03:36:58 +00:00
|
|
|
{
|
2012-09-04 20:29:32 +00:00
|
|
|
methods: apply_to_methods(fold.ctxt, doc.methods),
|
|
|
|
.. doc
|
2012-02-01 04:42:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-29 23:26:56 +00:00
|
|
|
fn apply_to_methods(op: op, docs: ~[doc::methoddoc]) -> ~[doc::methoddoc] {
|
2012-07-13 17:36:35 +00:00
|
|
|
do par::map(docs) |doc, copy op| {
|
2012-02-01 04:42:06 +00:00
|
|
|
{
|
|
|
|
brief: maybe_apply_op(op, doc.brief),
|
|
|
|
desc: maybe_apply_op(op, doc.desc),
|
2012-09-04 20:29:32 +00:00
|
|
|
sections: apply_to_sections(op, doc.sections),
|
|
|
|
.. doc
|
2012-01-31 03:36:58 +00:00
|
|
|
}
|
2012-02-01 04:42:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_impl(fold: fold::fold<op>, doc: doc::impldoc) -> doc::impldoc {
|
2012-02-17 23:59:57 +00:00
|
|
|
let doc = fold::default_seq_fold_impl(fold, doc);
|
2012-01-31 03:36:58 +00:00
|
|
|
|
2012-02-02 06:41:41 +00:00
|
|
|
{
|
2012-09-04 20:29:32 +00:00
|
|
|
methods: apply_to_methods(fold.ctxt, doc.methods),
|
|
|
|
.. doc
|
2012-02-02 06:41:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-26 02:51:46 +00:00
|
|
|
#[test]
|
|
|
|
fn should_execute_op_on_enum_brief() {
|
2012-07-14 05:57:48 +00:00
|
|
|
let doc = test::mk_doc(~"#[doc = \" a \"] enum a { b }");
|
2012-08-20 19:23:37 +00:00
|
|
|
assert doc.cratemod().enums()[0].brief() == Some(~"a");
|
2012-01-26 02:51:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_execute_op_on_enum_desc() {
|
2012-07-14 05:57:48 +00:00
|
|
|
let doc = test::mk_doc(~"#[doc = \" a \"] enum a { b }");
|
2012-08-20 19:23:37 +00:00
|
|
|
assert doc.cratemod().enums()[0].desc() == Some(~"a");
|
2012-01-26 02:51:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_execute_op_on_variant_desc() {
|
2012-07-14 05:57:48 +00:00
|
|
|
let doc = test::mk_doc(~"enum a { #[doc = \" a \"] b }");
|
2012-08-20 19:23:37 +00:00
|
|
|
assert doc.cratemod().enums()[0].variants[0].desc == Some(~"a");
|
2012-01-27 06:27:13 +00:00
|
|
|
}
|
|
|
|
|
2012-01-31 03:36:58 +00:00
|
|
|
#[test]
|
2012-07-03 23:30:42 +00:00
|
|
|
fn should_execute_op_on_trait_brief() {
|
2012-02-01 02:32:37 +00:00
|
|
|
let doc = test::mk_doc(
|
2012-07-31 17:27:51 +00:00
|
|
|
~"#[doc = \" a \"] trait i { fn a(); }");
|
2012-08-20 19:23:37 +00:00
|
|
|
assert doc.cratemod().traits()[0].brief() == Some(~"a");
|
2012-01-31 03:36:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-07-03 23:30:42 +00:00
|
|
|
fn should_execute_op_on_trait_desc() {
|
2012-02-01 02:32:37 +00:00
|
|
|
let doc = test::mk_doc(
|
2012-07-31 17:27:51 +00:00
|
|
|
~"#[doc = \" a \"] trait i { fn a(); }");
|
2012-08-20 19:23:37 +00:00
|
|
|
assert doc.cratemod().traits()[0].desc() == Some(~"a");
|
2012-01-31 03:36:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-07-03 23:30:42 +00:00
|
|
|
fn should_execute_op_on_trait_method_brief() {
|
2012-02-01 02:32:37 +00:00
|
|
|
let doc = test::mk_doc(
|
2012-07-31 17:27:51 +00:00
|
|
|
~"trait i { #[doc = \" a \"] fn a(); }");
|
2012-08-20 19:23:37 +00:00
|
|
|
assert doc.cratemod().traits()[0].methods[0].brief == Some(~"a");
|
2012-01-31 03:36:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-07-03 23:30:42 +00:00
|
|
|
fn should_execute_op_on_trait_method_desc() {
|
2012-02-01 02:32:37 +00:00
|
|
|
let doc = test::mk_doc(
|
2012-07-31 17:27:51 +00:00
|
|
|
~"trait i { #[doc = \" a \"] fn a(); }");
|
2012-08-20 19:23:37 +00:00
|
|
|
assert doc.cratemod().traits()[0].methods[0].desc == Some(~"a");
|
2012-01-31 03:36:58 +00:00
|
|
|
}
|
|
|
|
|
2012-02-01 04:42:06 +00:00
|
|
|
#[test]
|
|
|
|
fn should_execute_op_on_impl_brief() {
|
|
|
|
let doc = test::mk_doc(
|
2012-08-09 00:19:06 +00:00
|
|
|
~"#[doc = \" a \"] impl int { fn a() { } }");
|
2012-08-20 19:23:37 +00:00
|
|
|
assert doc.cratemod().impls()[0].brief() == Some(~"a");
|
2012-02-01 04:42:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_execute_op_on_impl_desc() {
|
|
|
|
let doc = test::mk_doc(
|
2012-08-09 00:19:06 +00:00
|
|
|
~"#[doc = \" a \"] impl int { fn a() { } }");
|
2012-08-20 19:23:37 +00:00
|
|
|
assert doc.cratemod().impls()[0].desc() == Some(~"a");
|
2012-02-01 04:42:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_execute_op_on_impl_method_brief() {
|
|
|
|
let doc = test::mk_doc(
|
2012-08-09 00:19:06 +00:00
|
|
|
~"impl int { #[doc = \" a \"] fn a() { } }");
|
2012-08-20 19:23:37 +00:00
|
|
|
assert doc.cratemod().impls()[0].methods[0].brief == Some(~"a");
|
2012-02-01 04:42:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_execute_op_on_impl_method_desc() {
|
|
|
|
let doc = test::mk_doc(
|
2012-08-09 00:19:06 +00:00
|
|
|
~"impl int { #[doc = \" a \"] fn a() { } }");
|
2012-08-20 19:23:37 +00:00
|
|
|
assert doc.cratemod().impls()[0].methods[0].desc == Some(~"a");
|
2012-02-01 04:42:06 +00:00
|
|
|
}
|
|
|
|
|
2012-02-02 06:41:41 +00:00
|
|
|
#[test]
|
|
|
|
fn should_execute_op_on_type_brief() {
|
|
|
|
let doc = test::mk_doc(
|
2012-07-14 05:57:48 +00:00
|
|
|
~"#[doc = \" a \"] type t = int;");
|
2012-08-20 19:23:37 +00:00
|
|
|
assert doc.cratemod().types()[0].brief() == Some(~"a");
|
2012-02-02 06:41:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_execute_op_on_type_desc() {
|
|
|
|
let doc = test::mk_doc(
|
2012-07-14 05:57:48 +00:00
|
|
|
~"#[doc = \" a \"] type t = int;");
|
2012-08-20 19:23:37 +00:00
|
|
|
assert doc.cratemod().types()[0].desc() == Some(~"a");
|
2012-02-02 06:41:41 +00:00
|
|
|
}
|
|
|
|
|
2012-03-09 19:47:31 +00:00
|
|
|
#[test]
|
|
|
|
fn should_execute_on_item_section_headers() {
|
|
|
|
let doc = test::mk_doc(
|
2012-07-14 05:57:48 +00:00
|
|
|
~"#[doc = \"\
|
2012-03-09 19:47:31 +00:00
|
|
|
# Header \n\
|
|
|
|
Body\"]\
|
|
|
|
fn a() { }");
|
2012-07-14 05:57:48 +00:00
|
|
|
assert doc.cratemod().fns()[0].sections()[0].header == ~"Header";
|
2012-03-09 19:47:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_execute_on_item_section_bodies() {
|
|
|
|
let doc = test::mk_doc(
|
2012-07-14 05:57:48 +00:00
|
|
|
~"#[doc = \"\
|
2012-03-09 19:47:31 +00:00
|
|
|
# Header\n\
|
|
|
|
Body \"]\
|
|
|
|
fn a() { }");
|
2012-07-14 05:57:48 +00:00
|
|
|
assert doc.cratemod().fns()[0].sections()[0].body == ~"Body";
|
2012-03-09 19:47:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-07-03 23:30:42 +00:00
|
|
|
fn should_execute_on_trait_method_section_headers() {
|
2012-03-09 19:47:31 +00:00
|
|
|
let doc = test::mk_doc(
|
2012-07-31 17:27:51 +00:00
|
|
|
~"trait i {
|
2012-03-09 19:47:31 +00:00
|
|
|
#[doc = \"\
|
|
|
|
# Header \n\
|
|
|
|
Body\"]\
|
|
|
|
fn a(); }");
|
2012-07-03 23:30:42 +00:00
|
|
|
assert doc.cratemod().traits()[0].methods[0].sections[0].header
|
2012-07-14 05:57:48 +00:00
|
|
|
== ~"Header";
|
2012-03-09 19:47:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-07-03 23:30:42 +00:00
|
|
|
fn should_execute_on_trait_method_section_bodies() {
|
2012-03-09 19:47:31 +00:00
|
|
|
let doc = test::mk_doc(
|
2012-07-31 17:27:51 +00:00
|
|
|
~"trait i {
|
2012-03-09 19:47:31 +00:00
|
|
|
#[doc = \"\
|
|
|
|
# Header\n\
|
|
|
|
Body \"]\
|
|
|
|
fn a(); }");
|
2012-07-14 05:57:48 +00:00
|
|
|
assert doc.cratemod().traits()[0].methods[0].sections[0].body == ~"Body";
|
2012-03-09 19:47:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_execute_on_impl_method_section_headers() {
|
|
|
|
let doc = test::mk_doc(
|
2012-08-09 00:19:06 +00:00
|
|
|
~"impl bool {
|
2012-03-09 19:47:31 +00:00
|
|
|
#[doc = \"\
|
|
|
|
# Header \n\
|
|
|
|
Body\"]\
|
|
|
|
fn a() { } }");
|
|
|
|
assert doc.cratemod().impls()[0].methods[0].sections[0].header
|
2012-07-14 05:57:48 +00:00
|
|
|
== ~"Header";
|
2012-03-09 19:47:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_execute_on_impl_method_section_bodies() {
|
|
|
|
let doc = test::mk_doc(
|
2012-08-09 00:19:06 +00:00
|
|
|
~"impl bool {
|
2012-03-09 19:47:31 +00:00
|
|
|
#[doc = \"\
|
|
|
|
# Header\n\
|
|
|
|
Body \"]\
|
|
|
|
fn a() { } }");
|
2012-07-14 05:57:48 +00:00
|
|
|
assert doc.cratemod().impls()[0].methods[0].sections[0].body == ~"Body";
|
2012-03-09 19:47:31 +00:00
|
|
|
}
|
|
|
|
|
2012-02-01 02:32:37 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2012-07-14 05:57:48 +00:00
|
|
|
fn mk_doc(source: ~str) -> doc::doc {
|
2012-06-30 23:19:07 +00:00
|
|
|
do astsrv::from_str(source) |srv| {
|
2012-07-14 05:57:48 +00:00
|
|
|
let doc = extract::from_srv(srv, ~"");
|
2012-02-28 02:07:16 +00:00
|
|
|
let doc = attr_pass::mk_pass().f(srv, doc);
|
2012-03-10 02:12:15 +00:00
|
|
|
let doc = desc_to_brief_pass::mk_pass().f(srv, doc);
|
2012-03-09 19:47:31 +00:00
|
|
|
let doc = sectionalize_pass::mk_pass().f(srv, doc);
|
2012-07-14 05:57:48 +00:00
|
|
|
mk_pass(~"", |s| str::trim(s) ).f(srv, doc)
|
2012-02-21 05:08:19 +00:00
|
|
|
}
|
2012-02-01 02:32:37 +00:00
|
|
|
}
|
2012-05-24 21:49:39 +00:00
|
|
|
}
|