about summary refs log tree commit diff
path: root/src/rustdoc/astsrv.rs
blob: 4bdf4498d637f1d162cbaadb7fbd575b69ed305b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#[doc(
    brief = "Provides all access to AST-related, non-sendable info",
    desc =
    "Rustdoc is intended to be parallel, and the rustc AST is filled \
     with shared boxes. The AST service attempts to provide a single \
     place to query AST-related information, shielding the rest of \
     Rustdoc from its non-sendableness."
)];

import rustc::syntax::ast;
import rustc::middle::ast_map;

export ctxt;
export ctxt_handler;
export srv;
export mk_srv_from_str;
export mk_srv_from_file;
export exec;

type ctxt = {
    ast: @ast::crate,
    map: ast_map::map
};

type ctxt_handler<T> = fn~(ctxt: ctxt) -> T;

type srv = {
    ctxt: ctxt
};

fn mk_srv_from_str(source: str) -> srv {
    {
        ctxt: build_ctxt(parse::from_str(source))
    }
}

fn mk_srv_from_file(file: str) -> srv {
    {
        ctxt: build_ctxt(parse::from_file(file))
    }
}

fn build_ctxt(ast: @ast::crate) -> ctxt {
    {
        ast: ast,
        map: ast_map::map_crate(*ast)
    }
}

fn exec<T>(
    srv: srv,
    f: fn~(ctxt: ctxt) -> T
) -> T {
    f(srv.ctxt)
}

#[cfg(test)]
mod tests {

    #[test]
    fn srv_should_build_ast_map() {
        let source = "fn a() { }";
        let srv = mk_srv_from_str(source);
        exec(srv) {|ctxt|
            assert ctxt.map.size() != 0u
        };
    }

    #[test]
    fn srv_should_return_request_result() {
        let source = "fn a() { }";
        let srv = mk_srv_from_str(source);
        let result = exec(srv) {|_ctxt| 1000};
        assert result == 1000;
    }
}