rust/src/librustdoc/pass.rs

98 lines
2.5 KiB
Rust
Raw Normal View History

// 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.
use core::prelude::*;
use astsrv;
use doc;
use extract;
use time;
use core::vec;
2012-09-21 19:10:46 -05:00
/// A single operation on the document model
pub struct Pass {
2012-09-21 19:10:46 -05:00
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,
2013-01-30 21:32:36 -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;
2013-01-30 15:14:35 -06:00
do time(copy pass.name) {
(pass.f)(srv, copy doc)
2012-09-21 19:10:46 -05:00
}
}
}
#[test]
fn test_run_passes() {
fn pass1(
_srv: astsrv::Srv,
2013-01-30 21:32:36 -06:00
doc: doc::Doc
2012-09-21 19:10:46 -05:00
) -> doc::Doc {
doc::Doc{
2012-09-21 19:10:46 -05:00
pages: ~[
doc::CratePage(doc::CrateDoc{
topmod: doc::ModDoc{
item: doc::ItemDoc {
2012-09-21 19:10:46 -05:00
name: doc.cratemod().name() + ~"two",
2013-01-30 15:14:35 -06:00
.. copy doc.cratemod().item
2012-09-21 19:10:46 -05:00
},
items: ~[],
index: None
}
2012-09-21 19:10:46 -05:00
})
]
}
2012-09-21 19:10:46 -05:00
}
fn pass2(
_srv: astsrv::Srv,
2013-01-30 21:32:36 -06:00
doc: doc::Doc
2012-09-21 19:10:46 -05:00
) -> doc::Doc {
doc::Doc{
2012-09-21 19:10:46 -05:00
pages: ~[
doc::CratePage(doc::CrateDoc{
topmod: doc::ModDoc{
item: doc::ItemDoc {
2012-09-21 19:10:46 -05:00
name: doc.cratemod().name() + ~"three",
2013-01-30 15:14:35 -06:00
.. copy doc.cratemod().item
2012-09-21 19:10:46 -05:00
},
items: ~[],
index: None
}
2012-09-21 19:10:46 -05:00
})
]
}
2012-09-21 19:10:46 -05:00
}
let source = ~"";
do astsrv::from_str(source) |srv| {
let passes = ~[
Pass {
2012-09-21 19:10:46 -05:00
name: ~"",
f: pass1
},
Pass {
2012-09-21 19:10:46 -05:00
name: ~"",
f: pass2
}
];
let doc = extract::from_srv(srv, ~"one");
let doc = run_passes(srv, doc, passes);
assert doc.cratemod().name() == ~"onetwothree";
}
}