bacon/export/
exports_settings.rs

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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
use {
    crate::*,
    std::{
        collections::HashMap,
        path::PathBuf,
    },
};

/// Settings for all exports
#[derive(Debug, Clone, Default)]
pub struct ExportsSettings {
    pub exports: HashMap<String, ExportSettings>,
}

impl ExportsSettings {
    pub fn set_locations_export_auto(
        &mut self,
        enabled: bool,
    ) {
        let locations_export = self
            .exports
            .entry("locations".to_string())
            .or_insert_with(default_locations_export_settings);
        locations_export.auto = enabled;
    }

    pub fn do_auto_exports(
        &self,
        state: &AppState<'_>,
    ) {
        info!("DOING AUTO EXPORTS");
        for (name, export) in &self.exports {
            if export.auto {
                info!("doing auto export {:?}", name);
                if let Err(e) = export.do_export(state) {
                    error!("error while exporting {:?}: {:?}", name, e);
                }
            }
        }
    }

    pub fn do_named_export(
        &self,
        requested_name: &str,
        state: &AppState<'_>,
    ) {
        if let Some(export) = self.exports.get(requested_name) {
            info!("DOING EXPORT {:?}", requested_name);
            if let Err(e) = export.do_export(state) {
                error!("error while exporting {:?}: {:?}", requested_name, e);
            }
        } else {
            warn!("no export named {:?}", requested_name);
        }
    }

    /// We apply different parts of the config, matching
    /// different generations of the config format:
    ///
    /// - the exports map (current)
    /// - the export object (recently deprecated since 2.22.0)
    /// - the export_locations field (deprecated since 2.9.0)
    ///
    /// FIXME Should we prevent having two exporters with the
    /// same path ?
    pub fn apply_config(
        &mut self,
        config: &Config,
    ) {
        // normal [exports] map
        for (name, ec) in &config.exports {
            if let Some(e) = self.exports.get_mut(name) {
                if let Some(exporter) = ec.exporter {
                    e.exporter = exporter;
                }
                if let Some(b) = ec.auto {
                    e.auto = b;
                }
                if let Some(p) = &ec.path {
                    e.path = p.clone();
                }
                if let Some(lf) = &ec.line_format {
                    e.line_format = lf.clone();
                }
                continue;
            }
            let exporter = match ec.exporter {
                Some(e) => e,
                None => match name.as_str() {
                    "analysis" => Exporter::Analysis,
                    "json-report" => Exporter::JsonReport,
                    "locations" => Exporter::Locations,
                    _ => {
                        warn!(
                            "Exporter not specified for export {:?}, using 'locations'",
                            name
                        );
                        Exporter::Locations
                    }
                },
            };
            let auto = ec.auto.unwrap_or(true);
            let path = ec.path.clone().unwrap_or_else(|| match exporter {
                Exporter::Analysis => default_analysis_path(),
                Exporter::Locations => default_locations_path(),
                Exporter::JsonReport => default_json_report_path(),
            });
            let line_format = ec.line_format.clone().unwrap_or_else(|| match exporter {
                Exporter::Locations => default_locations_line_format().to_string(),
                _ => "".to_string(),
            });
            self.exports.insert(name.clone(), ExportSettings {
                exporter,
                auto,
                path,
                line_format,
            });
        }

        // [export] object
        #[allow(deprecated)] // for compatibility
        if let Some(ec) = &config.export {
            match ec.exporter {
                Some(Exporter::Analysis) => {
                    let analysis_export = self
                        .exports
                        .entry("analysis".to_string())
                        .or_insert_with(default_analysis_export_settings);
                    if let Some(b) = ec.auto {
                        analysis_export.auto = b;
                    }
                    if let Some(p) = &ec.path {
                        analysis_export.path = p.clone();
                    }
                }
                Some(Exporter::JsonReport) => {
                    let json_report_export = self
                        .exports
                        .entry("json-report".to_string())
                        .or_insert_with(default_json_report_export_settings);
                    if let Some(b) = ec.auto {
                        json_report_export.auto = b;
                    }
                    if let Some(p) = &ec.path {
                        json_report_export.path = p.clone();
                    }
                }
                _ => {
                    let locations_export = self
                        .exports
                        .entry("locations".to_string())
                        .or_insert_with(default_locations_export_settings);
                    if let Some(b) = ec.auto {
                        locations_export.auto = b;
                    }
                    if let Some(p) = &ec.path {
                        locations_export.path = p.clone();
                    }
                    if let Some(lf) = &ec.line_format {
                        locations_export.line_format = lf.clone();
                    }
                }
            }
        }

        #[allow(deprecated)] // for compatibility
        if let Some(b) = config.export_locations {
            let locations_export = self
                .exports
                .entry("locations".to_string())
                .or_insert_with(default_locations_export_settings);
            locations_export.auto = b;
        }
    }
}

fn default_analysis_export_settings() -> ExportSettings {
    ExportSettings {
        exporter: Exporter::Analysis,
        auto: true,
        path: default_analysis_path(),
        line_format: "".to_string(), // not used
    }
}
fn default_json_report_export_settings() -> ExportSettings {
    ExportSettings {
        exporter: Exporter::JsonReport,
        auto: true,
        path: default_json_report_path(),
        line_format: "".to_string(), // not used
    }
}
fn default_locations_export_settings() -> ExportSettings {
    ExportSettings {
        exporter: Exporter::Locations,
        auto: true,
        path: default_locations_path(),
        line_format: default_locations_line_format().to_string(),
    }
}

pub fn default_locations_line_format() -> &'static str {
    "{kind} {path}:{line}:{column} {message}"
}

pub fn default_analysis_path() -> PathBuf {
    PathBuf::from("bacon-analysis.json")
}
pub fn default_json_report_path() -> PathBuf {
    PathBuf::from("bacon-report.json")
}
pub fn default_locations_path() -> PathBuf {
    PathBuf::from(".bacon-locations")
}