2012-12-03 18:48:01 -06: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.
|
|
|
|
|
2013-01-08 21:37:25 -06:00
|
|
|
use core::prelude::*;
|
|
|
|
|
2012-12-23 16:41:37 -06:00
|
|
|
use astsrv;
|
|
|
|
use doc;
|
2012-12-29 19:38:20 -06:00
|
|
|
use extract;
|
2013-01-08 21:37:25 -06:00
|
|
|
use time;
|
2012-12-23 16:41:37 -06:00
|
|
|
|
|
|
|
use core::vec;
|
|
|
|
|
2012-09-21 19:10:46 -05:00
|
|
|
/// A single operation on the document model
|
|
|
|
pub type Pass = {
|
|
|
|
name: ~str,
|
2012-11-19 20:48:46 -06:00
|
|
|
f: fn~(srv: astsrv::Srv, +doc: doc::Doc) -> doc::Doc
|
2012-09-21 19:10:46 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
pub fn run_passes(
|
|
|
|
srv: astsrv::Srv,
|
2012-11-19 20:48:46 -06:00
|
|
|
+doc: doc::Doc,
|
|
|
|
+passes: ~[Pass]
|
2012-09-21 19:10:46 -05:00
|
|
|
) -> doc::Doc {
|
|
|
|
let mut passno = 0;
|
|
|
|
do vec::foldl(doc, passes) |doc, pass| {
|
|
|
|
log(debug, fmt!("pass #%d", passno));
|
|
|
|
passno += 1;
|
|
|
|
log(debug, doc);
|
|
|
|
do time(pass.name) {
|
2012-11-29 19:51:16 -06:00
|
|
|
(pass.f)(srv, doc)
|
2012-09-21 19:10:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_run_passes() {
|
|
|
|
fn pass1(
|
|
|
|
_srv: astsrv::Srv,
|
2012-11-19 20:48:46 -06:00
|
|
|
+doc: doc::Doc
|
2012-09-21 19:10:46 -05:00
|
|
|
) -> doc::Doc {
|
|
|
|
doc::Doc_({
|
|
|
|
pages: ~[
|
|
|
|
doc::CratePage({
|
|
|
|
topmod: doc::ModDoc_({
|
|
|
|
item: {
|
|
|
|
name: doc.cratemod().name() + ~"two",
|
|
|
|
.. doc.cratemod().item
|
|
|
|
},
|
|
|
|
items: ~[],
|
|
|
|
index: None
|
|
|
|
})
|
|
|
|
})
|
|
|
|
]
|
|
|
|
})
|
|
|
|
}
|
|
|
|
fn pass2(
|
|
|
|
_srv: astsrv::Srv,
|
2012-11-19 20:48:46 -06:00
|
|
|
+doc: doc::Doc
|
2012-09-21 19:10:46 -05:00
|
|
|
) -> doc::Doc {
|
|
|
|
doc::Doc_({
|
|
|
|
pages: ~[
|
|
|
|
doc::CratePage({
|
|
|
|
topmod: doc::ModDoc_({
|
|
|
|
item: {
|
|
|
|
name: doc.cratemod().name() + ~"three",
|
|
|
|
.. doc.cratemod().item
|
|
|
|
},
|
|
|
|
items: ~[],
|
|
|
|
index: None
|
|
|
|
})
|
|
|
|
})
|
|
|
|
]
|
|
|
|
})
|
|
|
|
}
|
|
|
|
let source = ~"";
|
|
|
|
do astsrv::from_str(source) |srv| {
|
|
|
|
let passes = ~[
|
|
|
|
{
|
|
|
|
name: ~"",
|
|
|
|
f: pass1
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: ~"",
|
|
|
|
f: pass2
|
|
|
|
}
|
|
|
|
];
|
|
|
|
let doc = extract::from_srv(srv, ~"one");
|
|
|
|
let doc = run_passes(srv, doc, passes);
|
|
|
|
assert doc.cratemod().name() == ~"onetwothree";
|
|
|
|
}
|
|
|
|
}
|