Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(primitives): update Bytes formatting, add UpperHex #446

Merged
merged 2 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/dyn-abi/src/coerce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ impl<'a> ValueParser<'a> {
}

#[inline]
#[allow(clippy::ptr_arg)]
fn tuple<'i: 's, 't: 's, 's>(
&'s self,
tuple: &'t Vec<DynSolType>,
Expand Down
45 changes: 22 additions & 23 deletions crates/primitives/src/bytes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,25 @@ pub struct Bytes(pub bytes::Bytes);

impl fmt::Debug for Bytes {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Bytes(")?;
f.write_str(&self.hex_encode())?;
f.write_str(")")
fmt::LowerHex::fmt(self, f)
}
}

impl fmt::Display for Bytes {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.hex_encode())
fmt::LowerHex::fmt(self, f)
}
}

impl fmt::LowerHex for Bytes {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.hex_encode())
f.pad(&hex::encode_prefixed(self.as_ref()))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should match std behavior here by adding 0x only when fmt.alternate() is true

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(as a drive-by while we're touching this code)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll open a new PR for this

}
}

impl fmt::UpperHex for Bytes {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.pad(&hex::encode_upper_prefixed(self.as_ref()))
}
}

Expand Down Expand Up @@ -315,11 +319,6 @@ impl Bytes {
pub fn split_to(&mut self, at: usize) -> Self {
Self(self.0.split_to(at))
}

#[inline]
fn hex_encode(&self) -> String {
hex::encode_prefixed(self.0.as_ref())
}
}

#[cfg(feature = "arbitrary")]
Expand Down Expand Up @@ -364,22 +363,22 @@ mod tests {

#[test]
fn parse() {
assert_eq!("1213".parse::<Bytes>().unwrap(), hex::decode("1213").unwrap());
assert_eq!("0x1213".parse::<Bytes>().unwrap(), hex::decode("0x1213").unwrap());
}

#[test]
fn hex() {
let b = Bytes::from_static(&[1, 35, 69, 103, 137, 171, 205, 239]);
let expected = "0x0123456789abcdef";
assert_eq!(format!("{b:x}"), expected);
assert_eq!(format!("{b}"), expected);
let expected = Bytes::from_static(&[0x12, 0x13, 0xab, 0xcd]);
assert_eq!("1213abcd".parse::<Bytes>().unwrap(), expected);
assert_eq!("0x1213abcd".parse::<Bytes>().unwrap(), expected);
assert_eq!("1213ABCD".parse::<Bytes>().unwrap(), expected);
assert_eq!("0x1213ABCD".parse::<Bytes>().unwrap(), expected);
}

#[test]
fn debug() {
fn format() {
let b = Bytes::from_static(&[1, 35, 69, 103, 137, 171, 205, 239]);
assert_eq!(format!("{b:?}"), "Bytes(0x0123456789abcdef)");
assert_eq!(format!("{b:#?}"), "Bytes(0x0123456789abcdef)");
assert_eq!(format!("{b}"), "0x0123456789abcdef");
assert_eq!(format!("{b:x}"), "0x0123456789abcdef");
assert_eq!(format!("{b:?}"), "0x0123456789abcdef");
assert_eq!(format!("{b:#?}"), "0x0123456789abcdef");
assert_eq!(format!("{b:#x}"), "0x0123456789abcdef");
assert_eq!(format!("{b:X}"), "0x0123456789ABCDEF");
assert_eq!(format!("{b:#X}"), "0x0123456789ABCDEF");
}
}
2 changes: 1 addition & 1 deletion crates/primitives/src/bytes/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ mod tests {

assert_eq!(
serde_json::from_value::<TestCase>(json).unwrap().variable,
Bytes::from(Vec::from([0, 1, 2, 3, 4]))
Bytes::from_static(&[0, 1, 2, 3, 4])
);
}

Expand Down
19 changes: 19 additions & 0 deletions crates/sol-types/src/types/data_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1090,6 +1090,7 @@ supported_int!(
mod tests {
use super::*;
use crate::sol;
use alloy_primitives::hex;

macro_rules! assert_encoded_size {
($t:ty, $sz:expr) => {
Expand Down Expand Up @@ -1427,4 +1428,22 @@ mod tests {
assert_eq!(<Int<248>>::detokenize(token), "0xff82038405860788098a0b8c0d8e0f901192139415961798199a1b9c1d9e1fa0".as_u256_as_i256());
assert_eq!(<Int<256>>::detokenize(token), "0x0182038405860788098a0b8c0d8e0f901192139415961798199a1b9c1d9e1fa0".as_u256_as_i256());
}

#[test]
fn encode_packed() {
let value = (RustAddress::with_last_byte(1), U256::from(2), 3, -3, 3, -3);
let res =
<sol! { (address, uint160, uint24, int24, uint32, int32) }>::abi_encode_packed(&value);
assert_eq!(
res,
hex!(
"0000000000000000000000000000000000000001"
"0000000000000000000000000000000000000002"
"000003"
"fffffd"
"00000003"
"fffffffd"
)
);
}
}