Skip to content

Commit cfa7faa

Browse files
committed
Fix Display alternatives
1 parent 62f8e5a commit cfa7faa

File tree

8 files changed

+38
-10
lines changed

8 files changed

+38
-10
lines changed

plutus-ledger-api/src/v1/address.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ impl TryFromCSL<csl::Address> for Address {
8585
}
8686

8787
#[derive(Clone, Debug)]
88+
/// Address with network information. The `WithExtraInfo` variant has Display instance, serializing into
89+
/// a bech32 address format.
8890
pub struct AddressWithExtraInfo<'a> {
8991
pub address: &'a Address,
9092
pub network_tag: u8,
@@ -109,6 +111,7 @@ impl TryFromPLA<AddressWithExtraInfo<'_>> for csl::Address {
109111
}
110112
}
111113

114+
/// Serializing into a bech32 address format.
112115
impl std::fmt::Display for AddressWithExtraInfo<'_> {
113116
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
114117
let bech32_addr: Option<String> = self

plutus-ledger-api/src/v1/transaction.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ pub struct TransactionInput {
5454
pub index: BigInt,
5555
}
5656

57+
/// Serializing into a hexadecimal tx hash, followed by an tx id after a # (e.g. aabbcc#1)
5758
impl fmt::Display for TransactionInput {
5859
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
5960
write!(f, "{}#{}", self.transaction_id.0, self.index)

plutus-ledger-api/src/v1/value.rs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ impl CurrencySymbol {
7474
}
7575
}
7676

77+
/// Serialize into hexadecimal string, or empty string if Ada
78+
/// It returns `lovelace` instead of the empty string when the alternate flag is used (e.g.: format!("{:#}", cs))
7779
impl fmt::Display for CurrencySymbol {
7880
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
7981
match self {
@@ -377,14 +379,20 @@ impl fmt::Display for Value {
377379
.peekable();
378380
while let Some((cur_sym, tn, amount)) = it.next() {
379381
if cur_sym.is_ada() {
380-
write!(f, "{}", amount)?;
382+
amount.fmt(f)?;
381383
} else if tn.is_empty() {
382-
write!(f, "{} {}", amount, cur_sym)?;
384+
amount.fmt(f)?;
385+
" ".fmt(f)?;
386+
cur_sym.fmt(f)?;
383387
} else {
384-
write!(f, "{} {}.{}", amount, cur_sym, tn)?;
388+
amount.fmt(f)?;
389+
" ".fmt(f)?;
390+
cur_sym.fmt(f)?;
391+
".".fmt(f)?;
392+
tn.fmt(f)?;
385393
}
386394
if it.peek().is_some() {
387-
write!(f, "+")?;
395+
"+".fmt(f)?;
388396
}
389397
}
390398

@@ -756,6 +764,9 @@ impl IsPlutusData for TokenName {
756764
}
757765
}
758766

767+
/// Serialize into a hexadecimal string
768+
/// It tries to decode the token name from UTF8 when the alternate flag is used (e.g.: format!("{:#}", ac)),
769+
/// if failsed it prepends the hex value with `0x`
759770
impl fmt::Display for TokenName {
760771
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
761772
if f.alternate() {
@@ -796,12 +807,17 @@ pub struct AssetClass {
796807
pub token_name: TokenName,
797808
}
798809

810+
/// Serialize into two hexadecimal strings divided by a . (e.g. aabbcc.001122)
811+
/// It tries to decode the token name from UTF8 when the alternate flag is used (e.g.: format!("{:#}", ac)),
812+
/// if failsed it prepends the hex value with `0x`
799813
impl fmt::Display for AssetClass {
800814
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
801815
if self.token_name.is_empty() {
802-
write!(f, "{}", self.currency_symbol)
816+
self.currency_symbol.fmt(f)
803817
} else {
804-
write!(f, "{}.{}", self.currency_symbol, self.token_name)
818+
self.currency_symbol.fmt(f)?;
819+
".".fmt(f)?;
820+
self.token_name.fmt(f)
805821
}
806822
}
807823
}

plutus-ledger-api/src/v3/transaction.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ use num_bigint::BigInt;
66
#[cfg(feature = "serde")]
77
use serde::{Deserialize, Serialize};
88

9+
#[cfg(feature = "chrono")]
10+
pub use crate::v1::transaction::POSIXTimeConversionError;
911
pub use crate::v2::transaction::{
10-
DCert, POSIXTime, POSIXTimeRange, TransactionHash, TransactionId, TransactionInput,
11-
TransactionOutput, TxInInfo,
12+
DCert, POSIXTime, POSIXTimeRange, TransactionHash, TransactionInput, TransactionOutput,
13+
TransactionOutputWithExtraInfo, TxInInfo, WithdrawalsWithExtraInfo,
1214
};
1315
use crate::{
1416
self as plutus_ledger_api,

plutus-ledger-api/tests/display.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,15 @@ mod display_serialisation_tests {
5555
}
5656

5757
#[test]
58-
fn v1_asset_class_display() {
58+
fn v1_asset_class_display_1() {
5959
goldie::assert!(format!("{}", sample_asset_class()))
6060
}
6161

62+
#[test]
63+
fn v1_asset_class_display_2() {
64+
goldie::assert!(format!("{:#}", sample_asset_class()))
65+
}
66+
6267
#[test]
6368
fn v1_value_display_1() {
6469
goldie::assert!(format!("{}", sample_value()))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
01010101010101010101010101010101010101010101010101010101.Something
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
234+123 01010101010101010101010101010101010101010101010101010101.536f6d657468696e67
1+
234+123 01010101010101010101010101010101010101010101010101010101.Something

0 commit comments

Comments
 (0)