Skip to content

Commit a49dbdd

Browse files
committed
clang: move the module to output
1 parent c5b7dde commit a49dbdd

File tree

8 files changed

+73
-50
lines changed

8 files changed

+73
-50
lines changed

bear/src/semantic/clang/converter.rs renamed to bear/src/output/clang/converter.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
//! # Example
1717
//!
1818
//! ```rust
19-
//! use bear::semantic::clang::converter::CommandConverter;
19+
//! use bear::output::clang::converter::CommandConverter;
2020
//! use bear::config::Format;
2121
//!
2222
//! let config = Format::default();
@@ -26,8 +26,8 @@
2626
//! // into compilation database entries based on the configured format
2727
//! ```
2828
29-
use super::format::{ConfigurablePathFormatter, FormatConfigurationError, PathFormatter};
3029
use super::Entry;
30+
use super::{ConfigurablePathFormatter, FormatConfigurationError, PathFormatter};
3131
use crate::config;
3232
use crate::semantic::{ArgumentKind, Arguments, Command, CompilerCommand};
3333
use log::warn;
@@ -251,9 +251,11 @@ impl CommandConverter {
251251

252252
#[cfg(test)]
253253
mod tests {
254+
use super::super::super::clang::format::{FormatError, MockPathFormatter};
254255
use super::*;
255256
use crate::config::{EntryFormat, Format, PathFormat};
256257
use crate::semantic::{ArgumentKind, Command, CompilerCommand, CompilerPass};
258+
use std::io;
257259

258260
#[test]
259261
fn test_compiler_command_to_entries_single_source() {
@@ -448,8 +450,6 @@ mod tests {
448450

449451
#[test]
450452
fn test_path_formatting_with_custom_formatter() {
451-
use crate::semantic::clang::format::MockPathFormatter;
452-
453453
let mut mock_formatter = MockPathFormatter::new();
454454

455455
// Set up expectations for the mock
@@ -486,9 +486,6 @@ mod tests {
486486

487487
#[test]
488488
fn test_path_formatting_error_handling() {
489-
use crate::semantic::clang::format::{FormatError, MockPathFormatter};
490-
use std::io;
491-
492489
let mut mock_formatter = MockPathFormatter::new();
493490

494491
// Make format_directory fail
@@ -516,9 +513,6 @@ mod tests {
516513

517514
#[test]
518515
fn test_file_path_formatting_error_handling() {
519-
use crate::semantic::clang::format::{FormatError, MockPathFormatter};
520-
use std::io;
521-
522516
let mut mock_formatter = MockPathFormatter::new();
523517

524518
// Directory formatting succeeds
@@ -554,9 +548,6 @@ mod tests {
554548

555549
#[test]
556550
fn test_output_file_formatting_error_handling() {
557-
use crate::semantic::clang::format::{FormatError, MockPathFormatter};
558-
use std::io;
559-
560551
let mut mock_formatter = MockPathFormatter::new();
561552

562553
// Directory formatting succeeds
File renamed without changes.
File renamed without changes.

bear/src/semantic/clang/mod.rs renamed to bear/src/output/clang/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use thiserror::Error;
2323
// Re-export types for easier access
2424
pub use converter::CommandConverter;
2525
pub use filter::DuplicateEntryFilter;
26-
pub use format::{FormatConfigurationError, FormatError, PathFormatter};
26+
pub use format::{ConfigurablePathFormatter, FormatConfigurationError, FormatError, PathFormatter};
2727

2828
/// Represents an entry of the compilation database.
2929
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]

bear/src/output/formats.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
//! - The semantic database format (internal format of this project).
88
//! - The execution event database format (internal format of this project.)
99
10-
use super::json;
11-
use crate::{intercept, semantic::clang};
10+
use super::{clang, json};
11+
use crate::intercept;
1212
use serde_json::de::IoRead;
1313
use serde_json::StreamDeserializer;
1414
use thiserror::Error;
@@ -128,9 +128,9 @@ impl SerializationFormat<intercept::Event> for ExecutionEventDatabase {
128128
#[cfg(test)]
129129
mod test {
130130
mod compilation_database {
131+
use super::super::clang::{Entry, EntryError};
131132
use super::super::JsonCompilationDatabase as Sut;
132133
use super::super::{SerializationError, SerializationFormat};
133-
use crate::semantic::clang::{Entry, EntryError};
134134
use serde_json::error::Category;
135135
use serde_json::json;
136136
use std::io::{Cursor, Seek, SeekFrom};

bear/src/output/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
//! The `OutputWriter` struct represents the main entry point for writing output.
1010
//! The input to the `OutputWriter` is a stream of `semantic::Command` instances.
1111
12+
pub mod clang;
1213
mod formats;
1314
mod json;
1415
mod writers;

bear/src/output/writers.rs

Lines changed: 64 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// SPDX-License-Identifier: GPL-3.0-or-later
22

3+
use super::clang;
34
use super::formats::{JsonCompilationDatabase, SerializationError, SerializationFormat};
45
use super::{WriterCreationError, WriterError};
5-
use crate::semantic::clang::{DuplicateEntryFilter, Entry};
66
use crate::{config, semantic};
77
use std::{fs, io, path};
88

@@ -19,24 +19,26 @@ pub(super) trait IteratorWriter<T> {
1919
}
2020

2121
/// The type represents a converter that formats `semantic::Command` instances into `Entry` objects.
22-
pub(super) struct ConverterClangOutputWriter<T: IteratorWriter<Entry>> {
23-
converter: semantic::clang::CommandConverter,
22+
pub(super) struct ConverterClangOutputWriter<T: IteratorWriter<clang::Entry>> {
23+
converter: clang::CommandConverter,
2424
writer: T,
2525
}
2626

27-
impl<T: IteratorWriter<Entry>> ConverterClangOutputWriter<T> {
27+
impl<T: IteratorWriter<clang::Entry>> ConverterClangOutputWriter<T> {
2828
pub(super) fn new(
2929
writer: T,
3030
format: &config::Format,
31-
) -> Result<Self, semantic::clang::FormatConfigurationError> {
31+
) -> Result<Self, clang::FormatConfigurationError> {
3232
Ok(Self {
33-
converter: semantic::clang::CommandConverter::new(format.clone())?,
33+
converter: clang::CommandConverter::new(format.clone())?,
3434
writer,
3535
})
3636
}
3737
}
3838

39-
impl<T: IteratorWriter<Entry>> IteratorWriter<semantic::Command> for ConverterClangOutputWriter<T> {
39+
impl<T: IteratorWriter<clang::Entry>> IteratorWriter<semantic::Command>
40+
for ConverterClangOutputWriter<T>
41+
{
4042
fn write(self, semantics: impl Iterator<Item = semantic::Command>) -> Result<(), WriterError> {
4143
let entries = semantics.flat_map(|semantic| self.converter.to_entries(&semantic));
4244
self.writer.write(entries)
@@ -52,12 +54,12 @@ impl<T: IteratorWriter<Entry>> IteratorWriter<semantic::Command> for ConverterCl
5254
///
5355
/// # Note
5456
/// Reading errors will be ignored, and a warning will be logged.
55-
pub(super) struct AppendClangOutputWriter<T: IteratorWriter<Entry>> {
57+
pub(super) struct AppendClangOutputWriter<T: IteratorWriter<clang::Entry>> {
5658
writer: T,
5759
path: Option<path::PathBuf>,
5860
}
5961

60-
impl<T: IteratorWriter<Entry>> AppendClangOutputWriter<T> {
62+
impl<T: IteratorWriter<clang::Entry>> AppendClangOutputWriter<T> {
6163
pub(super) fn new(writer: T, input_path: &path::Path, append: bool) -> Self {
6264
let path = if input_path.exists() {
6365
Some(input_path.to_path_buf())
@@ -76,7 +78,7 @@ impl<T: IteratorWriter<Entry>> AppendClangOutputWriter<T> {
7678
/// because the logic is not bound to the instance.
7779
fn read_from_compilation_db(
7880
source: &path::Path,
79-
) -> Result<impl Iterator<Item = Entry>, SerializationError> {
81+
) -> Result<impl Iterator<Item = clang::Entry>, SerializationError> {
8082
let file = fs::File::open(source).map(io::BufReader::new)?;
8183

8284
let entries = JsonCompilationDatabase::read_and_ignore(file, |error| {
@@ -86,8 +88,8 @@ impl<T: IteratorWriter<Entry>> AppendClangOutputWriter<T> {
8688
}
8789
}
8890

89-
impl<T: IteratorWriter<Entry>> IteratorWriter<Entry> for AppendClangOutputWriter<T> {
90-
fn write(self, entries: impl Iterator<Item = Entry>) -> Result<(), WriterError> {
91+
impl<T: IteratorWriter<clang::Entry>> IteratorWriter<clang::Entry> for AppendClangOutputWriter<T> {
92+
fn write(self, entries: impl Iterator<Item = clang::Entry>) -> Result<(), WriterError> {
9193
if let Some(path) = &self.path {
9294
let entries_from_db = Self::read_from_compilation_db(path)
9395
.map_err(|err| WriterError::Io(path.clone(), err))?;
@@ -103,13 +105,13 @@ impl<T: IteratorWriter<Entry>> IteratorWriter<Entry> for AppendClangOutputWriter
103105
///
104106
/// The file is first written to a temporary file and then renamed to the final file name.
105107
/// This ensures that the output file is not left in an inconsistent state in case of errors.
106-
pub(super) struct AtomicClangOutputWriter<T: IteratorWriter<Entry>> {
108+
pub(super) struct AtomicClangOutputWriter<T: IteratorWriter<clang::Entry>> {
107109
writer: T,
108110
temp_path: path::PathBuf,
109111
final_path: path::PathBuf,
110112
}
111113

112-
impl<T: IteratorWriter<Entry>> AtomicClangOutputWriter<T> {
114+
impl<T: IteratorWriter<clang::Entry>> AtomicClangOutputWriter<T> {
113115
pub(super) fn new(writer: T, temp_path: &path::Path, final_path: &path::Path) -> Self {
114116
Self {
115117
writer,
@@ -119,8 +121,8 @@ impl<T: IteratorWriter<Entry>> AtomicClangOutputWriter<T> {
119121
}
120122
}
121123

122-
impl<T: IteratorWriter<Entry>> IteratorWriter<Entry> for AtomicClangOutputWriter<T> {
123-
fn write(self, entries: impl Iterator<Item = Entry>) -> Result<(), WriterError> {
124+
impl<T: IteratorWriter<clang::Entry>> IteratorWriter<clang::Entry> for AtomicClangOutputWriter<T> {
125+
fn write(self, entries: impl Iterator<Item = clang::Entry>) -> Result<(), WriterError> {
124126
self.writer.write(entries)?;
125127

126128
fs::rename(&self.temp_path, &self.final_path)
@@ -134,25 +136,25 @@ impl<T: IteratorWriter<Entry>> IteratorWriter<Entry> for AtomicClangOutputWriter
134136
///
135137
/// # Features
136138
/// - Filters duplicates based on the provided configuration.
137-
pub(super) struct UniqueOutputWriter<T: IteratorWriter<Entry>> {
139+
pub(super) struct UniqueOutputWriter<T: IteratorWriter<clang::Entry>> {
138140
writer: T,
139-
filter: DuplicateEntryFilter,
141+
filter: clang::DuplicateEntryFilter,
140142
}
141143

142-
impl<T: IteratorWriter<Entry>> UniqueOutputWriter<T> {
144+
impl<T: IteratorWriter<clang::Entry>> UniqueOutputWriter<T> {
143145
pub(super) fn create(
144146
writer: T,
145147
config: &config::DuplicateFilter,
146148
) -> Result<Self, WriterCreationError> {
147-
let filter = DuplicateEntryFilter::try_from(config.clone())
149+
let filter = clang::DuplicateEntryFilter::try_from(config.clone())
148150
.map_err(|err| WriterCreationError::Configuration(err.to_string()))?;
149151

150152
Ok(Self { writer, filter })
151153
}
152154
}
153155

154-
impl<T: IteratorWriter<Entry>> IteratorWriter<Entry> for UniqueOutputWriter<T> {
155-
fn write(self, entries: impl Iterator<Item = Entry>) -> Result<(), WriterError> {
156+
impl<T: IteratorWriter<clang::Entry>> IteratorWriter<clang::Entry> for UniqueOutputWriter<T> {
157+
fn write(self, entries: impl Iterator<Item = clang::Entry>) -> Result<(), WriterError> {
156158
let mut filter = self.filter.clone();
157159
let filtered_entries = entries.filter(move |entry| filter.unique(entry));
158160

@@ -183,8 +185,8 @@ impl ClangOutputWriter {
183185
}
184186
}
185187

186-
impl IteratorWriter<Entry> for ClangOutputWriter {
187-
fn write(self, entries: impl Iterator<Item = Entry>) -> Result<(), WriterError> {
188+
impl IteratorWriter<clang::Entry> for ClangOutputWriter {
189+
fn write(self, entries: impl Iterator<Item = clang::Entry>) -> Result<(), WriterError> {
188190
JsonCompilationDatabase::write(self.output, entries)
189191
.map_err(|err| WriterError::Io(self.path, err))
190192
}
@@ -198,8 +200,8 @@ mod tests {
198200

199201
struct MockWriter;
200202

201-
impl IteratorWriter<Entry> for MockWriter {
202-
fn write(self, _: impl Iterator<Item = Entry>) -> Result<(), WriterError> {
203+
impl IteratorWriter<clang::Entry> for MockWriter {
204+
fn write(self, _: impl Iterator<Item = clang::Entry>) -> Result<(), WriterError> {
203205
Ok(())
204206
}
205207
}
@@ -261,8 +263,18 @@ mod tests {
261263
let result_path = dir.path().join("result_file.json");
262264

263265
let entries_to_write = vec![
264-
Entry::from_arguments_str("file1.cpp", vec!["clang", "-c"], "/path/to/dir", None),
265-
Entry::from_arguments_str("file2.cpp", vec!["clang", "-c"], "/path/to/dir", None),
266+
clang::Entry::from_arguments_str(
267+
"file1.cpp",
268+
vec!["clang", "-c"],
269+
"/path/to/dir",
270+
None,
271+
),
272+
clang::Entry::from_arguments_str(
273+
"file2.cpp",
274+
vec!["clang", "-c"],
275+
"/path/to/dir",
276+
None,
277+
),
266278
];
267279

268280
let writer = ClangOutputWriter::create(&result_path).unwrap();
@@ -284,15 +296,35 @@ mod tests {
284296

285297
// Create the original file with some entries
286298
let original_entries = vec![
287-
Entry::from_arguments_str("file3.cpp", vec!["clang", "-c"], "/path/to/dir", None),
288-
Entry::from_arguments_str("file4.cpp", vec!["clang", "-c"], "/path/to/dir", None),
299+
clang::Entry::from_arguments_str(
300+
"file3.cpp",
301+
vec!["clang", "-c"],
302+
"/path/to/dir",
303+
None,
304+
),
305+
clang::Entry::from_arguments_str(
306+
"file4.cpp",
307+
vec!["clang", "-c"],
308+
"/path/to/dir",
309+
None,
310+
),
289311
];
290312
let writer = ClangOutputWriter::create(&input_path).unwrap();
291313
writer.write(original_entries.into_iter()).unwrap();
292314

293315
let new_entries = vec![
294-
Entry::from_arguments_str("file1.cpp", vec!["clang", "-c"], "/path/to/dir", None),
295-
Entry::from_arguments_str("file2.cpp", vec!["clang", "-c"], "/path/to/dir", None),
316+
clang::Entry::from_arguments_str(
317+
"file1.cpp",
318+
vec!["clang", "-c"],
319+
"/path/to/dir",
320+
None,
321+
),
322+
clang::Entry::from_arguments_str(
323+
"file2.cpp",
324+
vec!["clang", "-c"],
325+
"/path/to/dir",
326+
None,
327+
),
296328
];
297329

298330
let writer = ClangOutputWriter::create(&result_path).unwrap();

bear/src/semantic/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
//! - [`Arguments`] - Trait for representing different types of compiler arguments
2727
//! - [`ArgumentKind`] - Classifies the semantic meaning of arguments
2828
29-
pub mod clang;
3029
pub mod interpreters;
3130

3231
#[cfg(test)]

0 commit comments

Comments
 (0)