Skip to content

Commit

Permalink
make str continue to work with alloc
Browse files Browse the repository at this point in the history
  • Loading branch information
leighmcculloch committed May 13, 2024
1 parent 31890cf commit c1b62dd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
38 changes: 19 additions & 19 deletions src/curr/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
//#
//# ## Other
//# - ClaimableBalanceId
#![cfg(feature = "std")]
#![cfg(feature = "alloc")]

use super::{
AccountId, AssetCode, AssetCode12, AssetCode4, ClaimableBalanceId, Error, Hash, Limits,
MuxedAccount, MuxedAccountMed25519, NodeId, PublicKey, ReadXdr, ScAddress, SignerKey,
SignerKeyEd25519SignedPayload, Uint256, WriteXdr,
AccountId, AssetCode, AssetCode12, AssetCode4, ClaimableBalanceId, Error, Hash, MuxedAccount,
MuxedAccountMed25519, NodeId, PublicKey, ScAddress, SignerKey, SignerKeyEd25519SignedPayload,
Uint256,
};

impl From<stellar_strkey::DecodeError> for Error {
Expand Down Expand Up @@ -345,26 +345,26 @@ impl core::str::FromStr for ClaimableBalanceId {
type Err = Error;
fn from_str(s: &str) -> core::result::Result<Self, Self::Err> {
let bytes = hex::decode(s).map_err(|_| Error::InvalidHex)?;
ClaimableBalanceId::from_xdr(
bytes,
// No limit is safe for encoding ClaimableBalanceId as the type is a
// fixed size type.
Limits::none(),
)
match bytes.as_slice() {
[0, 0, 0, 0, ..] => Ok(ClaimableBalanceId::ClaimableBalanceIdTypeV0(Hash(
(&bytes[4..]).try_into()?,
))),
_ => Err(Error::Invalid),
}
}
}

impl core::fmt::Display for ClaimableBalanceId {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let bytes = self
.to_xdr(
// No limit is safe for encoding ClaimableBalanceId as the type is a
// fixed size type.
Limits::none(),
)
.map_err(|_| core::fmt::Error)?;
for b in bytes {
write!(f, "{b:02x}")?;
match self {
ClaimableBalanceId::ClaimableBalanceIdTypeV0(Hash(bytes)) => {
for b in [0u8, 0, 0, 0] {
write!(f, "{b:02x}")?;
}
for b in bytes {
write!(f, "{b:02x}")?;
}
}
}
Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions tests/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -555,11 +555,11 @@ fn claimable_balance_id() {
// Half byte short.
assert_eq!(ClaimableBalanceId::from_str("00000000010101010101010101010101010101010101010101010101010101010101010"), Err(Error::InvalidHex));
// Full byte short.
assert_eq!(ClaimableBalanceId::from_str("0000000001010101010101010101010101010101010101010101010101010101010101"), Err(Error::Io(std::io::Error::new(std::io::ErrorKind::UnexpectedEof, "failed to fill whole buffer"))));
assert_eq!(ClaimableBalanceId::from_str("0000000001010101010101010101010101010101010101010101010101010101010101"), Err(Error::LengthMismatch));
// Half byte too long.
assert_eq!(ClaimableBalanceId::from_str("0000000001010101010101010101010101010101010101010101010101010101010101011"), Err(Error::InvalidHex));
// Full byte too long.
assert_eq!(ClaimableBalanceId::from_str("00000000010101010101010101010101010101010101010101010101010101010101010101"), Err(Error::Invalid));
assert_eq!(ClaimableBalanceId::from_str("00000000010101010101010101010101010101010101010101010101010101010101010101"), Err(Error::LengthMismatch));
// Unrecognized discriminant value.
assert_eq!(ClaimableBalanceId::from_str("000000010101010101010101010101010101010101010101010101010101010101010101"), Err(Error::Invalid));
}

0 comments on commit c1b62dd

Please sign in to comment.