implements alternate formatting for address

This commit is contained in:
Riccardo Casatta 2021-04-07 15:49:58 +02:00
parent cac3f460a2
commit 104836a042
No known key found for this signature in database
GPG Key ID: FD986A969E450397
1 changed files with 15 additions and 13 deletions

View File

@ -359,11 +359,7 @@ impl Address {
/// Even inside Bitcoin URI may be more efficient to use the uppercase address since in QR codes /// Even inside Bitcoin URI may be more efficient to use the uppercase address since in QR codes
/// encoding modes can be mixed as needed within a QR symbol. /// encoding modes can be mixed as needed within a QR symbol.
pub fn to_qr_string(&self) -> String { pub fn to_qr_string(&self) -> String {
let address_string = self.to_string(); format!("{:#}", self)
match self.payload {
Payload::WitnessProgram { .. } => address_string.to_ascii_uppercase(),
_ => address_string,
}
} }
} }
@ -392,6 +388,11 @@ impl fmt::Display for Address {
version: ver, version: ver,
program: ref prog, program: ref prog,
} => { } => {
if fmt.alternate() {
//TODO format without allocation when alternate uppercase is in bech32
let lower = self.to_string();
write!(fmt, "{}", lower.to_ascii_uppercase())
} else {
let hrp = match self.network { let hrp = match self.network {
Network::Bitcoin => "bc", Network::Bitcoin => "bc",
Network::Testnet | Network::Signet => "tb", Network::Testnet | Network::Signet => "tb",
@ -403,6 +404,7 @@ impl fmt::Display for Address {
} }
} }
} }
}
} }
/// Extract the bech32 prefix. /// Extract the bech32 prefix.