|
| 1 | +use core::panic::assert_info::{AssertInfo, Assertion, BinaryAssertion, BoolAssertion}; |
| 2 | +use core::panic::Location; |
| 3 | + |
| 4 | +const RED: &str = "\x1b[31m"; |
| 5 | +const YELLOW: &str = "\x1b[33m"; |
| 6 | +const BLUE: &str = "\x1b[34m"; |
| 7 | +const CYAN: &str = "\x1b[36m"; |
| 8 | +const BOLD: &str = "\x1b[1m"; |
| 9 | +const RESET: &str = "\x1b[0m"; |
| 10 | + |
| 11 | +/// Print an assertion to standard error. |
| 12 | +/// |
| 13 | +/// If `ansi_colors` is true, this function unconditionally prints ANSI color codes. |
| 14 | +/// It should only be set to true only if it is known that the terminal supports it. |
| 15 | +pub fn pretty_print_assertion(assert: &AssertInfo<'_>, loc: Location<'_>, ansi_colors: bool) { |
| 16 | + if ansi_colors { |
| 17 | + print_pretty_header(loc); |
| 18 | + match &assert.assertion { |
| 19 | + Assertion::Bool(assert) => print_pretty_bool_assertion(assert), |
| 20 | + Assertion::Binary(assert) => print_pretty_binary_assertion(assert), |
| 21 | + } |
| 22 | + if let Some(msg) = &assert.message { |
| 23 | + print_pretty_message(msg); |
| 24 | + } |
| 25 | + } else { |
| 26 | + print_plain_header(loc); |
| 27 | + match &assert.assertion { |
| 28 | + Assertion::Bool(assert) => print_plain_bool_assertion(assert), |
| 29 | + Assertion::Binary(assert) => print_plain_binary_assertion(assert), |
| 30 | + } |
| 31 | + if let Some(msg) = &assert.message { |
| 32 | + print_plain_message(msg); |
| 33 | + } |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +fn print_plain_header(loc: Location<'_>) { |
| 38 | + eprintln!("Assertion failed at {}:{}:{}:", loc.file(), loc.line(), loc.column()) |
| 39 | +} |
| 40 | + |
| 41 | +fn print_pretty_header(loc: Location<'_>) { |
| 42 | + eprintln!( |
| 43 | + "{bold}{red}Assertion failed{reset} at {bold}{file}{reset}:{line}:{col}:", |
| 44 | + red = RED, |
| 45 | + bold = BOLD, |
| 46 | + reset = RESET, |
| 47 | + file = loc.file(), |
| 48 | + line = loc.line(), |
| 49 | + col = loc.column(), |
| 50 | + ); |
| 51 | +} |
| 52 | + |
| 53 | +fn print_plain_bool_assertion(assert: &BoolAssertion) { |
| 54 | + eprintln!("Assertion:\n {}", assert.expr); |
| 55 | + eprintln!("Expansion:\n false"); |
| 56 | +} |
| 57 | + |
| 58 | +fn print_pretty_bool_assertion(assert: &BoolAssertion) { |
| 59 | + eprintln!( |
| 60 | + "{bold}Assertion:{reset}\n {cyan}{expr}{reset}", |
| 61 | + cyan = CYAN, |
| 62 | + reset = RESET, |
| 63 | + bold = BOLD, |
| 64 | + expr = assert.expr, |
| 65 | + ); |
| 66 | + eprintln!( |
| 67 | + "{bold}Expansion:{reset}\n {cyan}false{reset}", |
| 68 | + cyan = CYAN, |
| 69 | + bold = BOLD, |
| 70 | + reset = RESET, |
| 71 | + ); |
| 72 | +} |
| 73 | + |
| 74 | +fn print_plain_binary_assertion(assert: &BinaryAssertion<'_>) { |
| 75 | + eprintln!("Assertion:\n {} {} {}", assert.left_expr, assert.op, assert.right_expr); |
| 76 | + eprintln!("Expansion:\n {:?} {} {:?}", assert.left_val, assert.op, assert.right_val); |
| 77 | +} |
| 78 | + |
| 79 | +fn print_pretty_binary_assertion(assert: &BinaryAssertion<'_>) { |
| 80 | + eprintln!( |
| 81 | + "{bold}Assertion:{reset}\n {cyan}{left} {bold}{blue}{op}{reset} {yellow}{right}{reset}", |
| 82 | + cyan = CYAN, |
| 83 | + blue = BLUE, |
| 84 | + yellow = YELLOW, |
| 85 | + bold = BOLD, |
| 86 | + reset = RESET, |
| 87 | + left = assert.left_expr, |
| 88 | + op = assert.op, |
| 89 | + right = assert.right_expr, |
| 90 | + ); |
| 91 | + eprintln!( |
| 92 | + "{bold}Expansion:{reset}\n {cyan}{left:?} {bold}{blue}{op}{reset} {yellow}{right:?}{reset}", |
| 93 | + cyan = CYAN, |
| 94 | + blue = BLUE, |
| 95 | + yellow = YELLOW, |
| 96 | + bold = BOLD, |
| 97 | + reset = RESET, |
| 98 | + left = assert.left_val, |
| 99 | + op = assert.op, |
| 100 | + right = assert.right_val, |
| 101 | + ); |
| 102 | +} |
| 103 | + |
| 104 | +fn print_plain_message(message: &std::fmt::Arguments<'_>) { |
| 105 | + eprintln!("Message:\n {}", message); |
| 106 | +} |
| 107 | + |
| 108 | +fn print_pretty_message(message: &std::fmt::Arguments<'_>) { |
| 109 | + eprintln!("{bold}Message:{reset}\n {msg}", bold = BOLD, reset = RESET, msg = message,); |
| 110 | +} |
0 commit comments