Skip to content

Commit bc49e6f

Browse files
committed
compilation database writer formats entries
1 parent ea518a9 commit bc49e6f

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

bear/src/output/writers.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,11 @@ impl<T: IteratorWriter<Entry>> IteratorWriter<Entry> for UniqueOutputWriter<T> {
195195
///
196196
/// # Features
197197
/// - Writes the entries to a file.
198+
/// - Formats the entries to the configured shape.
198199
pub(super) struct ClangOutputWriter {
199200
output: io::BufWriter<fs::File>,
201+
command_field_as_array: bool,
202+
keep_output_field: bool,
200203
}
201204

202205
impl ClangOutputWriter {
@@ -205,16 +208,33 @@ impl ClangOutputWriter {
205208
.map(io::BufWriter::new)
206209
.with_context(|| format!("Failed to open file: {:?}", file_name))?;
207210

208-
Ok(Self { output })
211+
Ok(Self {
212+
output,
213+
command_field_as_array: true,
214+
keep_output_field: true,
215+
})
209216
}
210217
}
211218

212219
impl IteratorWriter<Entry> for ClangOutputWriter {
213220
fn write(self, entries: impl Iterator<Item = Entry>) -> anyhow::Result<()> {
221+
let entries = entries.map(|entry| {
222+
let mut entry = if self.command_field_as_array {
223+
// It's safe to assume that the entry is valid, so we can unwrap.
224+
entry.as_arguments().unwrap()
225+
} else {
226+
entry.as_command()
227+
};
228+
if !self.keep_output_field {
229+
entry.output = None;
230+
}
231+
entry
232+
});
214233
JsonCompilationDatabase::write(self.output, entries)?;
215234
Ok(())
216235
}
217236
}
237+
218238
#[cfg(test)]
219239
mod tests {
220240
use super::*;

0 commit comments

Comments
 (0)