Skip to content

Commit

Permalink
refactor(compare): adress review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mstruebing committed Dec 12, 2020
1 parent 6ec8e98 commit 24f5cc3
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/common.rs
Expand Up @@ -7,12 +7,12 @@ mod warning;

use colored::*;
pub use compare::CompareFileType;
pub use compare::CompareWarning;
pub use file_entry::FileEntry;
pub use line_entry::LineEntry;
pub use output::check::CheckOutput;
pub use output::compare::CompareOutput;
pub use output::fix::FixOutput;
pub use warning::CompareWarning;
pub use warning::Warning;

pub const LF: &str = "\n";
Expand Down
24 changes: 22 additions & 2 deletions src/common/compare.rs
@@ -1,15 +1,35 @@
use std::fmt;
use std::path::PathBuf;

use crate::common::*;

// A structure used to compare environment files
#[derive(Debug)]
pub struct CompareFileType {
pub path: PathBuf,
pub keys: Vec<String>,
pub missing: Vec<String>,
}

#[derive(Debug)]
pub struct CompareWarning {
pub path: PathBuf,
pub missing_keys: Vec<String>,
}

impl fmt::Display for CompareWarning {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{:?}",
format!(
"{} is missing keys: {}",
self.path.display(),
self.missing_keys
.clone()
.into_iter()
.collect::<Vec<_>>()
.join(", ")
)
.italic(),
)
}
}
4 changes: 2 additions & 2 deletions src/common/output/compare.rs
Expand Up @@ -13,12 +13,12 @@ impl CompareOutput {
/// Prints information about a file in process
pub fn print_processing_info(&self, file: &FileEntry) {
if !self.is_quiet_mode {
println!("Collecting keys of file {}", file);
println!("Comparing {}", file);
}
}

/// Prints warnings without any additional information
pub fn print_warnings(&self, warnings: &[CompareWarning]) {
warnings.iter().for_each(|w| println!("{}", w.as_str()));
warnings.iter().for_each(|w| println!("{}", w))
}
}
21 changes: 0 additions & 21 deletions src/common/warning.rs
@@ -1,28 +1,7 @@
use std::fmt;
use std::path::PathBuf;

use crate::common::*;

#[derive(Debug)]
pub struct CompareWarning {
pub path: PathBuf,
pub missing_keys: Vec<String>,
}

impl CompareWarning {
pub fn as_str(&self) -> String {
format!(
"{:?} is missing keys: {:?}",
self.path,
self.missing_keys
.clone()
.into_iter()
.collect::<Vec<_>>()
.join(", ")
)
}
}

#[derive(Clone, Debug, PartialEq)]
pub struct Warning {
pub check_name: String,
Expand Down
14 changes: 7 additions & 7 deletions src/lib.rs
Expand Up @@ -131,7 +131,7 @@ fn get_needed_file_paths(args: &clap::ArgMatches) -> Vec<PathBuf> {
pub fn compare(
args: &clap::ArgMatches,
current_dir: &PathBuf,
) -> Result<Vec<String>, Box<dyn Error>> {
) -> Result<Vec<CompareWarning>, Box<dyn Error>> {
let mut all_keys: HashSet<String> = HashSet::new();
let lines_map = get_lines(args, current_dir)?;
let output = CompareOutput::new(args.is_present("quiet"));
Expand All @@ -141,19 +141,19 @@ pub fn compare(

// Nothing to check
if lines_map.is_empty() {
return Ok(warnings.iter().map(|w| w.as_str()).collect());
return Ok(warnings);
}

// // create CompareFileType structures for each file
// // Create CompareFileType structures for each file
for (_, (fe, strings)) in lines_map.into_iter().enumerate() {
output.print_processing_info(&fe);
let lines = get_line_entries(&fe, strings);
let mut keys: Vec<String> = Vec::new();

for line in lines {
if let Some(key) = line.get_key() {
all_keys.insert(key.clone());
keys.push(key)
all_keys.insert(key.to_string());
keys.push(key.to_string());
}
}

Expand All @@ -166,7 +166,7 @@ pub fn compare(
files_to_compare.push(file_to_compare);
}

// create warnings if any file misses any key
// Create warnings if any file misses any key
for file in files_to_compare {
let missing_keys: Vec<_> = all_keys
.iter()
Expand All @@ -184,7 +184,7 @@ pub fn compare(
}

output.print_warnings(&warnings);
Ok(warnings.iter().map(|w| w.as_str()).collect())
Ok(warnings)
}

fn get_file_paths(
Expand Down
7 changes: 3 additions & 4 deletions src/main.rs
Expand Up @@ -41,10 +41,9 @@ fn main() -> Result<(), Box<dyn Error>> {
process::exit(0);
}
("compare", Some(files)) => {
if let Ok(warnings) = dotenv_linter::compare(&files, &current_dir) {
if warnings.is_empty() {
process::exit(0);
}
let warnings = dotenv_linter::compare(&files, &current_dir)?;
if warnings.is_empty() {
process::exit(0);
}
}
_ => {
Expand Down

0 comments on commit 24f5cc3

Please sign in to comment.