Skip to content

Commit ab1adc8

Browse files
committed
rust: file format read-and-ignore takes reporter method
1 parent 782c188 commit ab1adc8

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

rust/bear/src/modes/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ impl Mode for Intercept {
6262
/// executed commands from the build process.
6363
pub struct Semantic {
6464
event_file: BufReader<fs::File>,
65-
event_file_name: path::PathBuf,
6665
semantic: SemanticAnalysisPipeline,
6766
}
6867

@@ -81,7 +80,6 @@ impl Semantic {
8180

8281
Ok(Self {
8382
event_file,
84-
event_file_name,
8583
semantic,
8684
})
8785
}
@@ -95,7 +93,9 @@ impl Mode for Semantic {
9593
self.semantic
9694
.analyze_and_write(ExecutionEventDatabase::read_and_ignore(
9795
self.event_file,
98-
self.event_file_name,
96+
|error| {
97+
log::warn!("Event file reading issue: {:?}", error);
98+
},
9999
))
100100
.map(|_| ExitCode::SUCCESS)
101101
}

rust/bear/src/output/clang/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,13 @@ impl<T: IteratorWriter<Entry>> AppendClangOutputWriter<T> {
106106
fn read_from_compilation_db(
107107
source: &path::Path,
108108
) -> anyhow::Result<impl Iterator<Item = Entry>> {
109-
let source_copy = source.to_path_buf();
110-
111109
let file = fs::File::open(source)
112110
.map(io::BufReader::new)
113111
.with_context(|| format!("Failed to open file: {:?}", source))?;
114112

115-
let entries = JsonCompilationDatabase::read_and_ignore(file, source_copy);
113+
let entries = JsonCompilationDatabase::read_and_ignore(file, |error| {
114+
log::warn!("Problems to read previous entries: {:?}", error);
115+
});
116116
Ok(entries)
117117
}
118118
}

rust/bear/src/output/formats.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use super::{clang, json};
1111
use crate::{intercept, semantic};
1212
use serde_json::de::IoRead;
1313
use serde_json::StreamDeserializer;
14-
use std::{io, path};
14+
use std::io;
1515
use thiserror::Error;
1616

1717
/// The trait represents a file format that can be written to and read from.
@@ -25,11 +25,14 @@ pub trait FileFormat<T> {
2525

2626
/// Reads the entries from the file and ignores any errors.
2727
/// This is not always feasible, when the file format is strict.
28-
fn read_and_ignore(reader: impl io::Read, source: path::PathBuf) -> impl Iterator<Item = T> {
28+
fn read_and_ignore(
29+
reader: impl io::Read,
30+
message_writer: impl Fn(&str),
31+
) -> impl Iterator<Item = T> {
2932
Self::read(reader).filter_map(move |result| match result {
3033
Ok(value) => Some(value),
3134
Err(error) => {
32-
log::warn!("Failed to read entry: {:?} from {:?}", error, source);
35+
message_writer(&error.to_string());
3336
None
3437
}
3538
})
@@ -400,7 +403,6 @@ mod test {
400403
use serde_json::json;
401404
use std::collections::HashMap;
402405
use std::io::{Cursor, Seek, SeekFrom};
403-
use std::path::PathBuf;
404406

405407
#[test]
406408
fn read_write() {
@@ -453,13 +455,17 @@ mod test {
453455
}
454456
});
455457
let content = format!("{}\n{}\n{}\n", line1, line2, line3);
456-
let source = PathBuf::from("/home/user/project.json");
457458

458459
let mut cursor = Cursor::new(content);
459-
let read_events: Vec<_> = Sut::read_and_ignore(&mut cursor, source).collect();
460+
let warnings = std::cell::RefCell::new(Vec::new());
461+
let read_events: Vec<_> = Sut::read_and_ignore(&mut cursor, |error| {
462+
warnings.borrow_mut().push(format!("Warning: {:?}", error));
463+
})
464+
.collect();
460465

461-
// Only the fist event is read, all other lines are ignored.
466+
// Only the first event is read, all other lines are ignored.
462467
assert_eq!(expected_values()[0..1], read_events);
468+
assert_eq!(warnings.borrow().len(), 1);
463469
}
464470

465471
fn expected_values() -> Vec<Event> {

0 commit comments

Comments
 (0)