Skip to content

Commit 754362b

Browse files
committed
output module documentation reviewed
1 parent f274394 commit 754362b

File tree

4 files changed

+60
-45
lines changed

4 files changed

+60
-45
lines changed

bear/src/output/formats.rs

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,46 @@
11
// SPDX-License-Identifier: GPL-3.0-or-later
22

3-
//! This module is responsible for declaring the file formats this project
4-
//! is using. The following formats are declared:
3+
//! This module declares the file formats used by this project.
4+
//! The following formats are declared:
55
//!
6-
//! - The JSON compilation database format. Declared by the Clang project.
7-
//! - The semantic database format. (Internal format of this project.)
8-
//! - The execution event database format. (Internal format of this project.)
6+
//! - The JSON compilation database format, as declared by the Clang project.
7+
//! - The semantic database format (internal format of this project).
8+
//! - The execution event database format (internal format of this project.)
99
1010
use super::json;
1111
use crate::{intercept, semantic, semantic::clang};
1212
use serde_json::de::IoRead;
1313
use serde_json::StreamDeserializer;
1414
use thiserror::Error;
1515

16-
/// The trait represents a file format that can be written to and read from.
16+
/// Represents errors that can occur while working with file formats.
17+
#[derive(Debug, Error)]
18+
pub enum SerializationError {
19+
#[error("Generic IO error: {0}")]
20+
Io(#[from] std::io::Error),
21+
#[error("Format syntax error: {0}")]
22+
Syntax(#[from] serde_json::Error),
23+
#[error("Format semantic error: {0}")]
24+
Semantic(#[from] clang::EntryError),
25+
}
26+
27+
/// A trait representing a file format that can be written to and read from.
1728
///
18-
/// The file format in this project is usually a sequence of values. This trait
19-
/// provides a type-independent abstraction over the file format.
29+
/// File formats in this project are usually sequences of values. This trait
30+
/// provides a type-independent abstraction over file formats.
2031
pub trait SerializationFormat<T> {
21-
fn write(_: impl std::io::Write, _: impl Iterator<Item = T>) -> Result<(), SerializationError>;
32+
/// Writes an iterator of items to the specified writer.
33+
fn write(
34+
writer: impl std::io::Write,
35+
items: impl Iterator<Item = T>,
36+
) -> Result<(), SerializationError>;
2237

23-
fn read(_: impl std::io::Read) -> impl Iterator<Item = Result<T, SerializationError>>;
38+
/// Reads items from the specified reader, returning an iterator of results.
39+
fn read(reader: impl std::io::Read) -> impl Iterator<Item = Result<T, SerializationError>>;
2440

25-
/// Reads the entries from the file and ignores any errors.
26-
/// This is not always feasible, when the file format is strict.
41+
/// Reads entries from the file and ignores any errors.
42+
///
43+
/// This is not always feasible when the file format is strict.
2744
fn read_and_ignore(
2845
reader: impl std::io::Read,
2946
message_writer: impl Fn(&str),
@@ -38,17 +55,6 @@ pub trait SerializationFormat<T> {
3855
}
3956
}
4057

41-
/// Represents errors that can occur while working with file formats.
42-
#[derive(Debug, Error)]
43-
pub enum SerializationError {
44-
#[error("Generic IO error: {0}")]
45-
Io(#[from] std::io::Error),
46-
#[error("Format syntax error: {0}")]
47-
Syntax(#[from] serde_json::Error),
48-
#[error("Format semantic error: {0}")]
49-
Semantic(#[from] clang::EntryError),
50-
}
51-
5258
/// The type represents a JSON compilation database format.
5359
///
5460
/// The format is a JSON array format, which is a sequence of JSON objects
@@ -102,9 +108,9 @@ pub struct JsonSemanticDatabase;
102108
impl SerializationFormat<semantic::Command> for JsonSemanticDatabase {
103109
fn write(
104110
writer: impl std::io::Write,
105-
entries: impl Iterator<Item = semantic::Command>,
111+
commands: impl Iterator<Item = semantic::Command>,
106112
) -> Result<(), SerializationError> {
107-
json::serialize_seq(writer, entries).map_err(SerializationError::Syntax)
113+
json::serialize_seq(writer, commands).map_err(SerializationError::Syntax)
108114
}
109115
fn read(
110116
_: impl std::io::Read,

bear/src/output/json.rs

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

3-
//! The module contains functions to serialize and deserialize JSON arrays.
3+
//! This module contains functions to serialize and deserialize JSON arrays.
44
//!
55
//! The main objective is to provide a way to serialize and deserialize
66
//! entries from an iterator into a JSON array and vice versa. Iterators
7-
//! allows us to process large datasets without loading everything into
7+
//! allow us to process large datasets without loading everything into
88
//! memory at once.
99
//!
10-
//! The format these methods are producing is a JSON array of objects.
10+
//! The format these methods produce is a JSON array of objects.
1111
//! It's *not* JSON lines format, which is a sequence of JSON objects
1212
//! separated by newlines.
1313

bear/src/output/mod.rs

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

3-
//! This module is responsible for writing the output of the semantic analysis.
3+
//! This module is responsible for writing the output of semantic analysis.
44
//!
55
//! The output can be in different formats, such as JSON compilation databases
66
//! or semantic analysis results in JSON format. The module provides functionality
77
//! to write these outputs to files, handle duplicates, and format the output
88
//! as needed.
99
//!
10-
//! The `OutputWriter` enum represents the main entry point for writing the output.
11-
//! The input of the `OutputWriter` is a stream of `semantic::CompilerCall` instances.
10+
//! The `OutputWriter` enum represents the main entry point for writing output.
11+
//! The input to the `OutputWriter` is a stream of `semantic::Command` instances.
1212
1313
mod formats;
1414
mod json;
@@ -24,6 +24,11 @@ use writers::{
2424
// Re-export types for convenience.
2525
pub use formats::{ExecutionEventDatabase, SerializationError, SerializationFormat};
2626

27+
/// A stack of output writers for Clang compilation databases.
28+
type ClangWriterStack = ConverterClangOutputWriter<
29+
AppendClangOutputWriter<AtomicClangOutputWriter<UniqueOutputWriter<ClangOutputWriter>>>,
30+
>;
31+
2732
/// Represents the output writer, which can handle different types of outputs.
2833
///
2934
/// This enum provides two variants:
@@ -33,11 +38,7 @@ pub use formats::{ExecutionEventDatabase, SerializationError, SerializationForma
3338
/// The variants are selected at runtime based on the configuration provided.
3439
pub enum OutputWriter {
3540
#[allow(private_interfaces)]
36-
Clang(
37-
ConverterClangOutputWriter<
38-
AppendClangOutputWriter<AtomicClangOutputWriter<UniqueOutputWriter<ClangOutputWriter>>>,
39-
>,
40-
),
41+
Clang(ClangWriterStack),
4142
#[allow(private_interfaces)]
4243
Semantic(SemanticOutputWriter),
4344
}
@@ -75,6 +76,13 @@ impl TryFrom<(&args::BuildSemantic, &config::Output)> for OutputWriter {
7576
}
7677

7778
impl OutputWriter {
79+
/// Writes semantic commands using the configured output writer.
80+
///
81+
/// # Arguments
82+
/// * `semantics` - An iterator of semantic commands to write
83+
///
84+
/// # Returns
85+
/// `Ok(())` on success, or a `WriterError` if writing fails.
7886
pub fn write(
7987
self,
8088
semantics: impl Iterator<Item = semantic::Command>,

bear/src/output/writers.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,19 @@ use crate::semantic::clang::{DuplicateEntryFilter, Entry};
88
use crate::{config, semantic};
99
use std::{fs, io, path};
1010

11-
/// The trait represents a writer for iterator type `T`.
11+
/// A trait representing a writer for iterator type `T`.
1212
///
1313
/// This trait is implemented by types that can consume an iterator of type `T`
1414
/// and write its elements to some output. The writing process may succeed or fail,
1515
/// returning either `()` on success or an error.
1616
pub(super) trait IteratorWriter<T> {
1717
/// Writes the iterator as a sequence of elements.
18-
/// It consumes the iterator and returns either a nothing or an error.
19-
fn write(self, _: impl Iterator<Item = T>) -> Result<(), WriterError>;
18+
///
19+
/// Consumes the iterator and returns either nothing on success or an error.
20+
fn write(self, items: impl Iterator<Item = T>) -> Result<(), WriterError>;
2021
}
2122

22-
/// This writer is used to write the semantic analysis results to a file.
23+
/// The type represents a writer for semantic analysis results to a file.
2324
///
2425
/// # Note
2526
/// The output format is not stable and may change in future versions.
@@ -51,7 +52,7 @@ impl IteratorWriter<semantic::Command> for SemanticOutputWriter {
5152
}
5253
}
5354

54-
/// Formats `semantic::CompilerCall` instances into `Entry` objects.
55+
/// The type represents a converter that formats `semantic::Command` instances into `Entry` objects.
5556
pub(super) struct ConverterClangOutputWriter<T: IteratorWriter<Entry>> {
5657
format: config::EntryFormat,
5758
writer: T,
@@ -73,7 +74,7 @@ impl<T: IteratorWriter<Entry>> IteratorWriter<semantic::Command> for ConverterCl
7374
}
7475
}
7576

76-
/// Handles the logic for appending entries to an existing Clang output file.
77+
/// The type represents a writer that handles appending entries to an existing Clang output file.
7778
///
7879
/// This writer supports reading existing entries from a compilation database file,
7980
/// combining them with new entries, and writing the result back to the file.
@@ -129,7 +130,7 @@ impl<T: IteratorWriter<Entry>> IteratorWriter<Entry> for AppendClangOutputWriter
129130
}
130131
}
131132

132-
/// Responsible for writing a JSON compilation database file atomically.
133+
/// The type represents a writer that writes JSON compilation database files atomically.
133134
///
134135
/// The file is first written to a temporary file and then renamed to the final file name.
135136
/// This ensures that the output file is not left in an inconsistent state in case of errors.
@@ -160,7 +161,7 @@ impl<T: IteratorWriter<Entry>> IteratorWriter<Entry> for AtomicClangOutputWriter
160161
}
161162
}
162163

163-
/// Responsible for writing a JSON compilation database file from the given entries.
164+
/// The type represents a writer that writes JSON compilation database files from given entries.
164165
///
165166
/// # Features
166167
/// - Filters duplicates based on the provided configuration.
@@ -190,7 +191,7 @@ impl<T: IteratorWriter<Entry>> IteratorWriter<Entry> for UniqueOutputWriter<T> {
190191
}
191192
}
192193

193-
/// Responsible for writing a JSON compilation database file from the given entries.
194+
/// The type represents a writer that writes JSON compilation database files from given entries.
194195
///
195196
/// # Features
196197
/// - Writes the entries to a file.

0 commit comments

Comments
 (0)