Improving bip32 ChildNumber display implementation
This commit is contained in:
parent
80b47f1f5b
commit
017cd71ca7
|
@ -169,8 +169,12 @@ impl From<ChildNumber> for u32 {
|
||||||
impl fmt::Display for ChildNumber {
|
impl fmt::Display for ChildNumber {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
match *self {
|
match *self {
|
||||||
ChildNumber::Hardened { index } => write!(f, "{}'", index),
|
ChildNumber::Hardened { index } => {
|
||||||
ChildNumber::Normal { index } => write!(f, "{}", index),
|
fmt::Display::fmt(&index, f)?;
|
||||||
|
let alt = f.alternate();
|
||||||
|
f.write_str(if alt { "'" } else { "h" })
|
||||||
|
},
|
||||||
|
ChildNumber::Normal { index } => fmt::Display::fmt(&index, f),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1083,5 +1087,15 @@ mod tests {
|
||||||
cc.to_string()
|
cc.to_string()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn fmt_child_number() {
|
||||||
|
assert_eq!("000005'", &format!("{:#06}", ChildNumber::from_hardened_idx(5).unwrap()));
|
||||||
|
assert_eq!("5'", &format!("{:#}", ChildNumber::from_hardened_idx(5).unwrap()));
|
||||||
|
assert_eq!("000005h", &format!("{:06}", ChildNumber::from_hardened_idx(5).unwrap()));
|
||||||
|
assert_eq!("5h", &format!("{}", ChildNumber::from_hardened_idx(5).unwrap()));
|
||||||
|
assert_eq!("42", &format!("{}", ChildNumber::from_normal_idx(42).unwrap()));
|
||||||
|
assert_eq!("000042", &format!("{:06}", ChildNumber::from_normal_idx(42).unwrap()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue