Merge rust-bitcoin/rust-bitcoin#1913: Manually implement Debug on Witness
d45dbef3e7
Manually implement Debug on Witness (Tobin C. Harding) Pull request description: The current derived debug implementation on `Witness` prints the content field as an array of integers. We can do better than this by manually implementing `Debug`. With this applied `Witness` is printed as follows: (first line is `{:?}` and the next is `{:#?}`): Using `{:?}`: ``` Witness: { indices: 3, indices_start: 8, witnesses: [[0x00], [0x02, 0x03], [0x04, 0x05]] } ``` Using `{:#?}`: ``` Witness: { indices: 3, indices_start: 8, witnesses: [ [0x00], [0x02, 0x03], [0x04, 0x05], ], } ``` ACKs for top commit: sanket1729: tested ACKd45dbef3e7
. This would be helpful for debugging downstream. apoelstra: ACKd45dbef3e7
Tree-SHA512: eacf4fa8e3f38c4e9ddc45de78afb8eab5b5b196b77a6612f61860e0e4e7ba96de2e7f434b92816e0b00532e73c05378cafc046ec9c34108e9d9216fb36c524a
This commit is contained in:
commit
a7fe0f5695
|
@ -6,6 +6,7 @@
|
|||
//!
|
||||
|
||||
use core::convert::TryInto;
|
||||
use core::fmt;
|
||||
use core::ops::Index;
|
||||
|
||||
use secp256k1::ecdsa;
|
||||
|
@ -28,7 +29,7 @@ use crate::{Script, VarInt};
|
|||
/// saving some allocations.
|
||||
///
|
||||
/// [segwit upgrade]: <https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki>
|
||||
#[derive(Clone, Default, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
|
||||
#[derive(Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub struct Witness {
|
||||
/// Contains the witness `Vec<Vec<u8>>` serialization without the initial varint indicating the
|
||||
/// number of elements (which is stored in `witness_elements`).
|
||||
|
@ -45,6 +46,108 @@ pub struct Witness {
|
|||
indices_start: usize,
|
||||
}
|
||||
|
||||
impl fmt::Debug for Witness {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
|
||||
if f.alternate() {
|
||||
fmt_debug_pretty(self, f)
|
||||
} else {
|
||||
fmt_debug(self, f)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn fmt_debug(w: &Witness, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
|
||||
#[rustfmt::skip]
|
||||
let comma_or_close = |current_index, last_index| {
|
||||
if current_index == last_index { "]" } else { ", " }
|
||||
};
|
||||
|
||||
f.write_str("Witness: { ")?;
|
||||
|
||||
f.write_str(&indices_str(w))?;
|
||||
f.write_str(&indices_start_str(w))?;
|
||||
|
||||
f.write_str("witnesses: [")?;
|
||||
|
||||
let instructions = w.iter();
|
||||
let last_instruction = instructions.len() - 1;
|
||||
|
||||
for (i, instruction) in instructions.enumerate() {
|
||||
let bytes = instruction.iter();
|
||||
let last_byte = bytes.len() - 1;
|
||||
|
||||
f.write_str("[")?;
|
||||
|
||||
for (j, byte) in bytes.enumerate() {
|
||||
f.write_str(&byte_str(*byte))?;
|
||||
f.write_str(comma_or_close(j, last_byte))?;
|
||||
}
|
||||
|
||||
f.write_str(comma_or_close(i, last_instruction))?;
|
||||
}
|
||||
|
||||
f.write_str(" }")?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn fmt_debug_pretty(w: &Witness, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
|
||||
let mut indent = 0;
|
||||
|
||||
writeln(f, indent, "Witness: { ")?;
|
||||
indent += 1;
|
||||
|
||||
writeln(f, indent, &indices_str(w))?;
|
||||
writeln(f, indent, &indices_start_str(w))?;
|
||||
|
||||
writeln(f, indent, "witnesses: [ ")?;
|
||||
indent += 1;
|
||||
|
||||
let instructions = w.iter();
|
||||
|
||||
for instruction in instructions {
|
||||
let bytes = instruction.iter();
|
||||
let last_byte = bytes.len() - 1;
|
||||
|
||||
write(f, indent, "[")?;
|
||||
|
||||
for (j, byte) in bytes.enumerate() {
|
||||
f.write_str(&byte_str(*byte))?;
|
||||
if j == last_byte {
|
||||
f.write_str("],\n")?;
|
||||
} else {
|
||||
f.write_str(", ")?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
indent -= 1;
|
||||
writeln(f, indent, "],")?;
|
||||
|
||||
indent -= 1;
|
||||
write(f, indent, "}")?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn indices_str(w: &Witness) -> String { format!("indices: {}, ", w.witness_elements) }
|
||||
|
||||
fn indices_start_str(w: &Witness) -> String { format!("indices_start: {}, ", w.indices_start) }
|
||||
|
||||
fn byte_str(byte: u8) -> String { format!("{:#04x}", byte) }
|
||||
|
||||
fn writeln(f: &mut fmt::Formatter<'_>, indent: usize, s: &str) -> Result<(), fmt::Error> {
|
||||
write(f, indent, s)?;
|
||||
f.write_str("\n")
|
||||
}
|
||||
|
||||
fn write(f: &mut fmt::Formatter<'_>, indent: usize, s: &str) -> Result<(), fmt::Error> {
|
||||
for _ in 0..indent {
|
||||
f.write_str(" ")?;
|
||||
}
|
||||
f.write_str(s)
|
||||
}
|
||||
|
||||
/// An iterator returning individual witness elements.
|
||||
pub struct Iter<'a> {
|
||||
inner: &'a [u8],
|
||||
|
|
Loading…
Reference in New Issue