Skip to content

Commit a0ba2a9

Browse files
committed
rust: config validation is in the config module
1 parent 080dd0e commit a0ba2a9

File tree

2 files changed

+27
-27
lines changed

2 files changed

+27
-27
lines changed

rust/bear/src/config.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
You should have received a copy of the GNU General Public License
1717
along with this program. If not, see <http://www.gnu.org/licenses/>.
1818
*/
19+
use std::collections::HashSet;
1920
use std::fs::OpenOptions;
2021
use std::path::{Path, PathBuf};
2122

@@ -286,6 +287,17 @@ pub struct Filter {
286287
pub duplicates: DuplicateFilter,
287288
}
288289

290+
impl Filter {
291+
/// Validate the configuration of the output writer.
292+
pub fn validate(&self) -> Self {
293+
Self {
294+
source: self.source.clone(),
295+
compilers: self.compilers.clone(),
296+
duplicates: self.duplicates.validate(),
297+
}
298+
}
299+
}
300+
289301
impl Default for Filter {
290302
fn default() -> Self {
291303
Filter {
@@ -335,6 +347,20 @@ pub struct DuplicateFilter {
335347
pub by_fields: Vec<OutputFields>,
336348
}
337349

350+
impl DuplicateFilter {
351+
/// Deduplicate the fields of the fields vector.
352+
pub fn validate(&self) -> Self {
353+
Self {
354+
by_fields: (&self.by_fields)
355+
.into_iter()
356+
.map(|field| field.clone())
357+
.collect::<HashSet<_>>()
358+
.into_iter()
359+
.collect(),
360+
}
361+
}
362+
}
363+
338364
impl Default for DuplicateFilter {
339365
fn default() -> Self {
340366
DuplicateFilter {

rust/bear/src/output/mod.rs

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
You should have received a copy of the GNU General Public License
1717
along with this program. If not, see <http://www.gnu.org/licenses/>.
1818
*/
19-
use std::collections::HashSet;
2019
use std::fs::{File, OpenOptions};
2120
use std::io::{BufReader, BufWriter};
2221
use std::path::{Path, PathBuf};
@@ -51,7 +50,7 @@ impl OutputWriter {
5150
config::Output::Clang { format, filter, .. } => OutputWriter {
5251
output: PathBuf::from(&args.file_name),
5352
append: args.append,
54-
filter: validate_filter(filter),
53+
filter: filter.validate(),
5554
format: format.clone(),
5655
},
5756
config::Output::Semantic { .. } => {
@@ -143,31 +142,6 @@ impl OutputWriter {
143142
}
144143
}
145144

146-
/// Validate the configuration of the output writer.
147-
///
148-
/// Validation is always successful, but it may modify the configuration values.
149-
fn validate_filter(filter: &config::Filter) -> config::Filter {
150-
config::Filter {
151-
source: filter.source.clone(),
152-
compilers: filter.compilers.clone(),
153-
duplicates: config::DuplicateFilter {
154-
by_fields: validate_duplicates_by_fields(filter.duplicates.by_fields.as_slice()),
155-
},
156-
}
157-
}
158-
159-
/// Validate the fields of the configuration.
160-
///
161-
/// Removes the duplicates from the list of fields.
162-
fn validate_duplicates_by_fields(fields: &[config::OutputFields]) -> Vec<config::OutputFields> {
163-
fields
164-
.into_iter()
165-
.map(|field| field.clone())
166-
.collect::<HashSet<_>>()
167-
.into_iter()
168-
.collect()
169-
}
170-
171145
pub fn into_entries(value: semantic::Meaning) -> Result<Vec<Entry>, anyhow::Error> {
172146
match value {
173147
semantic::Meaning::Compiler {

0 commit comments

Comments
 (0)