Skip to content

Commit

Permalink
Merge 11c7da9 into 7d7b09b
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAdiWijaya committed Jan 13, 2023
2 parents 7d7b09b + 11c7da9 commit d8a135e
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 98 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ Currently we only support SHA-256 but more checks can be added easily.
- Deletion of columns will no longer really delete them but replace every value with "DELETED"
- Expose config struct to library API
- Fixed a bug regarding wrong handling of multiple empty lines
- Reworked CSV reporting to have an interleaved and more compact view

### 0.1.4
- Add multiple includes and excludes - warning, this will break yamls from 0.1.3 and earlier
Expand Down
2 changes: 1 addition & 1 deletion src/csv/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl Display for Value {
write!(f, "{}", val).unwrap();
}
Value::String(val) => {
write!(f, "'{}'s", val).unwrap();
write!(f, "'{}'", val).unwrap();
}
}
Ok(())
Expand Down
141 changes: 100 additions & 41 deletions src/report/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,30 @@ pub(crate) struct RuleResult {
}

#[derive(Serialize, Debug, Clone)]
pub struct CSVReport {
pub struct CSVReportColumn {
pub nominal_value: String,
pub actual_value: String,
pub diffs: Vec<String>,
}

#[derive(Serialize, Debug, Clone)]
pub struct CSVReportRow {
pub columns: Vec<CSVReportColumn>,
pub has_diff: bool,
pub has_error: bool,
}

fn get_file_name(path: &Path) -> Result<Cow<str>, Error> {
path.file_name()
.map(|f| f.to_string_lossy())
.ok_or_else(|| {
Error::FileNameParsing(format!(
"Could not extract filename from {}",
path.to_string_lossy()
))
})
}

pub fn create_sub_folder(
rule_name: &str,
nominal: impl AsRef<Path>,
Expand Down Expand Up @@ -76,9 +94,12 @@ pub fn write_html_detail(
diffs: &[String],
rule_name: &str,
) -> Result<FileCompareResult, Error> {
let nominal_file_name = get_file_name(nominal.as_ref())?.to_string();
let actual_file_name = get_file_name(actual.as_ref())?.to_string();

let mut result = FileCompareResult {
nominal: nominal.as_ref().to_string_lossy().to_string(),
actual: actual.as_ref().to_string_lossy().to_string(),
nominal: nominal_file_name,
actual: actual_file_name,
is_error: false,
detail_path: None,
};
Expand Down Expand Up @@ -123,43 +144,66 @@ pub(crate) fn write_csv_detail(
diffs: &[DiffType],
rule_name: &str,
) -> Result<FileCompareResult, Error> {
let nominal_file_name = get_file_name(nominal.as_ref())?.to_string();
let actual_file_name = get_file_name(actual.as_ref())?.to_string();

let mut result = FileCompareResult {
nominal: nominal.as_ref().to_string_lossy().to_string(),
actual: actual.as_ref().to_string_lossy().to_string(),
nominal: nominal_file_name,
actual: actual_file_name,
is_error: false,
detail_path: None,
};

let headers: Vec<_> = nominal_table
let mut headers: CSVReportRow = CSVReportRow {
columns: vec![],
has_diff: false,
has_error: false,
};

nominal_table
.columns
.iter()
.zip(actual_table.columns.iter())
.map(|(n, a)| CSVReport {
actual_value: a
.header
.as_deref()
.unwrap_or("Header preprocessing not enabled in config")
.to_owned(),
nominal_value: n
.header
.as_deref()
.unwrap_or("Header preprocessing not enabled in config")
.to_owned(),
diffs: Vec::new(),
})
.collect();
.for_each(|(n, a)| {
let a_header = a.header.as_deref();
let n_header = n.header.as_deref();

if a_header.is_some() && n_header.is_some() {
let actual_value = a_header
.unwrap_or("Header preprocessing not enabled in config")
.to_owned();

let nominal_value = n_header
.unwrap_or("Header preprocessing not enabled in config")
.to_owned();

if nominal_value != actual_value {
headers.has_diff = true;
}

let rows: Vec<Vec<_>> = nominal_table
headers.columns.push(CSVReportColumn {
actual_value,
nominal_value,
diffs: Vec::new(),
});
}
});

let rows: Vec<CSVReportRow> = nominal_table
.rows()
.zip(actual_table.rows())
.enumerate()
.map(|(row, (n, a))| {
n.into_iter()
let mut has_diff = false;
let mut has_error = false;

let columns: Vec<CSVReportColumn> = n
.into_iter()
.zip(a.into_iter())
.enumerate()
.map(|(col, (n, a))| {
let current_pos = Position { col, row };
CSVReport {
let csv_report = CSVReportColumn {
nominal_value: n.to_string(),
actual_value: a.to_string(),
diffs: diffs
Expand All @@ -183,9 +227,25 @@ pub(crate) fn write_csv_detail(
}
})
.collect(),
};

if !csv_report.diffs.is_empty() {
has_error = true;
}

if csv_report.nominal_value != csv_report.actual_value {
has_diff = true;
}

csv_report
})
.collect()
.collect();

CSVReportRow {
has_error,
has_diff,
columns,
}
})
.collect();

Expand All @@ -199,11 +259,14 @@ pub(crate) fn write_csv_detail(
template::PLAIN_CSV_DETAIL_TEMPLATE,
)?;

let row_index_increment: usize = if headers.columns.is_empty() { 0 } else { 1 };

let mut ctx = Context::new();
ctx.insert("actual", &actual.as_ref().to_string_lossy());
ctx.insert("nominal", &nominal.as_ref().to_string_lossy());
ctx.insert("rows", &rows);
ctx.insert("headers", &headers);
ctx.insert("row_index_increment", &row_index_increment);

let file = fat_io_wrap_std(&detail_file, &File::create)?;
info!("detail html {:?} created", &detail_file);
Expand All @@ -222,9 +285,12 @@ pub fn write_image_detail(
diffs: &[String],
rule_name: &str,
) -> Result<FileCompareResult, Error> {
let nominal_file_name = get_file_name(nominal.as_ref())?.to_string();
let actual_file_name = get_file_name(actual.as_ref())?.to_string();

let mut result = FileCompareResult {
nominal: nominal.as_ref().to_string_lossy().to_string(),
actual: actual.as_ref().to_string_lossy().to_string(),
nominal: nominal_file_name,
actual: actual_file_name,
is_error: false,
detail_path: None,
};
Expand All @@ -247,17 +313,6 @@ pub fn write_image_detail(
ctx.insert("actual", &actual.as_ref().to_string_lossy());
ctx.insert("nominal", &nominal.as_ref().to_string_lossy());

fn get_file_name(path: &Path) -> Result<Cow<str>, Error> {
path.file_name()
.map(|f| f.to_string_lossy())
.ok_or_else(|| {
Error::FileNameParsing(format!(
"Could not extract filename from {}",
path.to_string_lossy()
))
})
}

let actual_file_extension = get_file_name(actual.as_ref())?;
let nominal_file_extension = get_file_name(nominal.as_ref())?;

Expand Down Expand Up @@ -298,9 +353,12 @@ pub fn write_pdf_detail(
diffs: &[(usize, String)],
rule_name: &str,
) -> Result<FileCompareResult, Error> {
let nominal_file_name = get_file_name(nominal.as_ref())?.to_string();
let actual_file_name = get_file_name(actual.as_ref())?.to_string();

let mut result = FileCompareResult {
nominal: nominal.as_ref().to_string_lossy().to_string(),
actual: actual.as_ref().to_string_lossy().to_string(),
nominal: nominal_file_name,
actual: actual_file_name,
is_error: false,
detail_path: None,
};
Expand All @@ -327,13 +385,13 @@ pub fn write_pdf_detail(
template::PLAIN_PDF_DETAIL_TEMPLATE,
)?;

let combined_lines: Vec<CSVReport> = actual_string
let combined_lines: Vec<CSVReportColumn> = actual_string
.lines()
.enumerate()
.into_iter()
.zip(nominal_string.lines().into_iter())
.map(|((l, a), n)| {
let mut result = CSVReport {
let mut result = CSVReportColumn {
nominal_value: n.replace(' ', "&nbsp;"),
actual_value: a.replace(' ', "&nbsp;"),
diffs: vec![],
Expand Down Expand Up @@ -421,6 +479,7 @@ pub(crate) fn write_index(
let index_file = report_dir.as_ref().join(template::INDEX_FILENAME);

let mut tera = Tera::default();

tera.add_raw_template(&index_file.to_string_lossy(), template::INDEX_TEMPLATE)?;

let mut ctx = Context::new();
Expand Down

0 comments on commit d8a135e

Please sign in to comment.