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