]> git.lizzy.rs Git - rust.git/blob - src/librustc_save_analysis/json_api_dumper.rs
Remove intermediate forms and some other refactoring
[rust.git] / src / librustc_save_analysis / json_api_dumper.rs
1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use std::io::Write;
12
13 use rustc_serialize::json::as_json;
14
15 use Dump;
16
17 use rls_data::{Analysis, Import, Def, CratePreludeData, Format, Relation};
18
19
20 // A dumper to dump a restricted set of JSON information, designed for use with
21 // libraries distributed without their source. Clients are likely to use type
22 // information here, and (for example) generate Rustdoc URLs, but don't need
23 // information for navigating the source of the crate.
24 // Relative to the regular JSON save-analysis info, this form is filtered to
25 // remove non-visible items.
26 pub struct JsonApiDumper<'b, W: Write + 'b> {
27     output: &'b mut W,
28     result: Analysis,
29 }
30
31 impl<'b, W: Write> JsonApiDumper<'b, W> {
32     pub fn new(writer: &'b mut W) -> JsonApiDumper<'b, W> {
33         let mut result = Analysis::new();
34         result.kind = Format::JsonApi;
35         JsonApiDumper { output: writer, result }
36     }
37 }
38
39 impl<'b, W: Write> Drop for JsonApiDumper<'b, W> {
40     fn drop(&mut self) {
41         if let Err(_) = write!(self.output, "{}", as_json(&self.result)) {
42             error!("Error writing output");
43         }
44     }
45 }
46
47 impl<'b, W: Write + 'b> Dump for JsonApiDumper<'b, W> {
48     fn crate_prelude(&mut self, data: CratePreludeData) {
49         self.result.prelude = Some(data)
50     }
51
52     fn dump_relation(&mut self, data: Relation) {
53         self.result.relations.push(data);
54     }
55     fn import(&mut self, public: bool, import: Import) {
56         if public {
57             self.result.imports.push(import);
58         }
59     }
60     fn dump_def(&mut self, public: bool, mut data: Def) {
61         if public {
62             data.attributes = vec![];
63             self.result.defs.push(data);
64         }
65     }
66 }