2016-04-25 03:54:00 -05:00
|
|
|
// Copyright 2016 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 std::io::Write;
|
|
|
|
|
|
|
|
use rustc_serialize::json::as_json;
|
|
|
|
|
2016-05-03 05:54:29 -05:00
|
|
|
use super::external_data::*;
|
2016-04-25 03:54:00 -05:00
|
|
|
use super::dump::Dump;
|
|
|
|
|
2016-05-03 05:54:29 -05:00
|
|
|
pub struct JsonDumper<'b, W: Write + 'b> {
|
2016-04-25 03:54:00 -05:00
|
|
|
output: &'b mut W,
|
2016-04-25 17:14:44 -05:00
|
|
|
first: bool,
|
2016-04-25 03:54:00 -05:00
|
|
|
}
|
|
|
|
|
2016-05-03 05:54:29 -05:00
|
|
|
impl<'b, W: Write> JsonDumper<'b, W> {
|
|
|
|
pub fn new(writer: &'b mut W) -> JsonDumper<'b, W> {
|
2016-04-25 17:14:44 -05:00
|
|
|
if let Err(_) = write!(writer, "[") {
|
|
|
|
error!("Error writing output");
|
2016-04-26 15:51:00 -05:00
|
|
|
}
|
2016-05-03 05:54:29 -05:00
|
|
|
JsonDumper { output: writer, first: true }
|
2016-04-25 17:14:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 05:54:29 -05:00
|
|
|
impl<'b, W: Write> Drop for JsonDumper<'b, W> {
|
2016-04-25 17:14:44 -05:00
|
|
|
fn drop(&mut self) {
|
|
|
|
if let Err(_) = write!(self.output, "]") {
|
|
|
|
error!("Error writing output");
|
|
|
|
}
|
2016-04-25 03:54:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! impl_fn {
|
|
|
|
($fn_name: ident, $data_type: ident) => {
|
2016-05-03 05:54:29 -05:00
|
|
|
fn $fn_name(&mut self, data: $data_type) {
|
2016-04-25 17:14:44 -05:00
|
|
|
if self.first {
|
|
|
|
self.first = false;
|
|
|
|
} else {
|
|
|
|
if let Err(_) = write!(self.output, ",") {
|
|
|
|
error!("Error writing output");
|
|
|
|
}
|
|
|
|
}
|
2016-04-25 03:54:00 -05:00
|
|
|
if let Err(_) = write!(self.output, "{}", as_json(&data)) {
|
|
|
|
error!("Error writing output '{}'", as_json(&data));
|
|
|
|
}
|
2016-04-25 05:53:01 -05:00
|
|
|
}
|
2016-04-25 03:54:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 05:54:29 -05:00
|
|
|
impl<'b, W: Write + 'b> Dump for JsonDumper<'b, W> {
|
2016-04-25 03:54:00 -05:00
|
|
|
impl_fn!(crate_prelude, CratePreludeData);
|
|
|
|
impl_fn!(enum_data, EnumData);
|
|
|
|
impl_fn!(extern_crate, ExternCrateData);
|
|
|
|
impl_fn!(impl_data, ImplData);
|
|
|
|
impl_fn!(inheritance, InheritanceData);
|
|
|
|
impl_fn!(function, FunctionData);
|
|
|
|
impl_fn!(function_ref, FunctionRefData);
|
|
|
|
impl_fn!(function_call, FunctionCallData);
|
|
|
|
impl_fn!(method, MethodData);
|
|
|
|
impl_fn!(method_call, MethodCallData);
|
|
|
|
impl_fn!(macro_data, MacroData);
|
|
|
|
impl_fn!(macro_use, MacroUseData);
|
|
|
|
impl_fn!(mod_data, ModData);
|
|
|
|
impl_fn!(mod_ref, ModRefData);
|
|
|
|
impl_fn!(struct_data, StructData);
|
|
|
|
impl_fn!(struct_variant, StructVariantData);
|
|
|
|
impl_fn!(trait_data, TraitData);
|
|
|
|
impl_fn!(tuple_variant, TupleVariantData);
|
|
|
|
impl_fn!(type_ref, TypeRefData);
|
|
|
|
impl_fn!(typedef, TypedefData);
|
|
|
|
impl_fn!(use_data, UseData);
|
|
|
|
impl_fn!(use_glob, UseGlobData);
|
|
|
|
impl_fn!(variable, VariableData);
|
|
|
|
impl_fn!(variable_ref, VariableRefData);
|
|
|
|
}
|