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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use std::collections::BTreeMap;
use std::env;
use std::fs::{remove_file, create_dir_all, File};
use std::io::Write;
use std::path::PathBuf;
use std::error::Error;
use rustc_serialize::json::as_json;
use syntax_pos::{Span, FileName};
use crate::ext::base::ExtCtxt;
use crate::diagnostics::plugin::{ErrorMap, ErrorInfo};
#[derive(PartialEq, RustcDecodable, RustcEncodable)]
pub struct ErrorMetadata {
    pub description: Option<String>,
    pub use_site: Option<ErrorLocation>
}
pub type ErrorMetadataMap = BTreeMap<String, ErrorMetadata>;
#[derive(PartialEq, RustcDecodable, RustcEncodable)]
pub struct ErrorLocation {
    pub filename: FileName,
    pub line: usize
}
impl ErrorLocation {
    
    pub fn from_span(ecx: &ExtCtxt<'_>, sp: Span) -> ErrorLocation {
        let loc = ecx.source_map().lookup_char_pos(sp.lo());
        ErrorLocation {
            filename: loc.file.name.clone(),
            line: loc.line
        }
    }
}
pub fn get_metadata_dir(prefix: &str) -> PathBuf {
    env::var_os("RUSTC_ERROR_METADATA_DST")
        .map(PathBuf::from)
        .expect("env var `RUSTC_ERROR_METADATA_DST` isn't set")
        .join(prefix)
}
fn get_metadata_path(directory: PathBuf, name: &str) -> PathBuf {
    directory.join(format!("{}.json", name))
}
pub fn output_metadata(ecx: &ExtCtxt<'_>, prefix: &str, name: &str, err_map: &ErrorMap)
    -> Result<(), Box<dyn Error>>
{
    
    let metadata_dir = get_metadata_dir(prefix);
    create_dir_all(&metadata_dir)?;
    
    let metadata_path = get_metadata_path(metadata_dir, name);
    let mut metadata_file = File::create(&metadata_path)?;
    
    let json_map = err_map.iter().map(|(k, &ErrorInfo { description, use_site })| {
        let key = k.as_str().to_string();
        let value = ErrorMetadata {
            description: description.map(|n| n.as_str().to_string()),
            use_site: use_site.map(|sp| ErrorLocation::from_span(ecx, sp))
        };
        (key, value)
    }).collect::<ErrorMetadataMap>();
    
    let result = write!(&mut metadata_file, "{}", as_json(&json_map));
    if result.is_err() {
        remove_file(&metadata_path)?;
    }
    Ok(result?)
}