rust/src/librustdoc/pass.rs

97 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 astsrv;
use doc;
use time;
#[cfg(test)] use extract;
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,
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 passes.iter().fold(doc) |doc, pass| {
2013-03-08 14:39:42 -06:00
debug!("pass #%d", passno);
2012-09-21 19:10:46 -05:00
passno += 1;
2013-07-02 14:47:32 -05:00
do time(pass.name.clone()) {
(pass.f)(srv.clone(), doc.clone())
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 {
name: doc.cratemod().name_() + "two",
2013-07-02 14:47:32 -05:00
.. doc.cratemod().item.clone()
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 {
name: doc.cratemod().name_() + "three",
2013-07-02 14:47:32 -05:00
.. doc.cratemod().item.clone()
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
}
];
2013-01-31 22:24:03 -06:00
let doc = extract::from_srv(srv.clone(), ~"one");
2012-09-21 19:10:46 -05:00
let doc = run_passes(srv, doc, passes);
assert_eq!(doc.cratemod().name_(), ~"onetwothree");
2012-09-21 19:10:46 -05:00
}
}