2012-12-03 16:48:01 -08:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
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
|
|
|
|
2013-01-08 19:37:25 -08:00
|
|
|
use core::prelude::*;
|
|
|
|
|
2012-12-23 17:41:37 -05:00
|
|
|
use astsrv;
|
2012-09-18 16:48:40 -07:00
|
|
|
use doc::ItemUtils;
|
2012-12-23 17:41:37 -05:00
|
|
|
use doc;
|
2012-12-05 15:06:54 -08:00
|
|
|
use fold::Fold;
|
2012-12-23 17:41:37 -05:00
|
|
|
use fold;
|
2013-01-08 19:37:25 -08:00
|
|
|
use pass::Pass;
|
2012-11-26 18:03:36 -08:00
|
|
|
use util::NominalOp;
|
2012-07-11 15:00:40 -07:00
|
|
|
|
2012-12-23 17:41:37 -05:00
|
|
|
use std::par;
|
2013-01-30 18:52:31 -08:00
|
|
|
use std::cell::Cell;
|
2012-12-23 17:41:37 -05:00
|
|
|
|
2013-01-30 19:32:36 -08:00
|
|
|
pub fn mk_pass(name: ~str, op: fn~(&str) -> ~str) -> Pass {
|
2013-01-30 18:52:31 -08:00
|
|
|
let op = Cell(op);
|
2013-01-08 14:00:45 -08:00
|
|
|
Pass {
|
2013-01-30 13:14:35 -08:00
|
|
|
name: copy name,
|
2013-01-30 19:32:36 -08:00
|
|
|
f: fn~(move op, srv: astsrv::Srv, doc: doc::Doc) -> doc::Doc {
|
2013-01-30 18:52:31 -08:00
|
|
|
run(srv, doc, op.take())
|
2012-02-27 18:07:16 -08:00
|
|
|
}
|
2012-01-24 21:16:01 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-30 18:52:31 -08: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(
|
2012-09-18 16:48:40 -07:00
|
|
|
_srv: astsrv::Srv,
|
2013-01-30 19:32:36 -08:00
|
|
|
doc: doc::Doc,
|
|
|
|
op: Op
|
2012-09-18 16:48:40 -07:00
|
|
|
) -> doc::Doc {
|
2012-11-26 18:03:36 -08:00
|
|
|
let op = NominalOp {
|
|
|
|
op: move op
|
|
|
|
};
|
2012-12-05 15:06:54 -08:00
|
|
|
let 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-09-04 13:29:32 -07:00
|
|
|
fold_impl: fold_impl,
|
2012-12-05 15:06:54 -08:00
|
|
|
.. fold::default_any_fold(move op)
|
|
|
|
};
|
2012-11-29 17:51:16 -08:00
|
|
|
(fold.fold_doc)(&fold, doc)
|
2012-01-24 21:16:01 -08:00
|
|
|
}
|
|
|
|
|
2013-01-30 19:32:36 -08:00
|
|
|
fn maybe_apply_op(op: NominalOp<Op>, s: &Option<~str>) -> Option<~str> {
|
2013-01-30 18:52:31 -08:00
|
|
|
s.map(|s| (op.op)(*s) )
|
2012-01-24 21:16:01 -08:00
|
|
|
}
|
|
|
|
|
2012-11-26 18:03:36 -08:00
|
|
|
fn fold_item(
|
|
|
|
fold: &fold::Fold<NominalOp<Op>>,
|
2013-01-30 19:32:36 -08:00
|
|
|
doc: doc::ItemDoc
|
2012-11-26 18:03:36 -08:00
|
|
|
) -> doc::ItemDoc {
|
2012-02-17 15:59:57 -08:00
|
|
|
let doc = fold::default_seq_fold_item(fold, doc);
|
2012-01-24 21:16:01 -08:00
|
|
|
|
2013-01-25 16:57:39 -08:00
|
|
|
doc::ItemDoc {
|
2013-01-30 18:52:31 -08:00
|
|
|
brief: maybe_apply_op(copy fold.ctxt, &doc.brief),
|
|
|
|
desc: maybe_apply_op(copy fold.ctxt, &doc.desc),
|
|
|
|
sections: apply_to_sections(copy fold.ctxt, copy doc.sections),
|
2012-09-04 13:29:32 -07:00
|
|
|
.. doc
|
2012-01-24 21:16:01 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-26 18:03:36 -08:00
|
|
|
fn apply_to_sections(
|
2013-01-10 10:59:58 -08:00
|
|
|
+op: NominalOp<Op>,
|
2013-01-30 19:32:36 -08:00
|
|
|
sections: ~[doc::Section]
|
2012-11-26 18:03:36 -08:00
|
|
|
) -> ~[doc::Section] {
|
2013-01-25 16:57:39 -08:00
|
|
|
par::map(sections, |section, copy op| doc::Section {
|
2013-01-30 13:14:35 -08:00
|
|
|
header: (op.op)(copy section.header),
|
|
|
|
body: (op.op)(copy section.body)
|
2012-06-26 13:55:56 -07:00
|
|
|
})
|
2012-03-09 11:47:31 -08:00
|
|
|
}
|
|
|
|
|
2012-11-26 18:03:36 -08:00
|
|
|
fn fold_enum(
|
|
|
|
fold: &fold::Fold<NominalOp<Op>>,
|
2013-01-30 19:32:36 -08:00
|
|
|
doc: doc::EnumDoc) -> doc::EnumDoc {
|
2012-02-17 15:59:57 -08:00
|
|
|
let doc = fold::default_seq_fold_enum(fold, doc);
|
2012-11-19 18:48:46 -08:00
|
|
|
let fold_copy = copy *fold;
|
2012-02-17 15:59:57 -08:00
|
|
|
|
2013-01-25 16:57:39 -08:00
|
|
|
doc::EnumDoc {
|
2012-11-19 18:48:46 -08:00
|
|
|
variants: do par::map(doc.variants) |variant, copy fold_copy| {
|
2013-01-25 16:57:39 -08:00
|
|
|
doc::VariantDoc {
|
2013-01-30 18:52:31 -08:00
|
|
|
desc: maybe_apply_op(copy fold_copy.ctxt, &variant.desc),
|
2013-01-30 13:14:35 -08:00
|
|
|
.. copy *variant
|
2012-01-25 18:51:46 -08:00
|
|
|
}
|
2012-09-04 13:29:32 -07:00
|
|
|
},
|
|
|
|
.. doc
|
2012-01-25 18:51:46 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-26 18:03:36 -08:00
|
|
|
fn fold_trait(
|
|
|
|
fold: &fold::Fold<NominalOp<Op>>,
|
2013-01-30 19:32:36 -08:00
|
|
|
doc: doc::TraitDoc
|
2012-11-26 18:03:36 -08:00
|
|
|
) -> doc::TraitDoc {
|
2012-07-03 16:30:42 -07:00
|
|
|
let doc = fold::default_seq_fold_trait(fold, doc);
|
2012-02-17 15:59:57 -08:00
|
|
|
|
2013-01-25 16:57:39 -08:00
|
|
|
doc::TraitDoc {
|
2013-01-30 18:52:31 -08:00
|
|
|
methods: apply_to_methods(copy fold.ctxt, copy doc.methods),
|
2012-09-04 13:29:32 -07:00
|
|
|
.. doc
|
2012-01-31 20:42:06 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-26 18:03:36 -08:00
|
|
|
fn apply_to_methods(
|
2013-01-30 19:32:36 -08:00
|
|
|
op: NominalOp<Op>,
|
|
|
|
docs: ~[doc::MethodDoc]
|
2012-11-26 18:03:36 -08:00
|
|
|
) -> ~[doc::MethodDoc] {
|
2013-01-10 10:59:58 -08:00
|
|
|
let op = copy op;
|
|
|
|
do par::map(docs) |doc| {
|
2013-01-25 16:57:39 -08:00
|
|
|
doc::MethodDoc {
|
2013-01-30 18:52:31 -08:00
|
|
|
brief: maybe_apply_op(copy op, &doc.brief),
|
|
|
|
desc: maybe_apply_op(copy op, &doc.desc),
|
|
|
|
sections: apply_to_sections(copy op, copy doc.sections),
|
2013-01-30 13:14:35 -08:00
|
|
|
.. copy *doc
|
2012-01-30 19:36:58 -08:00
|
|
|
}
|
2012-01-31 20:42:06 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-26 18:03:36 -08:00
|
|
|
fn fold_impl(
|
|
|
|
fold: &fold::Fold<NominalOp<Op>>,
|
2013-01-30 19:32:36 -08:00
|
|
|
doc: doc::ImplDoc
|
2012-11-26 18:03:36 -08:00
|
|
|
) -> 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
|
|
|
|
2013-01-25 16:57:39 -08:00
|
|
|
doc::ImplDoc {
|
2013-01-30 18:52:31 -08:00
|
|
|
methods: apply_to_methods(copy fold.ctxt, copy doc.methods),
|
2012-09-04 13:29:32 -07:00
|
|
|
.. doc
|
2012-02-01 22:41:41 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 }");
|
2012-08-20 12:23:37 -07:00
|
|
|
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 }");
|
2012-08-20 12:23:37 -07:00
|
|
|
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 }");
|
2012-08-20 12:23:37 -07:00
|
|
|
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-08-20 12:23:37 -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-08-20 12:23:37 -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-08-20 12:23:37 -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-08-20 12:23:37 -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-08-08 17:19:06 -07:00
|
|
|
~"#[doc = \" a \"] impl int { fn a() { } }");
|
2012-08-20 12:23:37 -07:00
|
|
|
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-08-08 17:19:06 -07:00
|
|
|
~"#[doc = \" a \"] impl int { fn a() { } }");
|
2012-08-20 12:23:37 -07:00
|
|
|
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-08-08 17:19:06 -07:00
|
|
|
~"impl int { #[doc = \" a \"] fn a() { } }");
|
2012-08-20 12:23:37 -07:00
|
|
|
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-08-08 17:19:06 -07:00
|
|
|
~"impl int { #[doc = \" a \"] fn a() { } }");
|
2012-08-20 12:23:37 -07:00
|
|
|
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;");
|
2012-08-20 12:23:37 -07:00
|
|
|
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;");
|
2012-08-20 12:23:37 -07:00
|
|
|
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-08-08 17:19:06 -07:00
|
|
|
~"impl 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-08-08 17:19:06 -07:00
|
|
|
~"impl 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-12-29 17:38:20 -08:00
|
|
|
use astsrv;
|
|
|
|
use attr_pass;
|
|
|
|
use desc_to_brief_pass;
|
|
|
|
use doc;
|
|
|
|
use extract;
|
|
|
|
use sectionalize_pass;
|
2013-01-08 19:37:25 -08:00
|
|
|
use text_pass::mk_pass;
|
2012-12-29 17:38:20 -08:00
|
|
|
|
|
|
|
use core::str;
|
|
|
|
|
2013-01-30 19:32:36 -08:00
|
|
|
pub fn mk_doc(source: ~str) -> doc::Doc {
|
2013-01-30 13:14:35 -08:00
|
|
|
do astsrv::from_str(copy source) |srv| {
|
2012-07-13 22:57:48 -07:00
|
|
|
let doc = extract::from_srv(srv, ~"");
|
2012-12-03 17:08:52 -08:00
|
|
|
let doc = (attr_pass::mk_pass().f)(srv, doc);
|
|
|
|
let doc = (desc_to_brief_pass::mk_pass().f)(srv, doc);
|
|
|
|
let doc = (sectionalize_pass::mk_pass().f)(srv, doc);
|
|
|
|
(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
|
|
|
}
|