Skip to content

Commit

Permalink
Add traits for generic diff types for reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher Regali committed Jul 25, 2023
1 parent 97c747d commit a8e7376
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 17 deletions.
10 changes: 5 additions & 5 deletions src/csv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ pub enum Error {
UnequalRowCount(usize, usize),
}

#[derive(Clone, Copy, Debug)]
pub(crate) struct Position {
#[derive(Clone, Copy, Debug, Serialize)]
pub struct Position {
pub row: usize,
pub col: usize,
}

#[derive(Debug)]
pub(crate) enum DiffType {
#[derive(Debug, Serialize, Clone)]
pub enum DiffType {
UnequalStrings {
nominal: String,
actual: String,
Expand Down Expand Up @@ -209,7 +209,7 @@ impl Mode {
}
}

#[derive(JsonSchema, Deserialize, Serialize, Debug, Default)]
#[derive(JsonSchema, Deserialize, Serialize, Debug, Default, Clone)]
/// Settings for the CSV comparison module
pub struct CSVCompareConfig {
#[serde(flatten)]
Expand Down
4 changes: 2 additions & 2 deletions src/csv/preprocessing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use serde::{Deserialize, Serialize};
use std::cmp::Ordering::Equal;
use tracing::{debug, warn};

#[derive(JsonSchema, Deserialize, Serialize, Debug)]
#[derive(JsonSchema, Deserialize, Serialize, Debug, Clone)]
/// Preprocessor options
pub enum Preprocessor {
/// Try to extract the headers from the first row - fallible if first row contains a number
Expand Down Expand Up @@ -237,7 +237,7 @@ mod tests {
File::open("tests/csv/data/defects_headers.csv").unwrap(),
&delimiters,
)
.unwrap()
.unwrap()
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/csv/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl Display for Quantity {
}
}

#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Clone, Serialize)]
pub enum Value {
Quantity(Quantity),
String(String),
Expand Down
2 changes: 1 addition & 1 deletion src/external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize};
use std::path::Path;
use tracing::{error, info};

#[derive(Debug, Deserialize, Serialize, JsonSchema)]
#[derive(Debug, Deserialize, Serialize, JsonSchema, Clone)]
pub struct ExternalConfig {
/// The executable to call - will be started like: `#executable #(#extra_params)* #nominal #actual`
executable: String,
Expand Down
4 changes: 2 additions & 2 deletions src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use thiserror::Error;
use vg_errortools::fat_io_wrap_std;
use vg_errortools::FatIOError;

#[derive(Debug, Deserialize, Serialize, JsonSchema)]
#[derive(Debug, Deserialize, Serialize, JsonSchema, Clone, Copy)]
pub enum HashFunction {
Sha256,
}
Expand Down Expand Up @@ -43,7 +43,7 @@ impl HashFunction {
}
}

#[derive(Debug, Deserialize, Serialize, JsonSchema)]
#[derive(Debug, Deserialize, Serialize, JsonSchema, Clone)]
/// Configuration options for the hash comparison module
pub struct HashConfig {
/// Which hash function to use
Expand Down
2 changes: 1 addition & 1 deletion src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use tracing::error;
use vg_errortools::fat_io_wrap_std;
use vg_errortools::FatIOError;

#[derive(Debug, Deserialize, Serialize, JsonSchema)]
#[derive(Debug, Deserialize, Serialize, JsonSchema, Clone)]
/// Plain text comparison config, also used for PDF
pub struct HTMLCompareConfig {
/// Normalized Damerau-Levenshtein distance, 0.0 = bad, 1.0 = identity
Expand Down
2 changes: 1 addition & 1 deletion src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::path::{Path, PathBuf};
use thiserror::Error;
use tracing::error;

#[derive(JsonSchema, Deserialize, Serialize, Debug)]
#[derive(JsonSchema, Deserialize, Serialize, Debug, Clone)]
/// Image comparison config options
pub struct ImageCompareConfig {
/// Threshold for image comparison < 0.5 is very dissimilar, 1.0 is identical
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub enum Error {
DifferentNumberOfFiles(usize, usize),
}

#[derive(Debug, Deserialize, Serialize, JsonSchema)]
#[derive(Debug, Deserialize, Serialize, JsonSchema, Clone)]
#[allow(clippy::upper_case_acronyms)]
/// Representing the comparison mode
pub enum ComparisonMode {
Expand Down Expand Up @@ -122,7 +122,7 @@ impl ConfigurationFile {
}
}

#[derive(Debug, Deserialize, Serialize, JsonSchema)]
#[derive(Debug, Deserialize, Serialize, JsonSchema, Clone)]
/// Representing a single comparison rule
pub struct Rule {
/// The name of the rule - will be displayed in logs
Expand Down
2 changes: 1 addition & 1 deletion src/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::time::SystemTime;
use tracing::error;

/// the configuration struct for file property comparison
#[derive(Debug, Deserialize, Serialize, JsonSchema)]
#[derive(Debug, Deserialize, Serialize, JsonSchema, Clone)]
pub struct PropertiesConfig {
/// Compare the file size, difference must be smaller then given value
file_size_tolerance_bytes: Option<u64>,
Expand Down
28 changes: 27 additions & 1 deletion src/report/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod template;

use crate::csv::{DiffType, Position, Table};
use crate::Rule;
use serde::Serialize;
use std::borrow::Cow;
use std::ffi::OsStr;
Expand Down Expand Up @@ -53,7 +54,7 @@ pub struct DetailPath {

#[derive(Serialize, Debug)]
pub(crate) struct RuleResult {
pub rule: crate::Rule,
pub rule: Rule,
pub compare_results: Vec<FileCompareResult>,
}

Expand All @@ -71,6 +72,31 @@ pub struct CSVReportRow {
pub has_error: bool,
}

#[derive(Serialize, Debug, Clone, Default)]
pub struct Report {
pub diffs: Vec<Difference>,
pub rules: Vec<Rule>,
}

#[derive(Serialize, Debug, Clone)]
pub struct Difference {
file_path: PathBuf,
rule_name: String,
is_error: bool,
detail: DiffDetail,
}

#[derive(Serialize, Debug, Clone)]
#[allow(clippy::upper_case_acronyms)]
pub enum DiffDetail {
CSV(DiffType),
Image { score: f64, diff_image: PathBuf },
Text { line: usize, score: f64 },
Hash { actual: String, nominal: String },
External { stdout: String, stderr: String },
Properties,
}

pub fn create_sub_folder() -> Result<DetailPath, Error> {
let temp_path = tempfile::Builder::new()
.prefix("havocompare-")
Expand Down

0 comments on commit a8e7376

Please sign in to comment.