| 347 | } |
| 348 | |
| 349 | fn format_bytes(data: &[u8], f: &mut impl fmt::Write) -> fmt::Result { |
| 350 | #![allow( |
| 351 | clippy::assertions_on_constants, |
| 352 | reason = "enforce constant relationships on edit" |
| 353 | )] |
| 354 | |
| 355 | const LINES_MIN_OVERFLOW: usize = 80; |
| 356 | const LINES_MAX_START: usize = 20; |
| 357 | const LINES_MAX_END: usize = 40; |
| 358 | const LINES_MAX_PRINTED: usize = LINES_MAX_START + LINES_MAX_END; |
| 359 | |
| 360 | const BYTES_MIN_OVERFLOW: usize = 8192; |
| 361 | const BYTES_MAX_START: usize = 2048; |
| 362 | const BYTES_MAX_END: usize = 2048; |
| 363 | const BYTES_MAX_PRINTED: usize = BYTES_MAX_START + BYTES_MAX_END; |
| 364 | |
| 365 | assert!(LINES_MAX_PRINTED < LINES_MIN_OVERFLOW); |
| 366 | assert!(BYTES_MAX_PRINTED < BYTES_MIN_OVERFLOW); |
| 367 | |
| 368 | let lines_total = data.as_bstr().lines_with_terminator().count(); |
| 369 | let multiline = 1 < lines_total; |
| 370 | |
| 371 | if LINES_MIN_OVERFLOW <= lines_total { |
| 372 | let lines_omitted = lines_total - LINES_MAX_PRINTED; |
| 373 | let start_lines = data.as_bstr().lines_with_terminator().take(LINES_MAX_START); |
| 374 | let end_lines = data |
| 375 | .as_bstr() |
| 376 | .lines_with_terminator() |
| 377 | .skip(LINES_MAX_START + lines_omitted); |
| 378 | writeln!(f, "<{lines_total} lines total>")?; |
| 379 | write_debug_bstrs(f, true, start_lines)?; |
| 380 | writeln!(f, "<{lines_omitted} lines omitted>")?; |
| 381 | write_debug_bstrs(f, true, end_lines) |
| 382 | } else if BYTES_MIN_OVERFLOW <= data.len() { |
| 383 | write!( |
| 384 | f, |
| 385 | "<{} bytes total>{}", |
| 386 | data.len(), |
| 387 | if multiline { "\n" } else { "" } |
| 388 | )?; |
| 389 | write_debug_bstrs( |
| 390 | f, |
| 391 | multiline, |
| 392 | data[..BYTES_MAX_START].lines_with_terminator(), |
| 393 | )?; |
| 394 | write!( |
| 395 | f, |
| 396 | "<{} bytes omitted>{}", |
| 397 | data.len() - BYTES_MAX_PRINTED, |
| 398 | if multiline { "\n" } else { "" } |
| 399 | )?; |
| 400 | write_debug_bstrs( |
| 401 | f, |
| 402 | multiline, |
| 403 | data[data.len() - BYTES_MAX_END..].lines_with_terminator(), |
| 404 | ) |
| 405 | } else { |
| 406 | write_debug_bstrs(f, multiline, data.lines_with_terminator()) |