mod human;
mod json;
#[cfg(feature = "read")]
pub(crate) mod read;
#[cfg(feature = "search")]
pub(crate) mod search;
use crate::{args::ValidationFormat, diagnostics::ValidationResult};
#[cfg(any(feature = "search", feature = "read"))]
const KILOBYTE: u64 = 1_000;
#[cfg(any(feature = "search", feature = "read"))]
const MEGABYTE: u64 = 1_000_000;
#[cfg(any(feature = "search", feature = "read"))]
pub(crate) fn format_bytes(bytes: u64) -> String {
let tenths = |unit: u64| bytes.saturating_add(unit / 20) / (unit / 10);
if bytes < KILOBYTE {
format!("{bytes}B")
} else if bytes < MEGABYTE {
let tenths = tenths(KILOBYTE);
format!("{}.{}kB", tenths / 10, tenths % 10)
} else {
let tenths = tenths(MEGABYTE);
format!("{}.{}MB", tenths / 10, tenths % 10)
}
}
fn escape_human_character(character: char, escaped: &mut String) -> bool {
match character {
'\n' => escaped.push_str("\\n"),
'\r' => escaped.push_str("\\r"),
'\t' => escaped.push_str("\\t"),
'\u{1b}' => escaped.push_str("\\x1b"),
character
if character.is_control()
|| matches!(
character,
'\u{061c}'
| '\u{200e}'
| '\u{200f}'
| '\u{2028}'..='\u{202e}'
| '\u{2066}'..='\u{206f}'
) =>
{
escaped.push_str(&format!("\\u{{{:x}}}", u32::from(character)));
}
_ => return false,
}
true
}
pub(crate) fn escape_human(value: &str) -> String {
let mut escaped = String::new();
for character in value.chars() {
if !escape_human_character(character, &mut escaped) {
escaped.push(character);
}
}
escaped
}
fn escape_human_quoted(value: &str) -> String {
let mut escaped = String::new();
for character in value.chars() {
if escape_human_character(character, &mut escaped) {
continue;
}
match character {
'\\' => escaped.push_str("\\\\"),
'"' => escaped.push_str("\\\""),
character => escaped.push(character),
}
}
escaped
}
#[cfg(any(feature = "search", feature = "read"))]
pub(crate) fn escape_compact(value: &str) -> String {
let mut escaped = String::with_capacity(value.len());
for character in value.chars() {
match character {
'\\' => escaped.push_str("\\\\"),
'\t' => escaped.push_str("\\t"),
'\n' => escaped.push_str("\\n"),
character if character.is_control() => {
escaped.push_str(&format!("\\u{{{:x}}}", u32::from(character)));
}
character => escaped.push(character),
}
}
escaped
}
pub(crate) fn render(
results: &[ValidationResult],
format: ValidationFormat,
use_color: bool,
) -> String {
match format {
ValidationFormat::Human => human::render_human(results, use_color),
ValidationFormat::Json => json::render_json(results),
}
}
#[cfg(all(test, any(feature = "search", feature = "read")))]
mod tests {
use super::{escape_compact, format_bytes};
#[test]
fn byte_sizes_use_human_units() {
assert_eq!(format_bytes(412), "412B");
assert_eq!(format_bytes(3_140), "3.1kB");
assert_eq!(format_bytes(2_450_000), "2.5MB");
}
#[test]
fn compact_fields_escape_delimiters_controls_and_backslashes() {
assert_eq!(
escape_compact("a\tb\nc\rd\\e\u{7}"),
"a\\tb\\nc\\u{d}d\\\\e\\u{7}"
);
}
}