use crate::exit_codes::ExitCode;
use serde::Serialize;
#[derive(Serialize)]
pub struct VerifyReport {
pub report_version: u32,
pub schema_fingerprint: String,
pub record_format: String,
pub file: String,
pub file_size_bytes: u64,
pub cli_opts: VerifyCliEcho,
pub records_total: u64,
pub errors_total: u64,
pub truncated: bool,
pub errors: Vec<VerifyError>,
pub sample: Vec<VerifySample>,
}
#[derive(Serialize)]
pub struct VerifyCliEcho {
pub codepage: String,
pub strict: bool,
pub max_errors: u32,
pub sample: u32,
pub strict_comments: bool,
}
#[derive(Serialize)]
pub struct VerifyError {
pub index: u64,
pub code: String,
pub field: Option<String>,
pub offset: Option<u64>,
pub msg: String,
pub hex: Option<String>,
}
#[derive(Serialize)]
pub struct VerifySample {
pub index: u64,
pub hex: String,
}
impl VerifyReport {
pub fn new(
schema_fingerprint: String,
record_format: String,
file: String,
file_size_bytes: u64,
cli_opts: VerifyCliEcho,
) -> Self {
Self {
report_version: 1,
schema_fingerprint,
record_format,
file,
file_size_bytes,
cli_opts,
records_total: 0,
errors_total: 0,
truncated: false,
errors: Vec::new(),
sample: Vec::new(),
}
}
pub fn add_error(&mut self, error: VerifyError) {
if self.errors.len() < self.cli_opts.max_errors as usize {
self.errors.push(error);
} else {
self.truncated = true;
}
self.errors_total += 1;
}
pub fn add_sample(&mut self, sample: VerifySample) {
if self.sample.len() < self.cli_opts.sample as usize {
self.sample.push(sample);
}
}
pub fn set_records_total(&mut self, total: u64) {
self.records_total = total;
}
pub fn exit_code(&self) -> ExitCode {
if self.errors_total > 0 {
ExitCode::Encode } else {
ExitCode::Ok }
}
}